Page events for Watermarking

teaser

31st May 2016
iText PDF

Switch code for this example

Watermarking.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/21575630/adding-watermark-directly-to-the-stream
  11.  * <p>
  12.  * Adding a watermark to the document immediately using a page event.
  13.  */
  14. package com.itextpdf.samples.sandbox.events;
  15.  
  16. import com.itextpdf.io.font.FontConstants;
  17. import com.itextpdf.kernel.color.Color;
  18. import com.itextpdf.kernel.events.Event;
  19. import com.itextpdf.kernel.events.IEventHandler;
  20. import com.itextpdf.kernel.events.PdfDocumentEvent;
  21. import com.itextpdf.kernel.font.PdfFont;
  22. import com.itextpdf.kernel.font.PdfFontFactory;
  23. import com.itextpdf.kernel.pdf.PdfDocument;
  24. import com.itextpdf.kernel.pdf.PdfPage;
  25. import com.itextpdf.kernel.pdf.PdfWriter;
  26. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  27. import com.itextpdf.layout.Canvas;
  28. import com.itextpdf.layout.Document;
  29. import com.itextpdf.layout.element.Cell;
  30. import com.itextpdf.layout.element.Paragraph;
  31. import com.itextpdf.layout.element.Table;
  32. import com.itextpdf.layout.property.TextAlignment;
  33. import com.itextpdf.layout.property.VerticalAlignment;
  34. import com.itextpdf.samples.GenericTest;
  35. import com.itextpdf.test.annotations.type.SampleTest;
  36.  
  37. import org.junit.experimental.categories.Category;
  38.  
  39. import java.io.BufferedReader;
  40. import java.io.File;
  41. import java.io.FileReader;
  42. import java.io.IOException;
  43. import java.util.StringTokenizer;
  44.  
  45. @Category(SampleTest.class)
  46. public class Watermarking extends GenericTest {
  47.     public static final String DATA = "./src/test/resources/data/united_states.csv";
  48.     public static final String DEST = "./target/test/resources/sandbox/events/watermarkings.pdf";
  49.  
  50.     public static void main(String[] args) throws Exception {
  51.         File file = new File(DEST);
  52.         file.getParentFile().mkdirs();
  53.         new Watermarking().manipulatePdf(DEST);
  54.     }
  55.  
  56.     public void process(Table table, String line, PdfFont font, boolean isHeader) {
  57.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  58.         int c = 0;
  59.         while (tokenizer.hasMoreTokens() && c++ < 3) {
  60.             if (isHeader) {
  61.                 table.addHeaderCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  62.             } else {
  63.                 table.addCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  64.             }
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     protected void manipulatePdf(String dest) throws Exception {
  70.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  71.         Document doc = new Document(pdfDoc);
  72.         pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkingEventHandler());
  73.  
  74.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  75.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  76.  
  77.         Table table = new Table(new float[]{4, 1, 3});
  78.         table.setWidthPercent(100);
  79.  
  80.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  81.         String line = br.readLine();
  82.         process(table, line, bold, true);
  83.         while ((line = br.readLine()) != null) {
  84.             process(table, line, font, false);
  85.         }
  86.         br.close();
  87.  
  88.         doc.add(table);
  89.  
  90.         doc.close();
  91.     }
  92.  
  93.  
  94.     protected class WatermarkingEventHandler implements IEventHandler {
  95.         @Override
  96.         public void handleEvent(Event event) {
  97.             PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
  98.             PdfDocument pdfDoc = docEvent.getDocument();
  99.             PdfPage page = docEvent.getPage();
  100.             PdfFont font = null;
  101.             try {
  102.                 font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  103.             } catch (IOException e) {
  104.                 e.printStackTrace();
  105.             }
  106.             PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
  107.             new Canvas(canvas, pdfDoc, page.getPageSize())
  108.                     .setFontColor(Color.LIGHT_GRAY)
  109.                     .setFontSize(60)
  110.                     .setFont(font)
  111.                     .showTextAligned(new Paragraph("WATERMARK"), 298, 421, pdfDoc.getPageNumber(page),
  112.                             TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
  113.         }
  114.     }
  115. }
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