Page events for Watermarking

This is a code example of iText PDF, discover more.

8th November 2015
admin-marketing

Switch code for this example

Watermarking.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/21575630/adding-watermark-directly-to-the-stream
  4.  *
  5.  * Adding a watermark to the document immediately using a page event.
  6.  */
  7. package sandbox.events;
  8.  
  9. import com.itextpdf.text.BaseColor;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Element;
  13. import com.itextpdf.text.Font;
  14. import com.itextpdf.text.Font.FontFamily;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.pdf.ColumnText;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfPTable;
  19. import com.itextpdf.text.pdf.PdfPageEventHelper;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.util.StringTokenizer;
  28.  
  29. import sandbox.WrapToTest;
  30.  
  31. @WrapToTest
  32. public class Watermarking {
  33.  
  34.     public static final String DEST = "results/events/united_states.pdf";
  35.     public static final String DATA = "resources/data/united_states.csv";
  36.     public static final Font FONT = new Font();
  37.     public static final Font BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  38.  
  39.     public static void main(String[] args) throws IOException, DocumentException {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new Watermarking().createPdf(DEST);
  43.     }
  44.    
  45.     public class Watermark extends PdfPageEventHelper {
  46.  
  47.         protected Phrase watermark = new Phrase("WATERMARK", new Font(FontFamily.HELVETICA, 60, Font.NORMAL, BaseColor.LIGHT_GRAY));
  48.        
  49.         @Override
  50.         public void onEndPage(PdfWriter writer, Document document) {
  51.             PdfContentByte canvas = writer.getDirectContentUnder();
  52.             ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, watermark, 298, 421, 45);
  53.         }
  54.     }
  55.    
  56.     public void createPdf(String dest) throws IOException, DocumentException {
  57.         Document document = new Document();
  58.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  59.         writer.setPageEvent(new Watermark());
  60.         document.open();
  61.         PdfPTable table = new PdfPTable(3);
  62.         table.setWidthPercentage(100);
  63.         table.setWidths(new int[]{4, 1, 3});
  64.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  65.         String line = br.readLine();
  66.         process(table, line, BOLD);
  67.         table.setHeaderRows(1);
  68.         while ((line = br.readLine()) != null) {
  69.             process(table, line, FONT);
  70.         }
  71.         br.close();
  72.         document.add(table);
  73.         document.close();
  74.     }
  75.    
  76.     public void process(PdfPTable table, String line, Font font) {
  77.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  78.         int c = 0;
  79.         while (tokenizer.hasMoreTokens() && c++ < 3) {
  80.             table.addCell(new Phrase(tokenizer.nextToken(), font));
  81.         }
  82.     }
  83. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now