Add watermarks to word processing documents

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.

  1. Load the document 
  2. Create and initialize watermark object 
  3. Set watermark properties
  4. Create WordProcessingWatermarkSectionOptions and call setSectionIndex()
  5. Add watermark to the section of the document
  6. Save the document

Following code adds watermark to the headers of a particular section.

advanced_usage.add_watermarks_to_word_processing.WordProcessingAddWatermarkToSection

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 19));                      
                                                                                                           
// Add watermark to all headers of the first section                                                       
WordProcessingWatermarkSectionOptions options = new WordProcessingWatermarkSectionOptions();               
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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingGetSectionProperties

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
WordProcessingContent content = 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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingAddWatermarkToSectionImages

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
TextWatermark watermark = new TextWatermark("Protected image", new Font("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                                                           
WordProcessingContent content = watermarker.getContent(WordProcessingContent.class);                       
WatermarkableImageCollection images = content.getSections().get_Item(0).findImages();                      
                                                                                                           
// Add watermark to all found images                                                                       
for (WatermarkableImage image : 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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingAddWatermarkToShapeImages

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                    
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx" 
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                           
                                                                                                            
TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));                       
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                               
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                   
watermark.setRotateAngle(45);                                                                               
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                                
watermark.setScaleFactor(1);                                                                                
                                                                                                            
WordProcessingContent content = watermarker.getContent(WordProcessingContent.class);                        
for (WordProcessingSection section : content.getSections())                                                 
{                                                                                                           
    for (WordProcessingShape shape : 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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingAddWatermarkToParticularPage

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
TextWatermark textWatermark = new TextWatermark("DRAFT", new Font("Arial", 42));                           
                                                                                                           
// Add watermark to the last page                                                                          
WordProcessingContent content = watermarker.getContent(WordProcessingContent.class);                       
WordProcessingWatermarkPagesOptions options = new WordProcessingWatermarkPagesOptions();                   
options.setPageNumbers(new int[] {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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingLinkHeaderFooterInSection

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                                                       
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"                                    
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                                                              
                                                                                                                                               
WordProcessingContent content = 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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingLinkAllHeaderFooterInSection

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
WordProcessingContent content = 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.

advanced_usage.add_watermarks_to_word_processing.WordProcessingAddImageWatermark

// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
ImageWatermark watermark = new ImageWatermark("large.png");                                         
                                                                                                           
// Add watermark to all headers of the first section                                                       
WordProcessingWatermarkSectionOptions options = new WordProcessingWatermarkSectionOptions();               
options.setSectionIndex(0);                                                                                
watermarker.add(watermark, options);                                                                       
                                                                                                           
// Link all other headers&footers to corresponding headers&footers of the first section                    
WordProcessingContent content = watermarker.getContent(WordProcessingContent.class);                       
for (int i = 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).

advanced_usage.add_watermarks_to_word_processing.WordProcessingSetDifferentFirstPageHeaderFooter

WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();                                   
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarker watermarker = new Watermarker("document.docx", loadOptions);                          
                                                                                                           
WordProcessingContent content = 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();