Replacing images

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

31st May 2016
iText PDF

Switch code for this example

ReplaceImage.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/26580912/pdf-convert-to-black-and-white-pngs
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  17. import com.itextpdf.layout.element.Image;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import javax.imageio.ImageIO;
  24. import java.awt.image.BufferedImage;
  25. import java.io.ByteArrayOutputStream;
  26. import java.io.File;
  27. import java.io.IOException;
  28.  
  29. @Category(SampleTest.class)
  30. public class ReplaceImage extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/images/replace_image.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/image.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ReplaceImage().manipulatePdf(DEST);
  38.     }
  39.  
  40.     public static Image makeBlackAndWhitePng(PdfImageXObject image) throws IOException {
  41.         BufferedImage bi = image.getBufferedImage();
  42.         BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_USHORT_GRAY);
  43.         newBi.getGraphics().drawImage(bi, 0, 0, null);
  44.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  45.         ImageIO.write(newBi, "png", baos);
  46.         return new Image(ImageDataFactory.create(baos.toByteArray()));
  47.     }
  48.  
  49.     public static void replaceStream(PdfStream orig, PdfStream stream) throws IOException {
  50.         orig.clear();
  51.         orig.setData(stream.getBytes());
  52.         for (PdfName name : stream.keySet()) {
  53.             orig.put(name, stream.get(name));
  54.         }
  55.     }
  56.  
  57.     @Override
  58.     protected void manipulatePdf(String dest) throws Exception {
  59.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
  60.         PdfDictionary page = pdfDoc.getFirstPage().getPdfObject();
  61.         PdfDictionary resources = page.getAsDictionary(PdfName.Resources);
  62.         PdfDictionary xobjects = resources.getAsDictionary(PdfName.XObject);
  63.         PdfName imgRef = xobjects.keySet().iterator().next();
  64.         PdfStream stream = xobjects.getAsStream(imgRef);
  65.         Image img = makeBlackAndWhitePng(new PdfImageXObject(stream));
  66.         replaceStream(stream, img.getXObject().getPdfObject());
  67.         pdfDoc.close();
  68.     }
  69. }
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