Watermark examples

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

5th November 2015
admin-marketing

Switch code for this example

TransparentWatermark.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to a question by a customer.
  3.  */
  4. package sandbox.stamper;
  5.  
  6. import com.itextpdf.text.DocumentException;
  7. import com.itextpdf.text.Element;
  8. import com.itextpdf.text.Font;
  9. import com.itextpdf.text.Font.FontFamily;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfGState;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class TransparentWatermark {
  24.  
  25.     public static final String SRC = "resources/pdfs/hero.pdf";
  26.     public static final String DEST = "results/stamper/hero_watermarked.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new TransparentWatermark().manipulatePdf(SRC, DEST);
  32.     }
  33.  
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         PdfContentByte under = stamper.getUnderContent(1);
  38.         Font f = new Font(FontFamily.HELVETICA, 15);
  39.         Phrase p = new Phrase("This watermark is added UNDER the existing content", f);
  40.         ColumnText.showTextAligned(under, Element.ALIGN_CENTER, p, 297, 550, 0);
  41.         PdfContentByte over = stamper.getOverContent(1);
  42.         p = new Phrase("This watermark is added ON TOP OF the existing content", f);
  43.         ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 500, 0);
  44.         p = new Phrase("This TRANSPARENT watermark is added ON TOP OF the existing content", f);
  45.         over.saveState();
  46.         PdfGState gs1 = new PdfGState();
  47.         gs1.setFillOpacity(0.5f);
  48.         over.setGState(gs1);
  49.         ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, 297, 450, 0);
  50.         over.restoreState();
  51.         stamper.close();
  52.         reader.close();
  53.     }
  54. }
TransparentWatermark2.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following StackOverflow question:
  3.  * http://stackoverflow.com/questions/29560373/watermark-pdfs-using-text-or-images-in-java
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfGState;
  17. import com.itextpdf.text.pdf.PdfReader;
  18. import com.itextpdf.text.pdf.PdfStamper;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class TransparentWatermark2 {
  27.  
  28.     public static final String SRC = "resources/pdfs/pages.pdf";
  29.     public static final String DEST = "results/stamper/pages_watermarked.pdf";
  30.     public static final String IMG = "resources/images/itext.png";
  31.  
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new TransparentWatermark2().manipulatePdf(SRC, DEST);
  36.     }
  37.  
  38.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  39.         PdfReader reader = new PdfReader(src);
  40.         int n = reader.getNumberOfPages();
  41.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  42.         // text watermark
  43.         Font f = new Font(FontFamily.HELVETICA, 30);
  44.         Phrase p = new Phrase("My watermark (text)", f);
  45.         // image watermark
  46.         Image img = Image.getInstance(IMG);
  47.         float w = img.getScaledWidth();
  48.         float h = img.getScaledHeight();
  49.         // transparency
  50.         PdfGState gs1 = new PdfGState();
  51.         gs1.setFillOpacity(0.5f);
  52.         // properties
  53.         PdfContentByte over;
  54.         Rectangle pagesize;
  55.         float x, y;
  56.         // loop over every page
  57.         for (int i = 1; i
TransparentWatermark3.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following StackOverflow question:
  3.  * http://stackoverflow.com/questions/29560373/watermark-pdfs-using-text-or-images-in-java
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.Phrase;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfGState;
  17. import com.itextpdf.text.pdf.PdfReader;
  18. import com.itextpdf.text.pdf.PdfStamper;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import sandbox.WrapToTest;
  24.  
  25. @WrapToTest
  26. public class TransparentWatermark3 {
  27.  
  28.     public static final String SRC = "resources/pdfs/pages.pdf";
  29.     public static final String DEST = "results/stamper/pages_watermarked3.pdf";
  30.     public static final String IMG = "resources/images/itext.png";
  31.  
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new TransparentWatermark3().manipulatePdf(SRC, DEST);
  36.     }
  37.  
  38.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  39.         PdfReader reader = new PdfReader(src);
  40.         int n = reader.getNumberOfPages();
  41.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  42.         stamper.setRotateContents(false);
  43.         // text watermark
  44.         Font f = new Font(FontFamily.HELVETICA, 30);
  45.         Phrase p = new Phrase("My watermark (text)", f);
  46.         // image watermark
  47.         Image img = Image.getInstance(IMG);
  48.         float w = img.getScaledWidth();
  49.         float h = img.getScaledHeight();
  50.         // transparency
  51.         PdfGState gs1 = new PdfGState();
  52.         gs1.setFillOpacity(0.5f);
  53.         // properties
  54.         PdfContentByte over;
  55.         Rectangle pagesize;
  56.         float x, y;
  57.         // loop over every page
  58.         for (int i = 1; i
WatermarkToTheSide.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/36511649
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.ColumnText;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class WatermarkToTheSide {
  25.  
  26.     public static final String SRC = "resources/pdfs/pages.pdf";
  27.     public static final String DEST = "results/stamper/side_watermark.pdf";
  28.  
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new WatermarkToTheSide().manipulatePdf(SRC, DEST);
  33.     }
  34.    
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         int n = reader.getNumberOfPages();
  39.         PdfContentByte canvas;
  40.         Rectangle pageSize;
  41.         float x, y;
  42.         for (int p = 1; p
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