Aspose.words is an advanced document processing control that enables users to perform a variety of document processing tasks in individual applications without using Microsoft words, including document processing such as document generation, modification, rendering, printing, document format conversion, and mail merge. In addition, Aspose.words supports Doc,ooxml,rtf,html,opendocument, PDF, XPS, epub and other formats.
Sometimes you need to insert a watermark in a Word document, such as if you want to print a draft document or mark it as confidential. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" J_0057.gif "/>
In Microsoft Word, you can use the Insert Watermark command to quickly insert a watermark. Not many people use this command to recognize that such a watermark is just a shape inserted with text into the header or footer, or in the center of the page. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0066.gif "alt=" J_0066.gif "/>
and in the Aspose.words, without a single “ insert watermark ” command just like Microsoft Word, it's easy to put
Shape or image into a header or footer, creating a watermark of any conceivable type. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0064.gif "alt=" J_0064.gif "/>
Example
Insert the watermark into a Word document.
C#
Using System;
Using System.Drawing;
Using System.IO;
Using System.Reflection;
Using Aspose.words;
Using Aspose.Words.Drawing;
Using Aspose.Words.Fields;
Namespace Addwatermark
{
public class Program
{
public static void Main (string[] args)
{
Sample infrastructure.
String exedir = Path.getdirectoryname (assembly.getexecutingassembly (). Location) + Path.directoryseparatorchar;
String datadir = new Uri (new Uri (Exedir), @ ": /.. /data/"). LocalPath;
Document doc = new document (DataDir + "Testfile.doc");
Insertwatermarktext (Doc, "Confidential");
Doc. Save (DataDir + "testfile out.doc");
}
<summary>
Inserts a watermark into a document.
</summary>
<param name= "Doc" >the input document.</param>
<param name= "Watermarktext" >text of the Watermark.</param>
private static void Insertwatermarktext (Document doc, String watermarktext)
{
Create a watermark shape. This would be a WordArt shape.
You is free to try the other shape types as watermarks.
Shape watermark = new shape (doc, shapetype.textplaintext);
Set up the text of the watermark.
Watermark. Textpath.text = Watermarktext;
Watermark. textpath.fontfamily = "Arial";
Watermark. Width = 500;
Watermark. Height = 100;
Text'll is directed from the Bottom-left to the Top-right corner.
Watermark. Rotation =-40;
Remove the following and lines if you need a solid black text.
Watermark. Fill.color = Color.gray; Try Lightgray to get more word-style watermark
Watermark. Strokecolor = Color.gray; Try Lightgray to get more word-style watermark
Place the watermark in the page center.
Watermark. RelativeHorizontalPosition = Relativehorizontalposition.page;
Watermark. RelativeVerticalPosition = Relativeverticalposition.page;
Watermark. Wraptype = Wraptype.none;
Watermark. VerticalAlignment = Verticalalignment.center;
Watermark. HorizontalAlignment = HorizontalAlignment.Center;
Create a new paragraph and append the watermark to this paragraph.
Paragraph Watermarkpara = new Paragraph (DOC);
Watermarkpara.appendchild (watermark);
Insert the watermark into all headers for each document section.
foreach (section sect in Doc. Sections)
{
There could is up to three different headers in each section, since we want
The watermark to appear in all pages, insert into all headers.
Insertwatermarkintoheader (Watermarkpara, sect, Headerfootertype.headerprimary);
Insertwatermarkintoheader (Watermarkpara, sect, Headerfootertype.headerfirst);
Insertwatermarkintoheader (Watermarkpara, sect, Headerfootertype.headereven);
}
}
private static void Insertwatermarkintoheader (Paragraph Watermarkpara, section sect, Headerfootertype Headertype)
{
HeaderFooter Header = sect. Headersfooters[headertype];
if (header = = null)
{
There is no header of the specified type in the current section, create it.
Header = new HeaderFooter (sect. Document, Headertype);
Sect. Headersfooters.add (header);
}
Insert a clone of the watermark into the header.
Header. AppendChild (Watermarkpara.clone (true));
}
}
}
visual basic imports microsoft.visualbasic imports system imports system.drawing imports system.ioimports system.reflectionimports Aspose.words imports aspose.words.drawing imports aspose.words.fieldsnamespace addwatermark public class program public shared sub main ( Byval args () as string) ' Sample infrastructure. Dim Exedir as string = path.getdirectoryname (System.Reflection.Assembly.GetExecutingAssembly () . Location) + Path.DirectorySeparatorChar Dim dataDir As String = New uri (New uri (Exedir), ". /.. /data/"). Localpathdim doc as new document (datadir & "TestFile.doc") Insertwatermarktext (doc, "Confidential") doc. Save (datadir & " Testfile out.doc ") end sub" <summary> " inserts a watermark into a document. ' </summary> ' < Param name= "Doc" >The input document.</param> " <param name=" Watermarktext ">text of the watermark.</param> private shared sub insertwatermarktext (byval doc as document, byval watermarktext as String) ' Create a watermark shape. This will be a wordart shape. ' you are free to try other shape types as watermarks. dim watermark as new shape (doc, Shapetype.textplaintext) ' Set up the text of the watermark. Watermark. textpath.text = watermarktext watermark. textpath.fontfamily = "Arial" watermark. Width = 500 watermark. height = 100 ' Text will be directed from the Bottom-left to the top-right corner. watermark. rotation = -40 ' remove the following two lines if you need a solid black text. watermark. fill.color = color.gray ' try lightgray to get more word-style watermark watermark. strokecolor = color.gray ' try lightgray to get more word-style watermark ' Place the watermark in the page center. Watermark. Relativehorizontalposition = relativehorizontalposition.page watermark. relativeverticalposition = Relativeverticalposition.page watermark. Wraptype = wraptype.none watermark. Verticalalignment = verticalalignment.center watermark. Horizontalalignment = horizontalalignment.center ' Create a new paragraph And append the watermark to this paragraph. dim watermarkpara as new paragraph (DOC) watermarkpara.appendchild (watermark) ' insert the watermark into all headers of each document section. for each sect as section in doc. sections ' there could be up to three different headers in each section, since we want ' the watermark to appear on all pages, insert into all headers. InsertwatermarkinToheader (watermarkpara, sect, headerfootertype.headerprimary) insertwatermarkintoheader ( Watermarkpara, sect, headerfootertype.headerfirst) insertwatermarkintoheader ( Watermarkpara, sect, headerfootertype.headereven) Next sect End Subprivate shared sub insertwatermarkintoheader (byval watermarkpara as paragraph , byval sect as section, byval headertype as headerfootertype) dim header as headerfooter = sect. HeadersFooters (headertype) if header is nothing then ' There is no header of the specified type in the current section, Create it. header = new headerfooter (Sect. Document, headertype) sect. Headersfooters.add (header) end if ' insert a&nbsP;clone of the watermark into the header. header. AppendChild (Watermarkpara.clone (True)) End Sub End Class End Namespace
See more aspose.words Information 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0071.gif "alt=" j_0071.gif "/ >
Aspose.words How to add a watermark to a document