Add watermarks to PDF documents

Adding watermark to a particular page 

GroupDocs.Watermark API allows you to add watermark to a particular page of a PDF document. Adding watermark to a PDF document using GroupDocs.Watermark consists of following steps.

  1. Load the document 
  2. Create and initialize watermark object 
  3. Set watermark properties 
  4. Add watermark to the page of the document using method setPageIndex() of PdfArtifactWatermarkOptions
  5. Save the document

Following code performs this functionality.

advanced_usage.add_watermarks_to_pdf.PdfAddWatermarks

PdfLoadOptions loadOptions = new PdfLoadOptions();                                                       
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions);                         
                                                                                                         
// Add text watermark to the first page                                                                  
TextWatermark textWatermark = new TextWatermark("This is a test watermark", new Font("Arial", 8));       
PdfArtifactWatermarkOptions textWatermarkOptions = new PdfArtifactWatermarkOptions();                    
textWatermarkOptions.setPageIndex(0);                                                                    
                                                                                                         
watermarker.add(textWatermark, textWatermarkOptions);                                                    
                                                                                                         
// Add image watermark to the second page                                                                
ImageWatermark imageWatermark = new ImageWatermark(Constants.ProtectJpg);                                
                                                                                                         
PdfArtifactWatermarkOptions imageWatermarkOptions = new PdfArtifactWatermarkOptions();                   
imageWatermarkOptions.setPageIndex(1);                                                                   
watermarker.add(imageWatermark, imageWatermarkOptions);                                                  
                                                                                                         
imageWatermark.close();                                                                                  
                                                                                                         
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();                                                                                     

Adding watermark to all images of a particular page

The API also allows you to add watermark to the images inside a particular page of the PDF document. Following code snippet shows how to add watermark to all images in a particular page of a PDF document.

advanced_usage.add_watermarks_to_pdf.PdfAddWatermarkToImages

PdfLoadOptions loadOptions = new PdfLoadOptions();                                                       
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions);                         
                                                                                                         
// Initialize image or text watermark                                                                    
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);                                                                             
                                                                                                         
PdfContent pdfContent = watermarker.getContent(PdfContent.class);                                        
                                                                                                         
// Get all images from the first page                                                                    
WatermarkableImageCollection images = pdfContent.getPages().get_Item(0).findImages();                    
                                                                                                         
// Add watermark to all found images                                                                     
for (WatermarkableImage image : images)                                                                  
{                                                                                                        
    image.add(watermark);                                                                                
}                                                                                                        
                                                                                                         
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();                                                                                     

Getting page size 

If for some reasons you want to use absolute sizing and positioning, you may also need to determine page size. GroupDocs.Watermark also provides the feature to get the dimensions of the page in a PDF document. Below example shows how to get width and height of a particular page.

advanced_usage.add_watermarks_to_pdf.PdfGetDimensions

PdfLoadOptions loadOptions = new PdfLoadOptions();                                                       
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions);                         
                                                                                                         
PdfContent pdfContent = watermarker.getContent(PdfContent.class);                                        
                                                                                                         
System.out.println(pdfContent.getPages().get_Item(0).getWidth());                                        
System.out.println(pdfContent.getPages().get_Item(0).getHeight());                                       
                                                                                                         
watermarker.close();                                                                                     

Page margins in PDF document

If a PDF document contains a crop box definition, Adobe Acrobat uses it for screen display and printing. That’s why GroupDocs.Watermark uses crop box by default to calculate relative watermark size and position. When you call Watermark.setConsiderParentMargins() mathods with true, trim box rectangle is used instead. But what if you want to get watermark aligned to bleed box or art box? In this case, you need to set the appropriate value with PdfContent.setPageMarginType() method as shown in the below example. advanced_usage.add_watermarks_to_pdf.PdfAddWatermarkWithPageMarginType

PdfLoadOptions loadOptions = new PdfLoadOptions();                                                       
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions);                         
                                                                                                         
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 42));                    
watermark.setHorizontalAlignment(HorizontalAlignment.Right);                                             
watermark.setVerticalAlignment(VerticalAlignment.Top);                                                   
watermark.setSizingType(SizingType.ScaleToParentDimensions);                                             
watermark.setScaleFactor(1);                                                                             
                                                                                                         
PdfContent pdfContent = watermarker.getContent(PdfContent.class);                                        
pdfContent.setPageMarginType(PdfPageMarginType.BleedBox);                                                
watermark.setConsiderParentMargins(true);                                                                
                                                                                                         
watermarker.add(watermark);                                                                              
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();                                                                                     

Add watermark to all attachments

GroupDocs.Watermark also provides the feature to add watermark to supported files in all attachments in a PDF document. Below example shows how to add watermark to all supported attachments.

advanced_usage.add_watermarks_to_pdf.PdfAddWatermarkToAllAttachments

TextWatermark watermark = new TextWatermark("This is WaterMark on Attachment", new Font("Arial", 19));   
PdfLoadOptions loadOptions = new PdfLoadOptions();                                                       
// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf, loadOptions);                         
                                                                                                         
PdfContent pdfContent = watermarker.getContent(PdfContent.class);                                        
for (PdfAttachment attachment : pdfContent.getAttachments())                                             
{                                                                                                        
    // Check if the attached file is supported by GroupDocs.Watermark                                    
    IDocumentInfo info = attachment.getDocumentInfo();                                                   
    if (info.getFileType() != FileType.Unknown && !info.isEncrypted())                                   
    {                                                                                                    
        // Load the attached document                                                                    
        Watermarker attachedWatermarker = attachment.createWatermarker();                                
                                                                                                         
        // Add watermark                                                                                 
        attachedWatermarker.add(watermark);                                                              
                                                                                                         
        // Save changes in the attached file                                                             
        attachment.updateContent(attachedWatermarker);                                                   
                                                                                                         
        attachedWatermarker.close();                                                                     
    }                                                                                                    
}                                                                                                        
                                                                                                         
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();                                                                                     

Advanced use cases

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.