Images that use different filters

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

31st May 2016
admin-marketing

Switch code for this example

FlateCompressJPEG1Pass.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 sample is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
  11.  *
  12.  * The question was about adding compression to an image that already used /DCTDecode
  13.  *
  14.  * IMPORTANT:
  15.  * This sample uses kernel iText functionality that was written in answer to the question.
  16.  * This example will only work starting with iText 5.5.1
  17.  */
  18. package com.itextpdf.samples.sandbox.images;
  19.  
  20. import com.itextpdf.io.image.ImageDataFactory;
  21. import com.itextpdf.kernel.geom.PageSize;
  22. import com.itextpdf.kernel.pdf.CompressionConstants;
  23. import com.itextpdf.kernel.pdf.PdfDocument;
  24. import com.itextpdf.kernel.pdf.PdfWriter;
  25. import com.itextpdf.layout.Document;
  26. import com.itextpdf.layout.element.Image;
  27. import com.itextpdf.samples.GenericTest;
  28. import com.itextpdf.test.annotations.type.SampleTest;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. import java.io.File;
  33.  
  34.  
  35. @Category(SampleTest.class)
  36. public class FlateCompressJPEG1Pass extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/images/flate_compress_jpeg_1pass.pdf";
  38.     public static final String IMAGE = "./src/test/resources/img/berlin2013.jpg";
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new FlateCompressJPEG1Pass().manipulatePdf(DEST);
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  49.  
  50.         // Note that it is not necessary to create new PageSize object,
  51.         // but for testing reasons (connected to parallelization) we call constructor here
  52.         PageSize pageSize = new PageSize(PageSize.A4).rotate();
  53.  
  54.         Document doc = new Document(pdfDoc, pageSize);
  55.  
  56.         Image image = new Image(ImageDataFactory.create(IMAGE));
  57.         image.getXObject().getPdfObject().setCompressionLevel(CompressionConstants.BEST_COMPRESSION);
  58.         image.scaleToFit(PageSize.A4.rotate().getWidth(), pageSize.getHeight());
  59.         image.setFixedPosition(0, 0);
  60.  
  61.         doc.add(image);
  62.  
  63.         doc.close();
  64.     }
  65. }
FlateCompressJPEG2Passes.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 sample is written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
  11.  *
  12.  * The question was about adding compression to an image that already used /DCTDecode
  13.  */
  14. package com.itextpdf.samples.sandbox.images;
  15.  
  16. import com.itextpdf.kernel.pdf.*;
  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 java.io.File;
  23.  
  24.  
  25. @Category(SampleTest.class)
  26. public class FlateCompressJPEG2Passes extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/images/flate_compress_jpeg_2passes.pdf";
  28.     public static final String SRC = "./src/test/resources/pdfs/image.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new FlateCompressJPEG2Passes().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfReader reader = new PdfReader(SRC);
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  40.  
  41.         PdfDictionary pageDict = pdfDoc.getPage(1).getPdfObject();
  42.         PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
  43.         PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
  44.         PdfName imgName = pageXObjects.keySet().iterator().next();
  45.         PdfStream imgStream = pageXObjects.getAsStream(imgName);
  46.         imgStream.setData(reader.readStreamBytesRaw(imgStream));
  47.  
  48.         PdfArray array = new PdfArray();
  49.         array.add(PdfName.FlateDecode);
  50.         array.add(PdfName.DCTDecode);
  51.         imgStream.put(PdfName.Filter, array);
  52.  
  53.         pdfDoc.close();
  54.     }
  55. }
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