Add text or image watermark

GroupDocs.Watermark allows adding watermarks and saving the resultant documents. The full list of supported document formats can be found here. You may add text and image watermarks to the documents from the local disk and from streams.

Add a text watermark

The following example demonstrates how to add a TextWatermark to a local document:

  • Create a watermarker for the local file (line 2);
  • Create a watermark with text and font (line 4);
  • Set the watermark color, horizontal and vertical alignments (lines 5-7);
  • Add the watermark to the document (line 9);
  • Save the document to the new file (line 10).
  • Close the watermarker and free its resources (line 12).

basic_usage.AddATextWatermark

// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker("document.pdf");                                      
                                                                                                         
TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));                        
watermark.setForegroundColor(Color.getRed());                                                            
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                            
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                
                                                                                                         
watermarker.add(watermark);                                                                              
watermarker.save("document.pdf");                                                              
                                                                                                         
watermarker.close();                                                                                   

Add an Image Watermark

The following example demonstrates how to add an ImageWatermark to a document from a stream:

  • Create a watermarker for the file stream (line 4);
  • Create an image watermark from the local image file (line 6);
  • Set the watermark horizontal and vertical alignments (lines 7, 8);
  • Add the watermark to the document (line 9);
  • Save the document to the new file (line 11).
  • Close the watermark and free its resources (line 13);
  • Close the watermarker and free its resources (line 14).

basic_usage.AddAnImageWatermark

// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.xlsx" 
FileInputStream stream = new FileInputStream("document.xlsx");                                     
                                                                                                            
Watermarker watermarker = new Watermarker(stream);                                                          
                                                                                                            
ImageWatermark watermark = new ImageWatermark("logo.png");                                           
watermark.setHorizontalAlignment(HorizontalAlignment.Center);                                               
watermark.setVerticalAlignment(VerticalAlignment.Center);                                                   
watermarker.add(watermark);                                                                                 
                                                                                                            
watermarker.save("document.xlsx");                                                                
                                                                                                            
watermark.close();                                                                                          
watermarker.close();                                                                                      
stream.close();