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 file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * <p>
 * The question was about adding compression to an image that already used /DCTDecode
 * <p>
 * IMPORTANT:
 * This sample uses kernel iText functionality that was written in answer to the question.
 * This example will only work starting with iText 5.5.1
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.CompressionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

import java.io.File;

public class FlateCompressJPEG1Pass {
    public static final String DEST = "./target/sandbox/images/flate_compress_jpeg_1pass.pdf";

    public static final String IMAGE = "./src/main/resources/img/berlin2013.jpg";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new FlateCompressJPEG1Pass().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        PageSize pageSize = PageSize.A4.rotate();
        Document doc = new Document(pdfDoc, pageSize);

        Image image = new Image(ImageDataFactory.create(IMAGE));
        image.getXObject().getPdfObject().setCompressionLevel(CompressionConstants.BEST_COMPRESSION);
        image.scaleAbsolute(pageSize.getWidth(), pageSize.getHeight());
        image.setFixedPosition(0, 0);
        doc.add(image);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Images
{
    public class FlateCompressJPEG1Pass
    {
        public static readonly String DEST = "results/sandbox/images/flate_compress_jpeg_1pass.pdf";

        public static readonly String IMAGE = "../../../resources/img/berlin2013.jpg";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new FlateCompressJPEG1Pass().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            PageSize pageSize = PageSize.A4.Rotate();
            Document doc = new Document(pdfDoc, pageSize);

            Image image = new Image(ImageDataFactory.Create(IMAGE));
            image.GetXObject().GetPdfObject().SetCompressionLevel(CompressionConstants.BEST_COMPRESSION);
            image.ScaleAbsolute(pageSize.GetWidth(), pageSize.GetHeight());
            image.SetFixedPosition(0, 0);
            doc.Add(image);

            doc.Close();
        }
    }
}


flatecompressjpeg2passes

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * <p>
 * The question was about adding compression to an image that already used /DCTDecode
 */
package com.itextpdf.samples.sandbox.images;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfStream;
import com.itextpdf.kernel.pdf.PdfArray;

import java.io.File;

public class FlateCompressJPEG2Passes {
    public static final String DEST = "./target/sandbox/images/flate_compress_jpeg_2passes.pdf";

    public static final String SRC = "./src/main/resources/pdfs/image.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new FlateCompressJPEG2Passes().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfReader reader = new PdfReader(SRC);
        PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));

        // Assume that there is a single XObject in the source document
        // and this single object is an image.
        PdfDictionary pageDict = pdfDoc.getFirstPage().getPdfObject();
        PdfDictionary pageResources = pageDict.getAsDictionary(PdfName.Resources);
        PdfDictionary pageXObjects = pageResources.getAsDictionary(PdfName.XObject);
        PdfName imgName = pageXObjects.keySet().iterator().next();
        PdfStream imgStream = pageXObjects.getAsStream(imgName);
        imgStream.setData(reader.readStreamBytesRaw(imgStream));

        PdfArray array = new PdfArray();
        array.add(PdfName.FlateDecode);
        array.add(PdfName.DCTDecode);
        imgStream.put(PdfName.Filter, array);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using System.Linq;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Images
{
    public class FlateCompressJPEG2Passes
    {
        public static readonly String DEST = "results/sandbox/images/flate_compress_jpeg_2passes.pdf";

        public static readonly String SRC = "../../../resources/pdfs/image.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new FlateCompressJPEG2Passes().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfReader reader = new PdfReader(SRC);
            PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary pageDict = pdfDoc.GetFirstPage().GetPdfObject();
            PdfDictionary pageResources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary pageXObjects = pageResources.GetAsDictionary(PdfName.XObject);
            PdfName imgName = pageXObjects.KeySet().First();
            PdfStream imgStream = pageXObjects.GetAsStream(imgName);
            imgStream.SetData(reader.ReadStreamBytesRaw(imgStream));

            PdfArray array = new PdfArray();
            array.Add(PdfName.FlateDecode);
            array.Add(PdfName.DCTDecode);
            imgStream.Put(PdfName.Filter, array);

            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.