Sample Obj-C code for using Apryse SDK to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression (lossless or lossy). The sample is intended to show how to specify hint information for the image encoder and is not meant to be a generic PDF optimization tool. To demonstrate the possible compression rates, we recompressed a document containing 17 scanned pages. The original input document is ~1.4MB and is using standard CCITT Fax compression. Lossless JBIG2 compression shrunk the filesize to 641KB, while lossy JBIG2 compression shrunk it to 176KB. Learn more about our iOS SDK.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9// This sample project illustrates how to recompress bi-tonal images in an
10// existing PDF document using JBIG2 compression. The sample is not intended
11// to be a generic PDF optimization tool.
12//
13// You can download the entire document using the following link:
14// http://www.pdftron.com/net/samplecode/data/US061222892.pdf
15//
16int main(int argc, char *argv[])
17{
18 @autoreleasepool {
19
20 [PTPDFNet Initialize: 0];
21
22 @try
23 {
24 PTPDFDoc *pdf_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/US061222892-a.pdf"];
25 [pdf_doc InitSecurityHandler];
26
27 PTSDFDoc *cos_doc = [pdf_doc GetSDFDoc];
28 int num_objs = [cos_doc XRefSize];
29 int i=1;
30 for(; i<num_objs; ++i)
31 {
32 PTObj * obj = [cos_doc GetObj: i];
33 if(obj && ![obj IsFree] && [obj IsStream])
34 {
35 // Process only images
36 PTDictIterator *itr = [obj Find: @"Subtype"];
37 if(![itr HasNext] || !([[[itr Value] GetName] isEqualToString:@"Image"]))
38 continue;
39
40 PTImage *input_image = [[PTImage alloc] initWithImage_xobject: obj];
41 // Process only gray-scale images
42 if([input_image GetComponentNum] != 1)
43 continue;
44 int bpc = [input_image GetBitsPerComponent];
45 if(bpc != 1) // Recompress only 1 BPC images
46 continue;
47
48 // Skip images that are already compressed using JBIG2
49 itr = [obj Find: @"Filter"];
50 if ([itr HasNext] && [[itr Value] IsName] &&
51 [[[itr Value] GetName] isEqualToString:@"JBIG2Decode"]) continue;
52
53 PTFilter *filter=[obj GetDecodedStream];
54 PTFilterReader *reader = [[PTFilterReader alloc] initWithFilter: filter];
55
56
57 PTObjSet *hint_set = [[PTObjSet alloc] init]; // A hint to image encoder to use JBIG2 compression
58 PTObj * hint=[hint_set CreateArray];
59
60 [hint PushBackName: @"JBIG2"];
61 [hint PushBackName: @"Lossless"];
62
63 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];
64
65 PTObj * new_img_obj = [new_image GetSDFObj];
66 itr = [obj Find: @"Decode"];
67 if([itr HasNext])
68 [new_img_obj Put: @"Decode" obj: [itr Value]];
69 itr = [obj Find: @"ImageMask"];
70 if ([itr HasNext])
71 [new_img_obj Put: @"ImageMask" obj: [itr Value]];
72 itr = [obj Find: @"Mask"];
73 if ([itr HasNext])
74 [new_img_obj Put: @"Mask" obj: [itr Value]];
75
76 [cos_doc Swap: i obj_num2: [new_img_obj GetObjNum]];
77 }
78 }
79
80 [pdf_doc SaveToFile: @"../../TestFiles/Output/US061222892_JBIG2.pdf" flags: e_ptremove_unused];
81 }
82 @catch(NSException *e)
83 {
84 NSLog(@"%@", e.reason);
85 NSLog(@"Please make sure that the pathname to the test file is correct.");
86 }
87
88 NSLog(@"Done.");
89 [PTPDFNet Terminate: 0];
90 return 0;
91 }
92}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9// This sample project illustrates how to recompress bi-tonal images in an
10// existing PDF document using JBIG2 compression. The sample is not intended
11// to be a generic PDF optimization tool.
12//
13// You can download the entire document using the following link:
14// http://www.pdftron.com/net/samplecode/data/US061222892.pdf
15//
16func runJBIG2Test() -> Int {
17 return autoreleasepool {
18
19
20 do {
21 try PTPDFNet.catchException {
22 let pdf_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "US061222892-a", ofType: "pdf"))
23 pdf_doc.initSecurityHandler()
24
25 let cos_doc: PTSDFDoc = pdf_doc.getSDFDoc()
26 let num_objs = cos_doc.xRefSize()
27 for i in 1..<num_objs {
28 guard let obj: PTObj = cos_doc.getObj(UInt32(i)) else {
29 continue
30 }
31 if !obj.isFree() && obj.isStream() {
32 // Process only images
33 var itr: PTDictIterator = obj.find("Subtype")
34 if !itr.hasNext() || !(itr.value().getName() == "Image") {
35 continue
36 }
37
38 let input_image: PTImage = PTImage(image_xobject: obj)
39 // Process only gray-scale images
40 if input_image.getComponentNum() != 1 {
41 continue
42 }
43 let bpc: Int32 = input_image.getBitsPerComponent()
44 if bpc != 1 {
45 // Recompress only 1 BPC images
46 continue
47 }
48
49 // Skip images that are already compressed using JBIG2
50 itr = obj.find("Filter")
51 if itr.hasNext() && itr.value().isName() && (itr.value().getName() == "JBIG2Decode") {
52 continue
53 }
54
55 let filter: PTFilter = obj.getDecodedStream()
56 let reader = PTFilterReader(filter: filter)
57
58 let hint_set: PTObjSet = PTObjSet() // A hint to image encoder to use JBIG2 compression
59 let hint: PTObj = hint_set.createArray()
60
61 hint.pushBackName("JBIG2")
62 hint.pushBackName("Lossless")
63
64 let new_image: PTImage = PTImage.create(withFilterData: cos_doc, image_data: reader, width: input_image.getWidth(), height: input_image.getHeight(), bpc: 1, color_space: PTColorSpace.createDeviceGray(), encoder_hints: hint)
65
66 let new_img_obj: PTObj = new_image.getSDFObj()
67 itr = obj.find("Decode")
68 if itr.hasNext() {
69 new_img_obj.put("Decode", obj: itr.value())
70 }
71 itr = obj.find("ImageMask")
72 if itr.hasNext() {
73 new_img_obj.put("ImageMask", obj: itr.value())
74 }
75 itr = obj.find("Mask")
76 if itr.hasNext() {
77 new_img_obj.put("Mask", obj: itr.value())
78 }
79
80 cos_doc.swap(UInt32(i), obj_num2: new_img_obj.getNum())
81 }
82 }
83
84 pdf_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("US061222892_JBIG2.pdf").path, flags: e_ptremove_unused.rawValue)
85 }
86 } catch let e as NSError {
87 print("\(e)")
88 print("Please make sure that the pathname to the test file is correct.")
89 }
90
91 print("Done.")
92
93 return 0
94 }
95}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales