Add watermarks to word processing documents
Leave feedback
Microsoft Word allows the user to divide and format the document into multiple sections. Defining sections in the document enables the user to set specific page layout and formatting for different parts of the document. An example of the sections is headers and footers. Headers and footers are used to display text or any graphical object on all the pages.
Adding watermark to a particular section
GroupDocs.Watermark API allows you to add watermark objects in the headers and footers of the page. Adding watermark to a section of a Word document using GroupDocs.Watermark consists of following steps.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Arial",19));// Add watermark to all headers of the first sectionWordProcessingWatermarkSectionOptionsoptions=newWordProcessingWatermarkSectionOptions();options.SectionIndex=0;watermarker.Add(watermark,options);watermarker.Save("document.docx");}
The code snippet above adds watermark to the first section (to all headers of this section). So, it will be displayed on all pages belonging to the section.
Getting page size
If for some reasons you want to use absolute sizing and positioning, you may also need to get some page properties for a section. GroupDocs.Watermark allows you to extract information about a particular section.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();Console.WriteLine(content.Sections[0].PageSetup.Width);Console.WriteLine(content.Sections[0].PageSetup.Height);Console.WriteLine(content.Sections[0].PageSetup.TopMargin);Console.WriteLine(content.Sections[0].PageSetup.RightMargin);Console.WriteLine(content.Sections[0].PageSetup.BottomMargin);Console.WriteLine(content.Sections[0].PageSetup.LeftMargin);}
Adding watermark to the images inside a particular section
Using GroupDocs.Watermark, you can add watermark to the images that belong to a particular section.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=45;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;// Get all images belonging to the first sectionWordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();WatermarkableImageCollectionimages=content.Sections[0].FindImages();// Add watermark to all found imagesforeach(WatermarkableImageimageinimages){image.Add(watermark);}watermarker.Save("document.docx");}
Adding watermark to the image shapes in a word document
Word document may also contain different shapes. The API allows you to use shape collection to add watermark to images in a document. Below example shows how to add watermark to image shapes.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.HorizontalAlignment=HorizontalAlignment.Center;watermark.VerticalAlignment=VerticalAlignment.Center;watermark.RotateAngle=45;watermark.SizingType=SizingType.ScaleToParentDimensions;watermark.ScaleFactor=1;WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();foreach(WordProcessingSectionsectionincontent.Sections){foreach(WordProcessingShapeshapeinsection.Shapes){// Headers&Footers usually contains only service information.// So, we skip images in headers/footers, expecting that they are probably watermarks or backgroundsif(shape.HeaderFooter==null&&shape.Image!=null){shape.Image.Add(watermark);}}}watermarker.Save("document.docx");}
Adding watermark to a particular page of word document
GroupDocs.Watermark enables you to add watermark to a particular page of a Word document. You can use following example to achieve this.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){TextWatermarktextWatermark=newTextWatermark("DRAFT",newFont("Arial",42));// Add watermark to the last pageWordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();WordProcessingWatermarkPagesOptionsoptions=newWordProcessingWatermarkPagesOptions();options.PageNumbers=newint[]{content.PageCount};watermarker.Add(textWatermark,options);watermarker.Save("document.docx");}
Working with headers and footers
Linking headers and footers
Header or footer in a Word document can be linked to the corresponding header or footer in the previous section. In this way, the same content is displayed in the linked header or footer. GroupDocs.Watermark API provides the option to link the header or footer using IsLinkedToPrevious property of WordProcessingHeaderFooter class. Following code snippet serves this purpose.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Link footer for even numbered pages to corresponding footer in previous sectioncontent.Sections[1].HeadersFooters[OfficeHeaderFooterType.FooterEven].IsLinkedToPrevious=true;watermarker.Save("document.docx");}
Linking all headers and footers
Following code snippet links all the headers and footers in a particular section.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Link footer for even numbered pages to corresponding footer in previous sectioncontent.Sections[1].HeadersFooters[1].IsLinkedToPrevious=true;watermarker.Save("document.docx");}
Add watermark to headers and footers with linking
This feature can be useful to reduce resultant file size when you’re adding image watermark to all sections.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){using(ImageWatermarkwatermark=newImageWatermark("large.png")){// Add watermark to all headers of the first sectionWordProcessingWatermarkSectionOptionsoptions=newWordProcessingWatermarkSectionOptions();options.SectionIndex=0;watermarker.Add(watermark,options);}// Link all other headers&footers to corresponding headers&footers of the first sectionWordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();for(inti=1;i<content.Sections.Count;i++){content.Sections[i].HeadersFooters.LinkToPrevious(true);}watermarker.Save("document.docx");}
Setting different headers or footers
Using GroupDocs.Watermark API, you can also set different headers or footers for even and odd numbered pages and for the first page of the document (as shown in below example).
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();content.Sections[0].PageSetup.DifferentFirstPageHeaderFooter=true;content.Sections[0].PageSetup.OddAndEvenPagesHeaderFooter=true;watermarker.Save("document.docx");}