Adding watermarks to images

31st May 2016
admin-marketing

Switch code for this example

WatermarkedImages1.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 code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/26814958/pdf-vertical-postion-method-gives-the-next-page-position-instead-of-current-page
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.color.DeviceGray;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.layout.Canvas;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Image;
  23. import com.itextpdf.layout.property.TextAlignment;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class WatermarkedImages1 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/images/watermarked_images1.pdf";
  34.     public static final String IMAGE1 = "./src/test/resources/img/bruno.jpg";
  35.     public static final String IMAGE2 = "./src/test/resources/img/dog.bmp";
  36.     public static final String IMAGE3 = "./src/test/resources/img/fox.bmp";
  37.     public static final String IMAGE4 = "./src/test/resources/img/bruno_ingeborg.jpg";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new WatermarkedImages1().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public Image getWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark) {
  46.         float width = img.getImageScaledWidth();
  47.         float height = img.getImageScaledHeight();
  48.         PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
  49.         new Canvas(template, pdfDoc).
  50.                 add(img).
  51.                 setFontColor(DeviceGray.WHITE).
  52.                 showTextAligned(watermark, width / 2, height / 2, TextAlignment.CENTER, (float) Math.PI / 6);
  53.         return new Image(template);
  54.     }
  55.  
  56.     @Override
  57.     protected void manipulatePdf(String dest) throws Exception {
  58.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  59.         Document doc = new Document(pdfDoc);
  60.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1)), "Bruno"));
  61.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE2)), "Dog"));
  62.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE3)), "Fox"));
  63.         Image image = new Image(ImageDataFactory.create(IMAGE4));
  64.         image.scaleToFit(400, 700);
  65.         doc.add(getWatermarkedImage(pdfDoc, image, "Bruno and Ingeborg"));
  66.         doc.close();
  67.     }
  68. }
WatermarkedImages2.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. /**
  10.  * This code sample was written by Bruno Lowagie in answer to this question:
  11.  * http://stackoverflow.com/questions/26814958/pdf-vertical-postion-method-gives-the-next-page-position-instead-of-current-page
  12.  */
  13. package com.itextpdf.samples.sandbox.images;
  14.  
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.color.DeviceRgb;
  17. import com.itextpdf.kernel.geom.Rectangle;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Canvas;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Image;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.element.Table;
  26. import com.itextpdf.layout.property.TextAlignment;
  27. import com.itextpdf.layout.property.VerticalAlignment;
  28. import com.itextpdf.layout.renderer.CellRenderer;
  29. import com.itextpdf.layout.renderer.DrawContext;
  30. import com.itextpdf.samples.GenericTest;
  31. import com.itextpdf.test.annotations.type.SampleTest;
  32.  
  33. import org.junit.experimental.categories.Category;
  34.  
  35. import java.io.File;
  36.  
  37. @Category(SampleTest.class)
  38. public class WatermarkedImages2 extends GenericTest {
  39.     public static final String DEST = "./target/test/resources/sandbox/images/watermarked_images2.pdf";
  40.     public static final String IMAGE1 = "./src/test/resources/img/bruno.jpg";
  41.     public static final String IMAGE2 = "./src/test/resources/img/dog.bmp";
  42.     public static final String IMAGE3 = "./src/test/resources/img/fox.bmp";
  43.     public static final String IMAGE4 = "./src/test/resources/img/bruno_ingeborg.jpg";
  44.  
  45.     public static void main(String[] args) throws Exception {
  46.         File file = new File(DEST);
  47.         file.getParentFile().mkdirs();
  48.         new WatermarkedImages2().manipulatePdf(DEST);
  49.     }
  50.  
  51.     @Override
  52.     protected void manipulatePdf(String dest) throws Exception {
  53.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  54.         Document doc = new Document(pdfDoc);
  55.  
  56.         Table table = new Table(1);
  57.         Cell cell;
  58.  
  59.         cell = new Cell().add(new Image(ImageDataFactory.create(IMAGE1)).setAutoScaleWidth(true));
  60.         cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Bruno"));
  61.         table.addCell(cell);
  62.  
  63.         cell = new Cell().add(new Image(ImageDataFactory.create(IMAGE2)).setAutoScaleWidth(true));
  64.         cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Dog"));
  65.         table.addCell(cell);
  66.  
  67.         cell = new Cell().add(new Image(ImageDataFactory.create(IMAGE3)).setAutoScaleWidth(true));
  68.         cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Fox"));
  69.         table.addCell(cell);
  70.  
  71.         cell = new Cell().add(new Image(ImageDataFactory.create(IMAGE4)).setAutoScaleWidth(true));
  72.         cell.setNextRenderer(new WatermarkedCellRenderer(cell, "Bruno and Ingeborg"));
  73.         table.addCell(cell);
  74.  
  75.         doc.add(table);
  76.         doc.close();
  77.     }
  78.  
  79.  
  80.     private class WatermarkedCellRenderer extends CellRenderer {
  81.         private String content;
  82.  
  83.         public WatermarkedCellRenderer(Cell modelElement, String content) {
  84.             super(modelElement);
  85.             this.content = content;
  86.         }
  87.  
  88.         @Override
  89.         public CellRenderer getNextRenderer() {
  90.             return new WatermarkedCellRenderer((Cell) modelElement, content);
  91.         }
  92.  
  93.         @Override
  94.         public void draw(DrawContext drawContext) {
  95.             super.draw(drawContext);
  96.             Paragraph p = new Paragraph(content).setFontColor(DeviceRgb.WHITE);
  97.             Rectangle rect = getOccupiedAreaBBox();
  98.             new Canvas(drawContext.getCanvas(), drawContext.getDocument(), getOccupiedAreaBBox())
  99.                     .showTextAligned(p, (rect.getLeft() + rect.getRight()) / 2, (rect.getBottom() + rect.getTop()) / 2,
  100.                             getOccupiedArea().getPageNumber(), TextAlignment.CENTER, VerticalAlignment.MIDDLE, (float) Math.PI / 6);
  101.         }
  102.     }
  103. }
WatermarkedImages3.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 code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/28515474/how-to-add-text-on-the-last-page-through-pdfcontentbyte
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.color.DeviceGray;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.layout.Canvas;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Cell;
  23. import com.itextpdf.layout.element.Image;
  24. import com.itextpdf.layout.element.Table;
  25. import com.itextpdf.layout.property.TextAlignment;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32.  
  33. @Category(SampleTest.class)
  34. public class WatermarkedImages3 extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/images/watermarked_images3.pdf";
  36.     public static final String IMAGE1 = "./src/test/resources/img/bruno.jpg";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new WatermarkedImages3().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public Image getWatermarkedImage(PdfDocument pdfDoc, Image img, String watermark) {
  45.         float width = img.getImageScaledWidth();
  46.         float height = img.getImageScaledHeight();
  47.         PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
  48.         new Canvas(template, pdfDoc).
  49.                 add(img).
  50.                 setFontColor(DeviceGray.WHITE).
  51.                 showTextAligned(watermark, width / 2, height / 2, TextAlignment.CENTER, (float) Math.PI * 30f / 180f);
  52.         return new Image(template);
  53.     }
  54.  
  55.     @Override
  56.     protected void manipulatePdf(String dest) throws Exception {
  57.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  58.         Document doc = new Document(pdfDoc);
  59.         Table table = new Table(1).setWidthPercent(80);
  60.         for (int i = 0; i < 35; i++) {
  61.             table.addCell(new Cell().add("rahlrokks doesn't listen to what people tell him"));
  62.         }
  63.         table.addCell(new Cell().add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1)), "Bruno").setAutoScale(true)));
  64.         doc.add(table);
  65.         doc.showTextAligned("Bruno knows best", 260, 400, TextAlignment.CENTER, 45f * (float) Math.PI / 180f);
  66.         doc.close();
  67.     }
  68. }
WatermarkedImages4.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 code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/29561417/draw-lines-on-image-in-pdf-using-itextsharp
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  21. import com.itextpdf.layout.Canvas;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Image;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class WatermarkedImages4 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/images/watermarked_images4.pdf";
  34.     public static final String IMAGE1 = "./src/test/resources/img/bruno.jpg";
  35.     public static final String IMAGE2 = "./src/test/resources/img/dog.bmp";
  36.     public static final String IMAGE3 = "./src/test/resources/img/fox.bmp";
  37.     public static final String IMAGE4 = "./src/test/resources/img/bruno_ingeborg.jpg";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new WatermarkedImages4().manipulatePdf(DEST);
  43.     }
  44.  
  45.     public Image getWatermarkedImage(PdfDocument pdfDocument, Image img) {
  46.         float width = img.getImageScaledWidth();
  47.         float height = img.getImageScaledHeight();
  48.         PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
  49.         new Canvas(template, pdfDocument).add(img);
  50.         new PdfCanvas(template, pdfDocument).
  51.                 saveState().
  52.                 setStrokeColor(Color.GREEN).
  53.                 setLineWidth(3).
  54.                 moveTo(width * .25f, height * .25f).
  55.                 lineTo(width * .75f, height * .75f).
  56.                 moveTo(width * .25f, height * .75f).
  57.                 lineTo(width * .25f, height * .25f).
  58.                 stroke().
  59.                 setStrokeColor(Color.WHITE).
  60.                 ellipse(0, 0, width, height).
  61.                 stroke().
  62.                 restoreState();
  63.         return new Image(template);
  64.     }
  65.  
  66.     @Override
  67.     protected void manipulatePdf(String dest) throws Exception {
  68.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  69.         Document doc = new Document(pdfDoc);
  70.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE1))));
  71.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE2))));
  72.         doc.add(getWatermarkedImage(pdfDoc, new Image(ImageDataFactory.create(IMAGE3))));
  73.         Image image = new Image(ImageDataFactory.create(IMAGE4));
  74.         image.scaleToFit(400, 700);
  75.         doc.add(getWatermarkedImage(pdfDoc, image));
  76.         doc.close();
  77.     }
  78. }
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