Skip to main content
Skip table of contents

Images that use different filters

Read more about these examples in the Q&A entry Click How to add JPEG images that are multi-stage filtered (DCTDecode and FlateDecode)?


flatecompressjpeg1pass

JAVA

JAVA
/**
 * This sample is written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
 * 
 * The question was about adding compression to an image that already used /DCTDecode
 * 
 * IMPORTANT:
 * This sample uses core iText functionality that was written in answer to the question.
 * This example will only work starting with iText 5.5.1 
 */
package sandbox.images;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class FlateCompressJPEG1Pass {
    public static final String IMAGE = "resources/images/berlin2013.jpg";
    public static final String DEST = "results/images/flatecompress_image1.pdf";
    
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FlateCompressJPEG1Pass().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        Image img = Image.getInstance(IMAGE);
        img.setCompressionLevel(PdfStream.BEST_COMPRESSION);
        img.scaleAbsolute(PageSize.A4.rotate());
        img.setAbsolutePosition(0, 0);
        document.add(img);
        document.close();
    }
}


flatecompressjpeg2passes

JAVA

JAVA
/**
 * This sample is written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/21958449/can-itextsharp-generate-pdf-with-jpeg-images-that-are-multi-stage-filtered-both
 * 
 * The question was about adding compression to an image that already used /DCTDecode
 */
package sandbox.images;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfArray;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class FlateCompressJPEG2Passes {
    public static final String SRC = "resources/pdfs/image.pdf";
    public static final String DEST = "results/images/flatecompress_image2.pdf";
    
    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FlateCompressJPEG2Passes().manipulatePdf(SRC, DEST);
    }
    
    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        // We assume that there's a single large picture on the first page
        PdfDictionary page = reader.getPageN(1);
        PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
        PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
        PdfName imgName = xobjects.getKeys().iterator().next();
        PRStream imgStream = (PRStream)xobjects.getAsStream(imgName);
        imgStream.setData(PdfReader.getStreamBytesRaw(imgStream), true);
        PdfArray array = new PdfArray();
        array.add(PdfName.FLATEDECODE);
        array.add(PdfName.DCTDECODE);
        imgStream.put(PdfName.FILTER, array);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        stamper.close();
        reader.close();
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.