Attachments in PDF document

Watermarks in PDF document

XObjects

When add() method of Watermarker class is called, simple XObject is added to a PDF document.

 Image XObject and Form XObject are used by GroupDocs.Watermark API to add ImageWatermark and TextWatermark respectively. XObjects are considered as a page real content, therefore, they are not removed by Adobe Acrobat during document sanitization.

Artifacts

According to artifact definition, the watermark can be represented by an artifact in a PDF document. The following example shows how artifact watermark can be added to a document with GroupDocs.Watermark using PdfArtifactWatermarkOptions. advanced_usage.add_watermarks_to_pdf.PdfAddArtifactWatermark

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);                         
                                                                                                         
PdfArtifactWatermarkOptions options = new PdfArtifactWatermarkOptions();                                 
                                                                                                         
// Add text watermark                                                                                    
TextWatermark textWatermark = new TextWatermark("This is an artifact watermark", new Font("Arial", 8));  
textWatermark.setHorizontalAlignment(HorizontalAlignment.Right);                                         
watermarker.add(textWatermark, options);                                                                 
                                                                                                         
// Add image watermark                                                                                   
ImageWatermark imageWatermark = new ImageWatermark(Constants.LogoBmp);                                   
                                                                                                         
watermarker.add(imageWatermark, options);                                                                
                                                                                                         
imageWatermark.close();                                                                                  
                                                                                                         
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();                                                                                     

Annotations

Annotation is the third type of PDF entities by which a watermark can be represented. Use the following code snippet to add watermark annotation to a PDF document using PdfAnnotationWatermarkOptions. advanced_usage.add_watermarks_to_pdf.PdfAddAnnotationWatermark

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);                         
                                                                                                         
PdfAnnotationWatermarkOptions options = new PdfAnnotationWatermarkOptions();                             
                                                                                                         
// Add text watermark                                                                                    
TextWatermark textWatermark = new TextWatermark("This is a annotation watermark", new Font("Arial", 8)); 
textWatermark.setHorizontalAlignment(HorizontalAlignment.Left);                                          
textWatermark.setVerticalAlignment(VerticalAlignment.Top);                                               
watermarker.add(textWatermark, options);                                                                 
                                                                                                         
// Add image watermark                                                                                   
ImageWatermark imageWatermark = new ImageWatermark(Constants.ProtectJpg);                                
                                                                                                         
imageWatermark.setHorizontalAlignment(HorizontalAlignment.Right);                                        
imageWatermark.setVerticalAlignment(VerticalAlignment.Top);                                              
watermarker.add(imageWatermark, options);                                                                
                                                                                                         
watermarker.save(Constants.OutDocumentPdf);                                                              
                                                                                                         
imageWatermark.close();                                                                                  
watermarker.close();                                                                                     

You can also add print only annotation watermark to the document using setPrintOnly() method of PdfAnnotationWatermarkOptions. The following code demonstrates this approach.

advanced_usage.add_watermarks_to_pdf.PdfAddPrintOnlyAnnotationWatermark

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 textWatermark = new TextWatermark("This is a print only test watermark. It won't appear in view mode.", new Font("Arial", 8));
Boolean isPrintOnly = true;                                                                                                                 
                                                                                                                                            
// Annotation will be printed, but not displayed in pdf viewing application                                                                 
PdfAnnotationWatermarkOptions options = new PdfAnnotationWatermarkOptions();                                                                
options.setPageIndex(0);                                                                                                                    
options.setPrintOnly(isPrintOnly);                                                                                                          
watermarker.add(textWatermark, options);                                                                                                    
                                                                                                                                            
watermarker.save(Constants.OutDocumentPdf);                                                                                                 
                                                                                                                                            
watermarker.close();                                                                                                                        

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.