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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Arial",19));// Add watermark to all headers of the first section
WordProcessingWatermarkSectionOptionsoptions=newWordProcessingWatermarkSectionOptions();options.setSectionIndex(0);watermarker.add(watermark,options);watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);System.out.println(content.getSections().get_Item(0).getPageSetup().getWidth());System.out.println(content.getSections().get_Item(0).getPageSetup().getHeight());System.out.println(content.getSections().get_Item(0).getPageSetup().getTopMargin());System.out.println(content.getSections().get_Item(0).getPageSetup().getRightMargin());System.out.println(content.getSections().get_Item(0).getPageSetup().getBottomMargin());System.out.println(content.getSections().get_Item(0).getPageSetup().getLeftMargin());watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(45);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);// Get all images belonging to the first section
WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);WatermarkableImageCollectionimages=content.getSections().get_Item(0).findImages();// Add watermark to all found images
for(WatermarkableImageimage:images){image.add(watermark);}watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(45);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);for(WordProcessingSectionsection:content.getSections()){for(WordProcessingShapeshape:section.getShapes()){// Headers&Footers usually contains only service information.
// So, we skip images in headers/footers, expecting that they are probably watermarks or backgrounds
if(shape.getHeaderFooter()==null&&shape.getImage()!=null){shape.getImage().add(watermark);}}}watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);TextWatermarktextWatermark=newTextWatermark("DRAFT",newFont("Arial",42));// Add watermark to the last page
WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);WordProcessingWatermarkPagesOptionsoptions=newWordProcessingWatermarkPagesOptions();options.setPageNumbers(newint[]{content.getPageCount()});watermarker.add(textWatermark,options);watermarker.save("document.docx");watermarker.close();
Working with headers and footers
Linking headers and footers
Header/footer in a Word document can be linked to the corresponding header/footer in the previous section. In this way, the same content is displayed in the linked header/footer. GroupDocs.Watermark API provides the option to link the header/footer using setLinkedToPrevious() method 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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Link footer for even numbered pages to corresponding footer in previous section
content.getSections().get_Item(1).getHeadersFooters().getByOfficeHeaderFooterType(OfficeHeaderFooterType.FooterEven).setLinkedToPrevious(true);watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Link footer for even numbered pages to corresponding footer in previous section
content.getSections().get_Item(1).getHeadersFooters().get_Item(1).setLinkedToPrevious(true);watermarker.save("document.docx");watermarker.close();
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.
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);ImageWatermarkwatermark=newImageWatermark("large.png");// Add watermark to all headers of the first section
WordProcessingWatermarkSectionOptionsoptions=newWordProcessingWatermarkSectionOptions();options.setSectionIndex(0);watermarker.add(watermark,options);// Link all other headers&footers to corresponding headers&footers of the first section
WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);for(inti=1;i<content.getSections().getCount();i++){content.getSections().get_Item(i).getHeadersFooters().linkToPrevious(true);}watermarker.save("document.docx");watermarker.close();
Setting different headers and 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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);content.getSections().get_Item(0).getPageSetup().setDifferentFirstPageHeaderFooter(true);content.getSections().get_Item(0).getPageSetup().setOddAndEvenPagesHeaderFooter(true);watermarker.save("document.docx");watermarker.close();