Compress PDFs - Optimizer - C++ Sample Code

Sample code for using Apryse SDK to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB. 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 <PDF/PDFNet.h>
7#include <PDF/Optimizer.h>
8#include <iostream>
9#include <PDF/Flattener.h>
10#include "../../LicenseKey/CPP/LicenseKey.h"
11
12using namespace std;
13using namespace pdftron;
14using namespace Common;
15using namespace SDF;
16using namespace PDF;
17
18//---------------------------------------------------------------------------------------
19// The following sample illustrates how to reduce PDF file size using 'pdftron.PDF.Optimizer'.
20// The sample also shows how to simplify and optimize PDF documents for viewing on mobile devices
21// and on the Web using 'pdftron.PDF.Flattener'.
22//
23// @note Both 'Optimizer' and 'Flattener' are separately licensable add-on options to the core PDFNet license.
24//
25// ----
26//
27// 'pdftron.PDF.Optimizer' can be used to optimize PDF documents by reducing the file size, removing
28// redundant information, and compressing data streams using the latest in image compression technology.
29//
30// PDF Optimizer can compress and shrink PDF file size with the following operations:
31// - Remove duplicated fonts, images, ICC profiles, and any other data stream.
32// - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF.
33// - Optionally down-sample large images to a given resolution.
34// - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats.
35// - Compress uncompressed streams and remove unused PDF objects.
36//
37// ----
38//
39// 'pdftron.PDF.Flattener' can be used to speed-up PDF rendering on mobile devices and on the Web by
40// simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text
41// whenever possible.
42//
43// Flattener can also be used to simplify process of writing custom converters from PDF to other formats.
44// In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a
45// very simple representation (e.g. vector text on top of a background image).
46//---------------------------------------------------------------------------------------
47
48int main(int argc, char * argv[])
49{
50 int ret = 0;
51 std::string input_path = "../../TestFiles/";
52 std::string output_path = "../../TestFiles/Output/";
53 std::string input_filename = "newsletter";
54
55 // The first step in every application using PDFNet is to initialize the
56 // library and set the path to common PDF resources. The library is usually
57 // initialized only once, but calling Initialize() multiple times is also fine.
58 PDFNet::Initialize(LicenseKey);
59
60 //--------------------------------------------------------------------------------
61 // Example 1) Simple optimization of a pdf with default settings.
62 //
63 try
64 {
65 pdftron::PDF::PDFDoc doc(input_path + input_filename + ".pdf");
66 doc.InitSecurityHandler();
67
68 Optimizer::Optimize(doc);
69
70 doc.Save(output_path + input_filename + "_opt1.pdf", SDFDoc::e_linearized, NULL);
71 }
72 catch (Common::Exception& e)
73 {
74 std::cout << e << endl;
75 ret = 1;
76 }
77 catch (...)
78 {
79 cout << "Unknown Exception" << endl;
80 ret = 1;
81 }
82
83 //--------------------------------------------------------------------------------
84 // Example 2) Reduce image quality and use jpeg compression for
85 // non monochrome images.
86 try
87 {
88 PDFDoc doc(input_path + input_filename + ".pdf");
89 doc.InitSecurityHandler();
90
91 Optimizer::ImageSettings image_settings;
92
93 // low quality jpeg compression
94 image_settings.SetCompressionMode(Optimizer::ImageSettings::e_jpeg);
95 image_settings.SetQuality(1);
96
97 // Set the output dpi to be standard screen resolution
98 image_settings.SetImageDPI(144,96);
99
100 // this option will recompress images not compressed with
101 // jpeg compression and use the result if the new image
102 // is smaller.
103 image_settings.ForceRecompression(true);
104
105 // this option is not commonly used since it can
106 // potentially lead to larger files. It should be enabled
107 // only if the output compression specified should be applied
108 // to every image of a given type regardless of the output image size
109 //image_settings.ForceChanges(true);
110
111
112 Optimizer::OptimizerSettings opt_settings;
113 opt_settings.SetColorImageSettings(image_settings);
114 opt_settings.SetGrayscaleImageSettings(image_settings);
115
116 // use the same settings for both color and grayscale images
117 Optimizer::Optimize(doc, opt_settings);
118
119 doc.Save(output_path + input_filename + "_opt2.pdf", SDFDoc::e_linearized, NULL);
120 }
121 catch (Common::Exception& e)
122 {
123 std::cout << e << endl;
124 ret = 1;
125 }
126 catch (...)
127 {
128 cout << "Unknown Exception" << endl;
129 ret = 1;
130 }
131
132 //--------------------------------------------------------------------------------
133 // Example 3) Use monochrome image settings and default settings
134 // for color and grayscale images.
135 try
136 {
137 PDFDoc doc(input_path + input_filename + ".pdf");
138 doc.InitSecurityHandler();
139
140 Optimizer::MonoImageSettings mono_image_settings;
141
142 mono_image_settings.SetCompressionMode(Optimizer::MonoImageSettings::e_jbig2);
143 mono_image_settings.ForceRecompression(true);
144
145 Optimizer::OptimizerSettings opt_settings;
146 opt_settings.SetMonoImageSettings(mono_image_settings);
147
148 Optimizer::Optimize(doc, opt_settings);
149
150 doc.Save(output_path + input_filename + "_opt3.pdf", SDFDoc::e_linearized, NULL);
151 }
152 catch (Common::Exception& e)
153 {
154 std::cout << e << endl;
155 ret = 1;
156 }
157 catch (...)
158 {
159 cout << "Unknown Exception" << endl;
160 ret = 1;
161 }
162
163 // ----------------------------------------------------------------------
164 // Example 4) Use Flattener to simplify content in this document
165 // using default settings
166 try
167 {
168 PDFDoc doc(input_path + "TigerText.pdf");
169 doc.InitSecurityHandler();
170
171 Flattener fl;
172
173 // The following lines can increase the resolution of background
174 // images.
175 //fl.SetDPI(300);
176 //fl.SetMaximumImagePixels(5000000);
177
178 // This line can be used to output Flate compressed background
179 // images rather than DCTDecode compressed images which is the default
180 //fl.SetPreferJPG(false);
181
182 // In order to adjust thresholds for when text is Flattened
183 // the following function can be used.
184 //fl.SetThreshold(Flattener::e_keep_most);
185
186 // We use e_fast option here since it is usually preferable
187 // to avoid Flattening simple pages in terms of size and
188 // rendering speed. If the desire is to simplify the
189 // document for processing such that it contains only text and
190 // a background image e_simple should be used instead.
191 fl.Process(doc,Flattener::e_fast);
192
193 doc.Save(output_path + "TigerText_flatten.pdf", SDFDoc::e_linearized, NULL);
194
195 }
196 catch (Common::Exception& e)
197 {
198 std::cout << e << endl;
199 ret = 1;
200 }
201 catch (...)
202 {
203 cout << "Unknown Exception" << endl;
204 ret = 1;
205 }
206
207 //--------------------------------------------------------------------------------
208 // Example 5) Optimize a PDF for viewing using SaveViewerOptimized.
209 try
210 {
211 PDFDoc doc(input_path + input_filename + ".pdf");
212 doc.InitSecurityHandler();
213
214 ViewerOptimizedOptions opts;
215
216 // set the maximum dimension (width or height) that thumbnails will have.
217 opts.SetThumbnailSize(1500);
218
219 // set thumbnail rendering threshold. A number from 0 (include all thumbnails) to 100 (include only the first thumbnail)
220 // representing the complexity at which SaveViewerOptimized would include the thumbnail.
221 // By default it only produces thumbnails on the first and complex pages.
222 // The following line will produce thumbnails on every page.
223 // opts.SetThumbnailRenderingThreshold(0);
224
225 doc.SaveViewerOptimized(output_path + input_filename + "_SaveViewerOptimized.pdf", opts);
226 }
227 catch (Common::Exception& e)
228 {
229 std::cout << e << endl;
230 ret = 1;
231 }
232 catch (...)
233 {
234 cout << "Unknown Exception" << endl;
235 ret = 1;
236 }
237
238 PDFNet::Terminate();
239 return ret;
240}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales