Replacing images

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

1st November 2015
admin-marketing

Switch code for this example

ReplaceImage.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/26580912/pdf-convert-to-black-and-white-pngs
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Image;
  9. import com.itextpdf.text.pdf.PRStream;
  10. import com.itextpdf.text.pdf.PdfDictionary;
  11. import com.itextpdf.text.pdf.PdfImage;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15. import com.itextpdf.text.pdf.PdfStream;
  16. import com.itextpdf.text.pdf.parser.PdfImageObject;
  17.  
  18. import java.awt.image.BufferedImage;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import javax.imageio.ImageIO;
  25. import sandbox.WrapToTest;
  26.  
  27. @WrapToTest
  28. public class ReplaceImage {
  29.    
  30.     public static final String SRC = "resources/pdfs/image.pdf";
  31.     public static final String DEST = "results/images/image_replaced.pdf";
  32.  
  33.     public static void main(String[] args) throws DocumentException, IOException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new ReplaceImage().manipulatePdf(SRC, DEST);
  37.     }
  38.  
  39.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  40.         PdfReader reader = new PdfReader(src);
  41.         PdfDictionary page = reader.getPageN(1);
  42.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  43.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  44.         PdfName imgRef = xobjects.getKeys().iterator().next();
  45.         PRStream stream = (PRStream) xobjects.getAsStream(imgRef);
  46.         Image img = makeBlackAndWhitePng(new PdfImageObject(stream));
  47.         PdfImage image = new PdfImage(makeBlackAndWhitePng(new PdfImageObject(stream)), "", null);
  48.         replaceStream(stream, image);
  49.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  50.         stamper.close();
  51.         reader.close();
  52.     }
  53.    
  54.     public static Image makeBlackAndWhitePng(PdfImageObject image) throws IOException, DocumentException {
  55.         BufferedImage bi = image.getBufferedImage();
  56.         BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_USHORT_GRAY);
  57.         newBi.getGraphics().drawImage(bi, 0, 0, null);
  58.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  59.         ImageIO.write(newBi, "png", baos);
  60.         return Image.getInstance(baos.toByteArray());
  61.     }
  62.    
  63.     public static void replaceStream(PRStream orig, PdfStream stream) throws IOException {
  64.         orig.clear();
  65.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  66.         stream.writeContent(baos);
  67.         orig.setData(baos.toByteArray(), false);
  68.         for (PdfName name : stream.getKeys()) {
  69.             orig.put(name, stream.get(name));
  70.         }
  71.     }
  72. }
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