Upload
chen-stephen
View
688
Download
8
Embed Size (px)
DESCRIPTION
Watermark appears on printed page whatever text or image can mark document for specific use. The text or image you use can be an emblem of some brand, logo of a company, monogram of a product, etc. Microsoft Word supports both text and image watermark. Using watermark feature in Word document would be helpful to inform audience of your document about the constraints over the usage you have applied. Today, I will share a solution to use watermark in Word document for C#/VB.NET.
Citation preview
Use C#VB.NET Insert Watermark into Word Document
Watermark is a valuable asset to any Microsoft Word document. It lets user to mark the document
as private, confidential, and write any text that informs about the usage & credibility of the
document. Sometimes, a watermark is a recognizable background image or pattern that is
embedded in a document at various shades of lightness or darkness. Actually, the standard
placement of a watermark is done in such a way in the background (e.g. diagonal) to catch the
reader’s eye and to convey a readily recognizable message that portrays the status of the document
(e.g. confidential, draft, ASAP, etc.). Of course there are other non-standard methods of
embedding a watermark, such as adding your own image.
Watermark appears on printed page whatever text or image can mark document for specific use.
The text or image you use can be an emblem of some brand, logo of a company, monogram of a
product, etc. Microsoft Word supports both text and image watermark. Using watermark feature in
Word document would be helpful to inform audience of your document about the constraints over
the usage you have applied. Today, I will share a solution to use watermark in Word document for
C#/VB.NET.
In this solution, before using C#/VB.NET insert watermark in Word document, we need know
Spire.Doc, a MS Word component which enables user to perform a wide range of Word document
processing tasks directly, such as generate, read, write and modify Word document for .NET and
Silverlight. Now, just follow the simple steps below and we can add watermark in document.
Step 1
Download Spire.Doc and install on your system
Step 2
Create a windows form project in Visual Studio and drag a button onto the window. Add
Spire.Doc.dll as reference.
Step 3
Double-click the button and write the code below into the project. Here you can design at your
wish.
C# Code:
using System;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
namespace watermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertWatermark(document);
//Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertWatermark(Document document)
{
Paragraph paragraph = document.AddSection().AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a
document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Watermark is a valuable asset to any Microsoft Word
document. It lets user to mark the document as private, confidential, and write any text
that informs about the usage & credibility of the document. Sometimes, a watermark is a
recognizable background image or pattern that is embedded in a document at various
shades of lightness or darkness. Actually, the standard placement of a watermark is done
in such a way in the background (e.g. diagonal) to catch the reader’s eye and to convey a
readily recognizable message that portrays the status of the document (e.g. confidential,
draft, ASAP, etc.). Of course there are other non-standard methods of embedding a
watermark, such as adding your own image.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Watermark appears on printed page whatever text or
image can mark document for specific use. The text or image you use can be an emblem of
some brand, logo of a company, monogram of a product, etc. Microsoft Word supports both
text and image watermark. Using watermark feature in Word document would be helpful to
inform audience of your document about the constraints over the usage you have applied.
Today, I will share a solution to use watermark in Word document for C#/VB.NET.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a
document.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("In this solution, before using C#/VB.NET insert watermark
in Word document, we need know Spire.Doc, a MS Word component which enables user to
perform a wide range of Word document processing tasks directly, such as generate, read,
write and modify Word document for .NET and Silverlight. Now, just follow the simple steps
below and we can add watermark in document.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print
documents without utilizing Microsoft Word.");
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "E-ICEBLUE";
txtWatermark.FontSize = 70;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}
VB.NET Code:
Imports System
Imports System.Text
Imports System.Windows.Forms
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace watermark
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As
EventArgs)
'Create word document
Dim document As New Document()
InsertWatermark(document)
'Save doc file.
document.SaveToFile("Sample.doc", FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertWatermark(ByVal document As Document)
Dim paragraph As Paragraph =
document.AddSection().AddParagraph()
paragraph.AppendText("The sample demonstrates how to
insert a watermark into a document.")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
paragraph = document.Sections(0).AddParagraph()
paragraph.AppendText("Watermark is a valuable asset to any
Microsoft Word document. It lets user to mark the document as
private, confidential, and write any text that informs about the
usage & credibility of the document. Sometimes, a watermark is a
recognizable background image or pattern that is embedded in a
document at various shades of lightness or darkness. Actually, the
standard placement of a watermark is done in such a way in the
background (e.g. diagonal) to catch the reader’s eye and to convey a
readily recognizable message that portrays the status of the document (e.g.
confidential, draft, ASAP, etc.). Of course there are other non-standard methods of
embedding a watermark, such as adding your own image.")
paragraph = document.Sections(0).AddParagraph()
paragraph = document.Sections(0).AddParagraph()
paragraph.AppendText("Watermark appears on printed page
whatever text or image can mark document for specific use. The text or image
you use can be an emblem of some brand, logo of a company, monogram of a
product, etc. Microsoft Word supports both text and image watermark. Using
watermark feature in Word document would be helpful to inform audience of your
document about the constraints over the usage you have applied. Today, I will
share a solution to use watermark in Word document for C#/VB.NET.")
paragraph = document.Sections(0).AddParagraph()
paragraph = document.Sections(0).AddParagraph()
paragraph.AppendText("The sample demonstrates how to insert a
watermark into a document.")
paragraph = document.Sections(0).AddParagraph()
paragraph = document.Sections(0).AddParagraph()
paragraph.AppendText("In this solution, before using C#/VB.NET
insert watermark in Word document, we need know Spire.Doc, a MS Word
component which enables user to perform a wide range of Word document
processing tasks directly, such as generate, read, write and modify Word
document for .NET and Silverlight. Now, just follow the simple steps below and we
can add watermark in document.")
paragraph = document.Sections(0).AddParagraph()
paragraph = document.Sections(0).AddParagraph()
paragraph.AppendText("Spire.Doc can generate, modify, convert,
render and print documents without utilizing Microsoft Word.")
Dim txtWatermark As New TextWatermark()
txtWatermark.Text = "E-ICEBLUE"
txtWatermark.FontSize = 70
txtWatermark.Layout = WatermarkLayout.Diagonal
document.Watermark = txtWatermark
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
TrySystem.Diagnostics.Process.Start(fileName)
Catch
End Try
End Sub
End Class
End Namespace
Step 4
Press F5 to start your project and click the button. The Word document with watermark will be
generated automatically.
Effective Screenshot:
As a professional and powerful Word component, Spire.Doc doesn't need Microsoft Office Word
Automation but also allows user to directly operate Word document, format and style and insert
content to Word document. With high quality, powerful functions and cheap price, Spire.Doc
supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more …