Adding Text Watermark

Adding Text Watermark in Java

The GroupDocs.Watermark for Java which is a part of Conholdate.Total for Java allows you to add text watermarks on images or other documents.

Follow the steps below to do the job:

  • 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).

Please note, if the document consists of multiple parts such as pages, worksheets, slides, frames etc, the watermark will be added to all of them.

Code snippet below demonstrates how to add text watermark to a document. 

// Constants.InDocumentPdf is an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarker watermarker = new Watermarker(Constants.InDocumentPdf);                                      
                                                                                                         
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(Constants.OutDocumentPdf);                                                              
                                                                                                         
watermarker.close();