Image masks

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

31st May 2016
iText PDF

Switch code for this example

MakeJpgMask.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/29096314/how-to-make-an-image-a-qualified-mask-candidate-in-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.io.source.ByteArrayOutputStream;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Image;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import javax.imageio.ImageIO;
  28. import java.awt.image.BufferedImage;
  29. import java.io.File;
  30. import java.io.IOException;
  31.  
  32. @Category(SampleTest.class)
  33. public class MakeJpgMask extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/images/make_jpg_mask.pdf";
  35.     public static final String IMAGE = "./src/test/resources/img/javaone2013.jpg";
  36.     public static final String MASK = "./src/test/resources/img/berlin2013.jpg";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new MakeJpgMask().manipulatePdf(DEST);
  42.     }
  43.  
  44.     public static ImageData makeBlackAndWhitePng(String image) throws IOException {
  45.         BufferedImage bi = ImageIO.read(new File(image));
  46.         BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_USHORT_GRAY);
  47.         newBi.getGraphics().drawImage(bi, 0, 0, null);
  48.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49.         ImageIO.write(newBi, "png", baos);
  50.         return ImageDataFactory.create(baos.toByteArray());
  51.     }
  52.  
  53.     @Override
  54.     protected void manipulatePdf(String dest) throws Exception {
  55.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  56.         // Note that it is not necessary to create new PageSize object,
  57.         // but for testing reasons (connected to parallelization) we call constructor here
  58.         PageSize pageSize = new PageSize(PageSize.A4).rotate();
  59.         Document doc = new Document(pdfDoc, pageSize);
  60.  
  61.         ImageData image = ImageDataFactory.create(IMAGE);
  62.         ImageData mask = makeBlackAndWhitePng(MASK);
  63.         mask.makeMask();
  64.         image.setImageMask(mask);
  65.  
  66.         Image img = new Image(image);
  67.         img.scaleAbsolute(pageSize.getWidth(), pageSize.getHeight());
  68.         img.setFixedPosition(0, 0);
  69.         doc.add(img);
  70.  
  71.         doc.close();
  72.     }
  73. }
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