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}
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.FilterReader
13import com.pdftron.pdf.ColorSpace
14import com.pdftron.pdf.Image
15import com.pdftron.pdf.PDFDoc
16import com.pdftron.sdf.ObjSet
17import com.pdftron.sdf.SDFDoc
18import java.util.*
19
20//This sample project illustrates how to recompress bi-tonal images in an
21//existing PDF document using JBIG2 compression. The sample is not intended
22//to be a generic PDF optimization tool.
23
24class JBIG2Test : PDFNetSample() {
25 init {
26 setTitle(R.string.sample_jbig_title)
27 setDescription(R.string.sample_jbig_description)
28 }
29
30 override fun run(outputListener: OutputListener?) {
31 super.run(outputListener)
32 mOutputListener = outputListener
33 mFileList.clear()
34 printHeader(outputListener!!)
35
36 try {
37 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "US061222892-a.pdf")!!.absolutePath).use { pdf_doc ->
38 pdf_doc.initSecurityHandler()
39
40 val cos_doc = pdf_doc.sdfDoc
41 val num_objs = cos_doc.xRefSize().toInt()
42 for (i in 1 until num_objs) {
43 val obj = cos_doc.getObj(i.toLong())
44 if (obj != null && !obj.isFree && obj.isStream) {
45 // Process only images
46 var itr = obj.find("Subtype")
47 if (!itr.hasNext() || itr.value().name != "Image")
48 continue
49
50 val input_image = Image(obj)
51 // Process only gray-scale images
52 if (input_image.componentNum != 1)
53 continue
54 val bpc = input_image.bitsPerComponent
55 if (bpc != 1)
56 // Recompress only 1 BPC images
57 continue
58
59 // Skip images that are already compressed using JBIG2
60 itr = obj.find("Filter")
61 if (itr.hasNext() && itr.value().isName &&
62 itr.value().name != "JBIG2Decode")
63 continue
64
65 val filter = obj.decodedStream
66 val reader = FilterReader(filter)
67
68 val hint_set = ObjSet()
69 val hint = hint_set.createArray() // A hint to image encoder to use JBIG2 compression
70 hint.pushBackName("JBIG2")
71 hint.pushBackName("Lossless")
72
73 val new_image = Image.create(cos_doc, reader,
74 input_image.imageWidth,
75 input_image.imageHeight, 1, ColorSpace.createDeviceGray(), hint)
76
77 val new_img_obj = new_image.sdfObj
78 itr = obj.find("Decode")
79 if (itr.hasNext())
80 new_img_obj.put("Decode", itr.value())
81 itr = obj.find("ImageMask")
82 if (itr.hasNext())
83 new_img_obj.put("ImageMask", itr.value())
84 itr = obj.find("Mask")
85 if (itr.hasNext())
86 new_img_obj.put("Mask", itr.value())
87
88 cos_doc.swap(i.toLong(), new_img_obj.objNum)
89 }
90 }
91
92 pdf_doc.save(Utils.createExternalFile("US061222892_JBIG2.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
93 }
94 } catch (e: Exception) {
95 mOutputListener!!.printError(e.stackTrace)
96 }
97
98 for (file in mFileList) {
99 addToFileList(file)
100 }
101 printFooter(outputListener)
102 }
103
104 companion object {
105
106 private var mOutputListener: OutputListener? = null
107
108 private val mFileList = ArrayList<String>()
109 }
110
111}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales