Watermark examples

31st May 2016
iText PDF

Switch code for this example

TransparentWatermark.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.  * This example was written by Bruno Lowagie in answer to a question by a customer.
  10.  */
  11. package com.itextpdf.samples.sandbox.stamper;
  12.  
  13. import com.itextpdf.io.font.FontConstants;
  14. import com.itextpdf.io.font.FontProgramFactory;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfResources;
  21. import com.itextpdf.kernel.pdf.PdfWriter;
  22. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  23. import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
  24. import com.itextpdf.layout.Canvas;
  25. import com.itextpdf.layout.element.Paragraph;
  26. import com.itextpdf.layout.property.TextAlignment;
  27. import com.itextpdf.layout.property.VerticalAlignment;
  28. import com.itextpdf.samples.GenericTest;
  29. import com.itextpdf.test.annotations.type.SampleTest;
  30.  
  31. import org.junit.experimental.categories.Category;
  32.  
  33. import java.io.File;
  34.  
  35.  
  36. @Category(SampleTest.class)
  37. public class TransparentWatermark extends GenericTest {
  38.     public static final String DEST = "./target/test/resources/sandbox/stamper/transparent_watermark.pdf";
  39.     public static final String SRC = "./src/test/resources/pdfs/hero.pdf";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new TransparentWatermark().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  50.         PdfCanvas under = new PdfCanvas(pdfDoc.getFirstPage().newContentStreamBefore(), new PdfResources(), pdfDoc);
  51.         PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA));
  52.         Paragraph p = new Paragraph("This watermark is added UNDER the existing content")
  53.                 .setFont(font).setFontSize(15);
  54.         new Canvas(under, pdfDoc, pdfDoc.getDefaultPageSize())
  55.                 .showTextAligned(p, 297, 550, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
  56.         PdfCanvas over = new PdfCanvas(pdfDoc.getFirstPage());
  57.         over.setFillColor(Color.BLACK);
  58.         p = new Paragraph("This watermark is added ON TOP OF the existing content")
  59.                 .setFont(font).setFontSize(15);
  60.         new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
  61.                 .showTextAligned(p, 297, 500, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
  62.         p = new Paragraph("This TRANSPARENT watermark is added ON TOP OF the existing content")
  63.                 .setFont(font).setFontSize(15);
  64.         over.saveState();
  65.         PdfExtGState gs1 = new PdfExtGState();
  66.         gs1.setFillOpacity(0.5f);
  67.         over.setExtGState(gs1);
  68.         new Canvas(over, pdfDoc, pdfDoc.getDefaultPageSize())
  69.                 .showTextAligned(p, 297, 450, 1, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
  70.         over.restoreState();
  71.         pdfDoc.close();
  72.     }
  73. }
TransparentWatermark2.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.  * This example was written by Bruno Lowagie in answer to a question by a customer.
  10.  */
  11. package com.itextpdf.samples.sandbox.stamper;
  12.  
  13. import com.itextpdf.io.font.FontConstants;
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfPage;
  21. import com.itextpdf.kernel.pdf.PdfReader;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
  25. import com.itextpdf.layout.Document;
  26. import com.itextpdf.layout.element.Paragraph;
  27. import com.itextpdf.layout.property.TextAlignment;
  28. import com.itextpdf.layout.property.VerticalAlignment;
  29. import com.itextpdf.samples.GenericTest;
  30. import com.itextpdf.test.annotations.type.SampleTest;
  31.  
  32. import org.junit.experimental.categories.Category;
  33.  
  34. import java.io.File;
  35.  
  36.  
  37. @Category(SampleTest.class)
  38. public class TransparentWatermark2 extends GenericTest {
  39.     public static final String DEST = "./target/test/resources/sandbox/stamper/transparent_watermark2.pdf";
  40.     public static final String IMG = "./src/test/resources/img/itext.png";
  41.     public static final String SRC = "./src/test/resources/pdfs/pages.pdf";
  42.  
  43.     public static void main(String[] args) throws Exception {
  44.         File file = new File(DEST);
  45.         file.getParentFile().mkdirs();
  46.         new TransparentWatermark2().manipulatePdf(DEST);
  47.     }
  48.  
  49.  
  50.     @Override
  51.     protected void manipulatePdf(String dest) throws Exception {
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  53.         Document doc = new Document(pdfDoc);
  54.         int n = pdfDoc.getNumberOfPages();
  55.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  56.         Paragraph p = new Paragraph("My watermark (text)").setFont(font).setFontSize(30);
  57.         // image watermark
  58.         ImageData img = ImageDataFactory.create(IMG);
  59.         //  Implement transformation matrix usage in order to scale image
  60.         float w = img.getWidth();
  61.         float h = img.getHeight();
  62.         // transparency
  63.         PdfExtGState gs1 = new PdfExtGState();
  64.         gs1.setFillOpacity(0.5f);
  65.         // properties
  66.         PdfCanvas over;
  67.         Rectangle pagesize;
  68.         float x, y;
  69.         // loop over every page
  70.         for (int i = 1; i
TransparentWatermark3.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.  * This example was written by Bruno Lowagie in answer to a question by a customer.
  10.  */
  11. package com.itextpdf.samples.sandbox.stamper;
  12.  
  13. import com.itextpdf.io.font.FontConstants;
  14. import com.itextpdf.io.font.FontProgramFactory;
  15. import com.itextpdf.io.image.ImageData;
  16. import com.itextpdf.io.image.ImageDataFactory;
  17. import com.itextpdf.kernel.font.PdfFont;
  18. import com.itextpdf.kernel.font.PdfFontFactory;
  19. import com.itextpdf.kernel.geom.Rectangle;
  20. import com.itextpdf.kernel.pdf.PdfDocument;
  21. import com.itextpdf.kernel.pdf.PdfReader;
  22. import com.itextpdf.kernel.pdf.PdfWriter;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
  25. import com.itextpdf.layout.Document;
  26. import com.itextpdf.layout.element.Paragraph;
  27. import com.itextpdf.layout.property.TextAlignment;
  28. import com.itextpdf.layout.property.VerticalAlignment;
  29. import com.itextpdf.samples.GenericTest;
  30. import com.itextpdf.test.annotations.type.SampleTest;
  31.  
  32. import org.junit.experimental.categories.Category;
  33.  
  34. import java.io.File;
  35.  
  36. @Category(SampleTest.class)
  37. public class TransparentWatermark3 extends GenericTest {
  38.     public static final String DEST = "./target/test/resources/sandbox/stamper/transparent_watermark3.pdf";
  39.     public static final String IMG = "./src/test/resources/img/itext.png";
  40.     public static final String SRC = "./src/test/resources/pdfs/pages.pdf";
  41.  
  42.     public static void main(String[] args) throws Exception {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new TransparentWatermark3().manipulatePdf(DEST);
  46.     }
  47.  
  48.     @Override
  49.     protected void manipulatePdf(String dest) throws Exception {
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  51.         Document doc = new Document(pdfDoc);
  52.         int n = pdfDoc.getNumberOfPages();
  53.         PdfFont font = PdfFontFactory.createFont(FontProgramFactory.createFont(FontConstants.HELVETICA));
  54.         Paragraph p = new Paragraph("My watermark (text)").setFont(font).setFontSize(30);
  55.         // image watermark
  56.         ImageData img = ImageDataFactory.create(IMG);
  57.         //  Implement transformation matrix usage in order to scale image
  58.         float w = img.getWidth();
  59.         float h = img.getHeight();
  60.         // transparency
  61.         PdfExtGState gs1 = new PdfExtGState();
  62.         gs1.setFillOpacity(0.5f);
  63.         // properties
  64.         PdfCanvas over;
  65.         Rectangle pagesize;
  66.         float x, y;
  67.         // loop over every page
  68.         for (int i = 1; i
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