JBIGTest

Sample Java 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 Android SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.filters.Filter;
13import com.pdftron.filters.FilterReader;
14import com.pdftron.pdf.ColorSpace;
15import com.pdftron.pdf.Image;
16import com.pdftron.pdf.PDFDoc;
17import com.pdftron.sdf.DictIterator;
18import com.pdftron.sdf.Obj;
19import com.pdftron.sdf.ObjSet;
20import com.pdftron.sdf.SDFDoc;
21
22import java.util.ArrayList;
23
24//This sample project illustrates how to recompress bi-tonal images in an
25//existing PDF document using JBIG2 compression. The sample is not intended
26//to be a generic PDF optimization tool.
27
28public class JBIG2Test extends PDFNetSample {
29
30 private static OutputListener mOutputListener;
31
32 private static ArrayList<String> mFileList = new ArrayList<>();
33
34 public JBIG2Test() {
35 setTitle(R.string.sample_jbig_title);
36 setDescription(R.string.sample_jbig_description);
37 }
38
39 @Override
40 public void run(OutputListener outputListener) {
41 super.run(outputListener);
42 mOutputListener = outputListener;
43 mFileList.clear();
44 printHeader(outputListener);
45
46 try (PDFDoc pdf_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "US061222892-a.pdf").getAbsolutePath())) {
47 pdf_doc.initSecurityHandler();
48
49 SDFDoc cos_doc = pdf_doc.getSDFDoc();
50 int num_objs = (int) cos_doc.xRefSize();
51 for (int i = 1; i < num_objs; ++i) {
52 Obj obj = cos_doc.getObj(i);
53 if (obj != null && !obj.isFree() && obj.isStream()) {
54 // Process only images
55 DictIterator itr = obj.find("Subtype");
56 if (!itr.hasNext() || !itr.value().getName().equals("Image"))
57 continue;
58
59 Image input_image = new Image(obj);
60 // Process only gray-scale images
61 if (input_image.getComponentNum() != 1)
62 continue;
63 int bpc = input_image.getBitsPerComponent();
64 if (bpc != 1) // Recompress only 1 BPC images
65 continue;
66
67 // Skip images that are already compressed using JBIG2
68 itr = obj.find("Filter");
69 if (itr.hasNext() && itr.value().isName() &&
70 !itr.value().getName().equals("JBIG2Decode")) continue;
71
72 Filter filter = obj.getDecodedStream();
73 FilterReader reader = new FilterReader(filter);
74
75 ObjSet hint_set = new ObjSet();
76 Obj hint = hint_set.createArray(); // A hint to image encoder to use JBIG2 compression
77 hint.pushBackName("JBIG2");
78 hint.pushBackName("Lossless");
79
80 Image new_image = Image.create(cos_doc, reader,
81 input_image.getImageWidth(),
82 input_image.getImageHeight(), 1, ColorSpace.createDeviceGray(), hint);
83
84 Obj new_img_obj = new_image.getSDFObj();
85 itr = obj.find("Decode");
86 if (itr.hasNext())
87 new_img_obj.put("Decode", itr.value());
88 itr = obj.find("ImageMask");
89 if (itr.hasNext())
90 new_img_obj.put("ImageMask", itr.value());
91 itr = obj.find("Mask");
92 if (itr.hasNext())
93 new_img_obj.put("Mask", itr.value());
94
95 cos_doc.swap(i, new_img_obj.getObjNum());
96 }
97 }
98
99 pdf_doc.save(Utils.createExternalFile("US061222892_JBIG2.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
100 } catch (Exception e) {
101 mOutputListener.printError(e.getStackTrace());
102 }
103
104 for (String file : mFileList) {
105 addToFileList(file);
106 }
107 printFooter(outputListener);
108 }
109
110
111}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales