Some test text!


Optimize & compress PDFs on iOS

To optimize a PDF with default settings.

PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
[PTOptimizer Optimize: doc settings: [[PTOptimizerSettings alloc] init]];

Compress & optimize PDF files
Full code sample which shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology.

linkAbout optimize and compress

Compression as a subset of optimization represents encoding specific data using fewer bits than the original content by reducing the size of the data. This is distinct from optimize (which modifies all images) because you have the ability to choose individual images and to selectively choose the compression type for each.

PDFTron SDK supports all basic and advanced compression filters allowed in PDF including:

  • JPEG2000
  • JBIG2
  • CCITT Fax
  • Flate/PNG
  • Crypt Filters

linkCompress images in a PDF document

To compress images using JBIG2 compression inside a PDF.

const pdf_doc = await PDFNet.PDFDoc.createFromURL(filename);

const cos_doc = await pdf_doc.getSDFDoc();
const num_objs = await cos_doc.xRefSize();
for (let i = 1; i < num_objs; ++i) {
    const obj = await cos_doc.getObj(i);
    if (obj && !(await obj.isFree()) && await obj.isStream()) {
        // Process only images
        var itr = await obj.find("Subtype");
        if (!(await itr.hasNext()) || await (await itr.value()).getName() !== "Image")
        const input_image = await PDFNet.Image.createFromObj(obj);
        // Process only gray-scale images
        if (await input_image.getComponentNum() != 1)
        if (await input_image.getBitsPerComponent() != 1) // Recompress only 1 BPC images

        // Skip images that are already compressed using JBIG2
        itr = await obj.find("Filter");
        if (await itr.hasNext()) {
            const value = await itr.value();
            if (await value.isName() && await value.getName() === "JBIG2Decode") continue;

        const filter = await obj.getDecodedStream();
        const reader = await PDFNet.FilterReader.create(filter);

        const hint_set = await PDFNet.ObjSet.create();
        const hint = await hint_set.createArray();


        const new_image = await PDFNet.Image.createFromStream(cos_doc, reader, await input_image.getImageWidth(),
            await input_image.getImageHeight(), 1, await PDFNet.ColorSpace.createDeviceGray(), hint);

        const new_img_obj = await new_image.getSDFObj();
        itr = await obj.find("Decode");
        if (await itr.hasNext())
            new_img_obj.put("Decode", await itr.value());
        itr = await obj.find("ImageMask");
        if (await itr.hasNext())
            new_img_obj.put("ImageMask", await itr.value());
        itr = await obj.find("Mask");
        if (await itr.hasNext())
            new_img_obj.put("Mask", await itr.value());

        await cos_doc.swap(i, await new_img_obj.getObjNum());
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTSDFDoc *cos_doc = [pdf_doc GetSDFDoc];
int num_objs = [cos_doc XRefSize];
for(int i=1; i<num_objs; ++i) 
    PTObj * obj = [cos_doc GetObj: i];
    if(!obj || [obj IsFree] || ![obj IsStream]) 
    // Process only images
    PTDictIterator *itr = [obj Find: @"Subtype"];
    if(![itr HasNext] || !([[[itr Value] GetName] isEqualToString:@"Image"]))
    PTImage *input_image = [[PTImage alloc] initWithImage_xobject: obj];

    // Process only gray-scale images
    if([input_image GetComponentNum] != 1)

    int bpc = [input_image GetBitsPerComponent];
    if(bpc != 1)    // Recompress only 1 BPC images

    // Skip images that are already compressed using JBIG2
    itr = [obj Find: @"Filter"];
    if ([itr HasNext] && [[itr Value] IsName] && [[[itr Value] GetName] isEqualToString:@"JBIG2Decode"]) 

    PTFilter *filter=[obj GetDecodedStream];
    PTFilterReader *reader = [[PTFilterReader alloc] initWithFilter: filter];

    PTObjSet *hint_set = [[PTObjSet alloc] init];   // A hint to image encoder to use JBIG2 compression
    PTObj * hint=[hint_set CreateArray];

    [hint PushBackName: @"JBIG2"];
    [hint PushBackName: @"Lossless"];

    PTImage *new_image = [PTImage CreateWithFilterData: cos_doc 
                                            image_data: reader 
                                                 width: [input_image GetImageWidth] 
                                                height: [input_image GetImageHeight] 
                                                   bpc: 1 
                                           color_space: [PTColorSpace CreateDeviceGray] 
                                         encoder_hints: hint];
    [cos_doc Swap: i obj_num2: [[pdf_doc GetSDFDoc] GetObjNum]];

PDF image JBIG2 compression
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.

Get the answers you need: Support


Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.