Optimize & compress PDFs on iOS

To optimize a PDF with default settings.

1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2[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.

About 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.

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

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

Compress images in a PDF document

To compress images using JBIG2 compression inside a PDF.

1PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
2PTSDFDoc *cos_doc = [pdf_doc GetSDFDoc];
3int num_objs = [cos_doc XRefSize];
4for(int i=1; i<num_objs; ++i)
5{
6 PTObj * obj = [cos_doc GetObj: i];
7 if(!obj || [obj IsFree] || ![obj IsStream])
8 continue;
9
10 // Process only images
11 PTDictIterator *itr = [obj Find: @"Subtype"];
12 if(![itr HasNext] || !([[[itr Value] GetName] isEqualToString:@"Image"]))
13 continue;
14
15 PTImage *input_image = [[PTImage alloc] initWithImage_xobject: obj];
16
17 // Process only gray-scale images
18 if([input_image GetComponentNum] != 1)
19 continue;
20
21 int bpc = [input_image GetBitsPerComponent];
22 if(bpc != 1) // Recompress only 1 BPC images
23 continue;
24
25 // Skip images that are already compressed using JBIG2
26 itr = [obj Find: @"Filter"];
27 if ([itr HasNext] && [[itr Value] IsName] && [[[itr Value] GetName] isEqualToString:@"JBIG2Decode"])
28 continue;
29
30 PTFilter *filter=[obj GetDecodedStream];
31 PTFilterReader *reader = [[PTFilterReader alloc] initWithFilter: filter];
32
33 PTObjSet *hint_set = [[PTObjSet alloc] init]; // A hint to image encoder to use JBIG2 compression
34 PTObj * hint=[hint_set CreateArray];
35
36 [hint PushBackName: @"JBIG2"];
37 [hint PushBackName: @"Lossless"];
38
39 PTImage *new_image = [PTImage CreateWithFilterData: cos_doc
40 image_data: reader
41 width: [input_image GetImageWidth]
42 height: [input_image GetImageHeight]
43 bpc: 1
44 color_space: [PTColorSpace CreateDeviceGray]
45 encoder_hints: hint];
46 [cos_doc Swap: i obj_num2: [[pdf_doc GetSDFDoc] GetObjNum]];
47}

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales