Images that use different filters

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

23rd October 2015
admin-marketing

Switch code for this example

FlateCompressJPEG1Pass.java
  1. /**
  2.  * This sample is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
  4.  *
  5.  * The question was about adding compression to an image that already used /DCTDecode
  6.  *
  7.  * IMPORTANT:
  8.  * This sample uses core iText functionality that was written in answer to the question.
  9.  * This example will only work starting with iText 5.5.1
  10.  */
  11. package sandbox.images;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Image;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.pdf.PdfStream;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class FlateCompressJPEG1Pass {
  28.     public static final String IMAGE = "resources/images/berlin2013.jpg";
  29.     public static final String DEST = "results/images/flatecompress_image1.pdf";
  30.    
  31.     public static void main(String[] args) throws DocumentException, IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new FlateCompressJPEG1Pass().createPdf(DEST);
  35.     }
  36.  
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document(PageSize.A4.rotate());
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         Image img = Image.getInstance(IMAGE);
  42.         img.setCompressionLevel(PdfStream.BEST_COMPRESSION);
  43.         img.scaleAbsolute(PageSize.A4.rotate());
  44.         img.setAbsolutePosition(0, 0);
  45.         document.add(img);
  46.         document.close();
  47.     }
  48. }
FlateCompressJPEG2Passes.java
  1. /**
  2.  * This sample is written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
  4.  *
  5.  * The question was about adding compression to an image that already used /DCTDecode
  6.  */
  7. package sandbox.images;
  8.  
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.pdf.PRStream;
  11. import com.itextpdf.text.pdf.PdfArray;
  12. import com.itextpdf.text.pdf.PdfDictionary;
  13. import com.itextpdf.text.pdf.PdfName;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20.  
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class FlateCompressJPEG2Passes {
  25.     public static final String SRC = "resources/pdfs/image.pdf";
  26.     public static final String DEST = "results/images/flatecompress_image2.pdf";
  27.    
  28.     public static void main(String[] args) throws DocumentException, IOException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new FlateCompressJPEG2Passes().manipulatePdf(SRC, DEST);
  32.     }
  33.    
  34.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  35.         PdfReader reader = new PdfReader(src);
  36.         // We assume that there's a single large picture on the first page
  37.         PdfDictionary page = reader.getPageN(1);
  38.         PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
  39.         PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
  40.         PdfName imgName = xobjects.getKeys().iterator().next();
  41.         PRStream imgStream = (PRStream)xobjects.getAsStream(imgName);
  42.         imgStream.setData(PdfReader.getStreamBytesRaw(imgStream), true);
  43.         PdfArray array = new PdfArray();
  44.         array.add(PdfName.FLATEDECODE);
  45.         array.add(PdfName.DCTDECODE);
  46.         imgStream.put(PdfName.FILTER, array);
  47.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  48.         stamper.close();
  49.         reader.close();
  50.     }
  51. }
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