Reduce image

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

31st May 2016
iText PDF

Switch code for this example

ReduceSize.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 example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/30483622/compressing-images-in-existing-pdfs-makes-the-resulting-pdf-file-bigger-lowagie
  11.  */
  12. package com.itextpdf.samples.sandbox.images;
  13.  
  14. import com.itextpdf.io.source.ByteArrayOutputStream;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.xobject.PdfImageXObject;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import javax.imageio.ImageIO;
  23. import java.awt.*;
  24. import java.awt.geom.AffineTransform;
  25. import java.awt.image.BufferedImage;
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class ReduceSize extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/images/reduce_size.pdf";
  31.     public static final String SRC = "./src/test/resources/pdfs/single_image.pdf";
  32.     public static final float FACTOR = 0.5f;
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ReduceSize().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfWriter writer = new PdfWriter(DEST, new WriterProperties().setFullCompressionMode(true));
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), writer);
  44.         PdfObject object;
  45.         PdfStream stream;
  46.         for (PdfIndirectReference indRef : pdfDoc.listIndirectReferences()) {
  47.             object = indRef.getRefersTo();
  48.             if (object == null || !object.isStream()) {
  49.                 continue;
  50.             }
  51.             stream = (PdfStream) object;
  52.             if (!PdfName.Image.equals(stream.getAsName(PdfName.Subtype))) {
  53.                 continue;
  54.             }
  55.             if (!PdfName.DCTDecode.equals(stream.getAsName(PdfName.Filter))) {
  56.                 continue;
  57.             }
  58.             PdfImageXObject image = new PdfImageXObject(stream);
  59.             BufferedImage bi = image.getBufferedImage();
  60.             if (bi == null) {
  61.                 continue;
  62.             }
  63.             int width = (int) (bi.getWidth() * FACTOR);
  64.             int height = (int) (bi.getHeight() * FACTOR);
  65.             if (width <= 0 || height <= 0) {
  66.                 continue;
  67.             }
  68.             BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  69.             AffineTransform at = AffineTransform.getScaleInstance(FACTOR, FACTOR);
  70.             Graphics2D g = img.createGraphics();
  71.             g.drawRenderedImage(bi, at);
  72.             ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
  73.             ImageIO.write(img, "JPG", imgBytes);
  74.             stream.clear();
  75.             stream.setData(imgBytes.toByteArray());
  76.             stream.put(PdfName.Type, PdfName.XObject);
  77.             stream.put(PdfName.Subtype, PdfName.Image);
  78.             stream.put(PdfName.Filter, PdfName.DCTDecode);
  79.             stream.put(PdfName.Width, new PdfNumber(width));
  80.             stream.put(PdfName.Height, new PdfNumber(height));
  81.             stream.put(PdfName.BitsPerComponent, new PdfNumber(8));
  82.             stream.put(PdfName.ColorSpace, PdfName.DeviceRGB);
  83.         }
  84.         pdfDoc.close();
  85.     }
  86. }
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