Compress PDF Image JBIG2 - Java Sample Code

Sample 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. Capabilities include programatically creating new fields and widget annotations, form filling, modifying existing field values, form templating, and flattening form fields.

Learn more about our Server 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
6import com.pdftron.filters.Filter;
7import com.pdftron.filters.FilterReader;
8import com.pdftron.pdf.*;
9import com.pdftron.sdf.DictIterator;
10import com.pdftron.sdf.Obj;
11import com.pdftron.sdf.ObjSet;
12import com.pdftron.sdf.SDFDoc;
13
14// This sample project illustrates how to recompress bi-tonal images in an
15// existing PDF document using JBIG2 compression. The sample is not intended
16// to be a generic PDF optimization tool.
17//
18// You can download the entire document using the following link:
19// http://www.pdftron.com/net/samplecode/data/US061222892.pdf
20public class JBIG2Test {
21
22 public static void main(String[] args) {
23 PDFNet.initialize(PDFTronLicense.Key());
24
25 String input_path = "../../TestFiles/";
26 String output_path = "../../TestFiles/Output/";
27
28 try (PDFDoc pdf_doc = new PDFDoc(input_path + "US061222892-a.pdf")) {
29 pdf_doc.initSecurityHandler();
30
31 SDFDoc cos_doc = pdf_doc.getSDFDoc();
32 int num_objs = (int) cos_doc.xRefSize();
33 for (int i = 1; i < num_objs; ++i) {
34 Obj obj = cos_doc.getObj(i);
35 if (obj != null && !obj.isFree() && obj.isStream()) {
36 // Process only images
37 DictIterator itr = obj.find("Subtype");
38 if (!itr.hasNext() || !itr.value().getName().equals("Image"))
39 continue;
40
41 Image input_image = new Image(obj);
42 // Process only gray-scale images
43 if (input_image.getComponentNum() != 1)
44 continue;
45 int bpc = input_image.getBitsPerComponent();
46 if (bpc != 1) // Recompress only 1 BPC images
47 continue;
48
49 // Skip images that are already compressed using JBIG2
50 itr = obj.find("Filter");
51 if (itr.hasNext() && itr.value().isName() &&
52 !itr.value().getName().equals("JBIG2Decode")) continue;
53
54 Filter filter = obj.getDecodedStream();
55 FilterReader reader = new FilterReader(filter);
56
57 ObjSet hint_set = new ObjSet();
58 Obj hint = hint_set.createArray(); // A hint to image encoder to use JBIG2 compression
59 hint.pushBackName("JBIG2");
60 hint.pushBackName("Lossless");
61
62 Image new_image = Image.create(cos_doc, reader,
63 input_image.getImageWidth(),
64 input_image.getImageHeight(), 1, ColorSpace.createDeviceGray(), hint);
65
66 Obj new_img_obj = new_image.getSDFObj();
67 itr = obj.find("Decode");
68 if (itr.hasNext())
69 new_img_obj.put("Decode", itr.value());
70 itr = obj.find("ImageMask");
71 if (itr.hasNext())
72 new_img_obj.put("ImageMask", itr.value());
73 itr = obj.find("Mask");
74 if (itr.hasNext())
75 new_img_obj.put("Mask", itr.value());
76
77 cos_doc.swap(i, new_img_obj.getObjNum());
78 }
79 }
80
81 pdf_doc.save(output_path + "US061222892_JBIG2.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
82 } catch (Exception e) {
83 e.printStackTrace();
84 }
85
86 PDFNet.terminate();
87 }
88
89}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales