Adding watermarks to images

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

23rd October 2015
admin-marketing

Switch code for this example

WatermarkedImages1.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/26814958/pdf-vertical-postion-method-gives-the-next-page-position-instead-of-current-page
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.GrayColor;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfTemplate;
  18. import com.itextpdf.text.pdf.PdfWriter;
  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 WatermarkedImages1 {
  27.     public static final String IMAGE1 = "resources/images/bruno.jpg";
  28.     public static final String IMAGE2 = "resources/images/dog.bmp";
  29.     public static final String IMAGE3 = "resources/images/fox.bmp";
  30.     public static final String IMAGE4 = "resources/images/bruno_ingeborg.jpg";
  31.     public static final Font FONT = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
  32.     public static final String DEST = "results/images/watermark_template.pdf";
  33.    
  34.     public static void main(String[] args) throws IOException, DocumentException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new WatermarkedImages1().createPdf(DEST);
  38.     }
  39.    
  40.     public void createPdf(String dest) throws IOException, DocumentException {
  41.         Document document = new Document();
  42.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  43.         document.open();
  44.         PdfContentByte cb = writer.getDirectContentUnder();
  45.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1), "Bruno"));
  46.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE2), "Dog"));
  47.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE3), "Fox"));
  48.         Image img = Image.getInstance(IMAGE4);
  49.         img.scaleToFit(400, 700);
  50.         document.add(getWatermarkedImage(cb, img, "Bruno and Ingeborg"));
  51.         document.close();
  52.     }
  53.    
  54.     public Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException {
  55.         float width = img.getScaledWidth();
  56.         float height = img.getScaledHeight();
  57.         PdfTemplate template = cb.createTemplate(width, height);
  58.         template.addImage(img, width, 0, 0, height, 0, 0);
  59.         ColumnText.showTextAligned(template, Element.ALIGN_CENTER,
  60.                 new Phrase(watermark, FONT), width / 2, height / 2, 30);
  61.         return Image.getInstance(template);
  62.     }
  63. }
WatermarkedImages2.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/26814958/pdf-vertical-postion-method-gives-the-next-page-position-instead-of-current-page
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.ColumnText;
  16. import com.itextpdf.text.pdf.GrayColor;
  17. import com.itextpdf.text.pdf.PdfContentByte;
  18. import com.itextpdf.text.pdf.PdfPCell;
  19. import com.itextpdf.text.pdf.PdfPCellEvent;
  20. import com.itextpdf.text.pdf.PdfPTable;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import sandbox.WrapToTest;
  27.  
  28. @WrapToTest
  29. public class WatermarkedImages2 {
  30.     public static final String IMAGE1 = "resources/images/bruno.jpg";
  31.     public static final String IMAGE2 = "resources/images/dog.bmp";
  32.     public static final String IMAGE3 = "resources/images/fox.bmp";
  33.     public static final String IMAGE4 = "resources/images/bruno_ingeborg.jpg";
  34.     public static final Font FONT = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
  35.     public static final String DEST = "results/images/watermark_table.pdf";
  36.    
  37.     public static void main(String[] args) throws IOException, DocumentException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new WatermarkedImages2().createPdf(DEST);
  41.     }
  42.    
  43.     public void createPdf(String dest) throws IOException, DocumentException {
  44.         Document document = new Document();
  45.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  46.         document.open();
  47.         PdfPTable table = new PdfPTable(1);
  48.         table.setWidthPercentage(100);
  49.         PdfPCell cell;
  50.         cell = new PdfPCell(Image.getInstance(IMAGE1), true);
  51.         cell.setCellEvent(new WatermarkedCell("Bruno"));
  52.         table.addCell(cell);
  53.         cell = new PdfPCell(Image.getInstance(IMAGE2), true);
  54.         cell.setCellEvent(new WatermarkedCell("Dog"));
  55.         table.addCell(cell);
  56.         cell = new PdfPCell(Image.getInstance(IMAGE3), true);
  57.         cell.setCellEvent(new WatermarkedCell("Fox"));
  58.         table.addCell(cell);
  59.         cell = new PdfPCell(Image.getInstance(IMAGE4), true);
  60.         cell.setCellEvent(new WatermarkedCell("Bruno and Ingeborg"));
  61.         table.addCell(cell);
  62.         document.add(table);
  63.         document.close();
  64.     }
  65.    
  66.     class WatermarkedCell implements PdfPCellEvent {
  67.         String watermark;
  68.        
  69.         public WatermarkedCell(String watermark) {
  70.             this.watermark = watermark;
  71.         }
  72.        
  73.         public void cellLayout(PdfPCell cell, Rectangle position,
  74.             PdfContentByte[] canvases) {
  75.             PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
  76.             ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
  77.                 new Phrase(watermark, FONT),
  78.                 (position.getLeft() + position.getRight()) / 2,
  79.                 (position.getBottom() + position.getTop()) / 2, 30);
  80.         }
  81.     }
  82. }
WatermarkedImages3.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/28515474/how-to-add-text-on-the-last-page-through-pdfcontentbyte
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.ColumnText;
  15. import com.itextpdf.text.pdf.GrayColor;
  16. import com.itextpdf.text.pdf.PdfContentByte;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfTemplate;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class WatermarkedImages3 {
  28.     public static final String IMAGE1 = "resources/images/bruno.jpg";
  29.     public static final Font FONT = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, GrayColor.GRAYWHITE);
  30.     public static final String DEST = "results/images/watermark3.pdf";
  31.    
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new WatermarkedImages3().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Document document = new Document();
  40.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.         document.open();
  42.         PdfPTable table = new PdfPTable(1);
  43.         for (int i = 0; i < 50; i++) {
  44.             table.addCell("rahlrokks doesn't listen to what people tell him");
  45.         }
  46.         PdfContentByte cb = writer.getDirectContentUnder();
  47.         table.addCell(getWatermarkedImage(cb, Image.getInstance(IMAGE1), "Bruno"));
  48.         document.add(table);
  49.         ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("Bruno knows best"), 260, 400, 45);
  50.         document.close();
  51.     }
  52.    
  53.     public Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException {
  54.         float width = img.getScaledWidth();
  55.         float height = img.getScaledHeight();
  56.         PdfTemplate template = cb.createTemplate(width, height);
  57.         template.addImage(img, width, 0, 0, height, 0, 0);
  58.         ColumnText.showTextAligned(template, Element.ALIGN_CENTER,
  59.                 new Phrase(watermark, FONT), width / 2, height / 2, 30);
  60.         return Image.getInstance(template);
  61.     }
  62. }
WatermarkedImages4.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/29561417/draw-lines-on-image-in-pdf-using-itextsharp
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfTemplate;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class WatermarkedImages4 {
  22.     public static final String IMAGE1 = "resources/images/bruno.jpg";
  23.     public static final String IMAGE2 = "resources/images/dog.bmp";
  24.     public static final String IMAGE3 = "resources/images/fox.bmp";
  25.     public static final String IMAGE4 = "resources/images/bruno_ingeborg.jpg";
  26.     public static final String DEST = "results/images/watermark_image4.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new WatermarkedImages4().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfContentByte cb = writer.getDirectContentUnder();
  39.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));
  40.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE2)));
  41.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE3)));
  42.         Image img = Image.getInstance(IMAGE4);
  43.         img.scaleToFit(400, 700);
  44.         document.add(getWatermarkedImage(cb, img));
  45.         document.close();
  46.     }
  47.    
  48.     public Image getWatermarkedImage(PdfContentByte cb, Image img) throws DocumentException {
  49.         float width = img.getScaledWidth();
  50.         float height = img.getScaledHeight();
  51.         PdfTemplate template = cb.createTemplate(width, height);
  52.         template.addImage(img, width, 0, 0, height, 0, 0);
  53.         template.saveState();
  54.         template.setColorStroke(BaseColor.GREEN);
  55.         template.setLineWidth(3);
  56.         template.moveTo(width * .25f, height * .25f);
  57.         template.lineTo(width * .75f, height * .75f);
  58.         template.moveTo(width * .25f, height * .75f);
  59.         template.lineTo(width * .25f, height * .25f);
  60.         template.stroke();
  61.         template.setColorStroke(BaseColor.WHITE);
  62.         template.ellipse(0, 0, width, height);
  63.         template.stroke();
  64.         template.restoreState();
  65.         return Image.getInstance(template);
  66.     }
  67. }
WatermarkedImages5.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/38027783
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfTemplate;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class WatermarkedImages5 {
  23.     public static final String IMAGE1 = "resources/images/bruno.jpg";
  24.     public static final String IMAGE2 = "resources/images/dog.bmp";
  25.     public static final String IMAGE3 = "resources/images/fox.bmp";
  26.     public static final String IMAGE4 = "resources/images/bruno_ingeborg.jpg";
  27.     public static final String DEST = "results/images/watermark_image5.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new WatermarkedImages5().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         PdfContentByte cb = writer.getDirectContentUnder();
  40.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1)));
  41.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE2)));
  42.         document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE3)));
  43.         Image img = Image.getInstance(IMAGE4);
  44.         img.scaleToFit(400, 700);
  45.         document.add(getWatermarkedImage(cb, img));
  46.         document.close();
  47.     }
  48.    
  49.     public Image getWatermarkedImage(PdfContentByte cb, Image img) throws DocumentException {
  50.         float width = img.getScaledWidth();
  51.         float height = img.getScaledHeight();
  52.         PdfTemplate template = cb.createTemplate(width, height);
  53.         template.addImage(img, width, 0, 0, height, 0, 0);
  54.         PdfPTable table = new PdfPTable(2);
  55.         table.setTotalWidth(width);
  56.         table.getDefaultCell().setBorderColor(BaseColor.YELLOW);
  57.         table.addCell("Test1");
  58.         table.addCell("Test2");
  59.         table.addCell("Test3");
  60.         table.addCell("Test4");
  61.         table.writeSelectedRows(0, -1, 0, height, template);
  62.         return Image.getInstance(template);
  63.     }
  64. }
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