Compress PDF Image JBIG3 - C++ 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
6#include <iostream>
7#include <PDF/PDFNet.h>
8#include <PDF/PDFDoc.h>
9#include <PDF/ElementBuilder.h>
10#include <Filters/FilterReader.h>
11#include <SDF/ObjSet.h>
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14using namespace std;
15using namespace pdftron;
16using namespace Filters;
17using namespace PDF;
18using namespace SDF;
19using namespace Common;
20
21// This sample project illustrates how to recompress bi-tonal images in an
22// existing PDF document using JBIG2 compression. The sample is not intended
23// to be a generic PDF optimization tool.
24//
25// You can download the entire document using the following link:
26// http://www.pdftron.com/net/samplecode/data/US061222892.pdf
27//
28int main(int argc, char *argv[])
29{
30 PDFNet::Initialize(LicenseKey);
31
32 try
33 {
34 PDFDoc pdf_doc("../../TestFiles/US061222892-a.pdf");
35 pdf_doc.InitSecurityHandler();
36
37 SDFDoc& cos_doc = pdf_doc.GetSDFDoc();
38 int num_objs = cos_doc.XRefSize();
39 for(int i=1; i<num_objs; ++i)
40 {
41 Obj obj = cos_doc.GetObj(i);
42 if(obj && !obj.IsFree() && obj.IsStream())
43 {
44 // Process only images
45 DictIterator itr = obj.Find("Subtype");
46 if(!itr.HasNext() || strcmp(itr.Value().GetName(), "Image"))
47 continue;
48
49 Image input_image(obj);
50 // Process only gray-scale images
51 if(input_image.GetComponentNum() != 1)
52 continue;
53 int bpc = input_image.GetBitsPerComponent();
54 if(bpc != 1) // Recompress only 1 BPC images
55 continue;
56
57 // Skip images that are already compressed using JBIG2
58 itr = obj.Find("Filter");
59 if (itr.HasNext() && itr.Value().IsName() &&
60 !strcmp(itr.Value().GetName(), "JBIG2Decode")) continue;
61
62 Filter filter=obj.GetDecodedStream();
63 FilterReader reader(filter);
64
65
66 ObjSet hint_set; // A hint to image encoder to use JBIG2 compression
67 Obj hint=hint_set.CreateArray();
68
69 hint.PushBackName("JBIG2");
70 hint.PushBackName("Lossless");
71
72 Image new_image = Image::Create(cos_doc, reader,
73 input_image.GetImageWidth(),
74 input_image.GetImageHeight(), 1, ColorSpace::CreateDeviceGray(), hint);
75
76 Obj new_img_obj = new_image.GetSDFObj();
77 itr = obj.Find("Decode");
78 if(itr.HasNext())
79 new_img_obj.Put("Decode", itr.Value());
80 itr = obj.Find("ImageMask");
81 if (itr.HasNext())
82 new_img_obj.Put("ImageMask", itr.Value());
83 itr = obj.Find("Mask");
84 if (itr.HasNext())
85 new_img_obj.Put("Mask", itr.Value());
86
87 cos_doc.Swap(i, new_img_obj.GetObjNum());
88 }
89 }
90
91 pdf_doc.Save("../../TestFiles/Output/US061222892_JBIG2.pdf", SDFDoc::e_remove_unused, 0);
92 }
93 catch(Common::Exception& e)
94 {
95 cout << e << endl;
96 cout << "Please make sure that the pathname to the test file is correct." << endl;
97 }
98 catch(...)
99 {
100 cout << "Unknown Exception" << endl;
101 }
102
103 PDFNet::Terminate();
104 return 0;
105}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales