Reduce image

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

7th November 2015
admin-marketing

Switch code for this example

ReduceSize.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/30483622/compressing-images-in-existing-pdfs-makes-the-resulting-pdf-file-bigger-lowagie
  4.  */
  5. package sandbox.images;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PRStream;
  9. import com.itextpdf.text.pdf.PdfName;
  10. import com.itextpdf.text.pdf.PdfNumber;
  11. import com.itextpdf.text.pdf.PdfObject;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14. import com.itextpdf.text.pdf.parser.PdfImageObject;
  15.  
  16. import java.awt.Graphics2D;
  17. import java.awt.geom.AffineTransform;
  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. /**
  28.  * @author Bruno Lowagie (iText Software)
  29.  */
  30. @WrapToTest
  31. public class ReduceSize {
  32.    
  33.     public static final String SRC = "resources/pdfs/single_image.pdf";
  34.     public static final String DEST = "results/images/single_image_reduced.pdf";
  35.     public static final float FACTOR = 0.5f;
  36.    
  37.     public static void main(String[] args) throws DocumentException, IOException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ReduceSize().manipulatePdf(SRC, DEST);
  41.     }
  42.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  43.         PdfReader reader = new PdfReader(src);
  44.         int n = reader.getXrefSize();
  45.         PdfObject object;
  46.         PRStream stream;
  47.         // Look for image and manipulate image stream
  48.         for (int i = 0; i < n; i++) {
  49.             object = reader.getPdfObject(i);
  50.             if (object == null || !object.isStream())
  51.                 continue;
  52.             stream = (PRStream)object;
  53.             if (!PdfName.IMAGE.equals(stream.getAsName(PdfName.SUBTYPE)))
  54.                 continue;
  55.             if (!PdfName.DCTDECODE.equals(stream.getAsName(PdfName.FILTER)))
  56.                 continue;
  57.             PdfImageObject image = new PdfImageObject(stream);
  58.             BufferedImage bi = image.getBufferedImage();
  59.             if (bi == null)
  60.                 continue;
  61.             int width = (int)(bi.getWidth() * FACTOR);
  62.             int height = (int)(bi.getHeight() * FACTOR);
  63.             if (width
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