Optimizer

Sample JavaScript 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. Learn more about our Web SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//---------------------------------------------------------------------------------------
7// The following sample illustrates how to reduce PDF file size using 'pdftron.PDF.Optimizer'.
8// The sample also shows how to simplify and optimize PDF documents for viewing on mobile devices
9// and on the Web using 'pdftron.PDF.Flattener'.
10//
11// @note Both 'Optimizer' and 'Flattener' are separately licensable add-on options to the core PDFNet license.
12//
13// ----
14//
15// 'pdftron.PDF.Optimizer' can be used to optimize PDF documents by reducing the file size, removing
16// redundant information, and compressing data streams using the latest in image compression technology.
17//
18// PDF Optimizer can compress and shrink PDF file size with the following operations:
19// - Remove duplicated fonts, images, ICC profiles, and any other data stream.
20// - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF.
21// - Optionally down-sample large images to a given resolution.
22// - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats.
23// - Compress uncompressed streams and remove unused PDF objects.
24//
25// ----
26//
27// 'pdftron.PDF.Flattener' can be used to speed-up PDF rendering on mobile devices and on the Web by
28// simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text
29// whenever possible.
30//
31// Flattener can also be used to simplify process of writing custom converters from PDF to other formats.
32// In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a
33// very simple representation (e.g. vector text on top of a background image).
34//---------------------------------------------------------------------------------------
35
36(exports => {
37
38
39 const PDFNet = exports.Core.PDFNet;
40
41 exports.runOptimizerTest = () => {
42 const main = async () => {
43 const inputPath = '../TestFiles/';
44 const inputFilename = 'newsletter';
45
46 // The first step in every application using PDFNet is to initialize the
47 // library and set the path to common PDF resources. The library is usually
48 // initialized only once, but calling Initialize() multiple times is also fine.
49
50 //--------------------------------------------------------------------------------
51 // Example 1) Simple optimization of a pdf with default settings.
52 //
53 try {
54 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + inputFilename + '.pdf');
55 doc.initSecurityHandler();
56 await doc.lock();
57 await PDFNet.Optimizer.optimize(doc);
58 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
59 saveBufferAsPDFDoc(docbuf, inputFilename + '_opt1.pdf');
60 } catch (err) {
61 console.log(err);
62 }
63
64 //--------------------------------------------------------------------------------
65 // Example 2) Reduce image quality and use jpeg compression for
66 // non monochrome images.
67 try {
68 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + inputFilename + '.pdf');
69 doc.initSecurityHandler();
70 await doc.lock();
71 const imageSettings = new PDFNet.Optimizer.ImageSettings();
72
73 // low quality jpeg compression
74 imageSettings.setCompressionMode(PDFNet.Optimizer.ImageSettings.CompressionMode.e_jpeg);
75 imageSettings.setQuality(1);
76
77 // Set the output dpi to be standard screen resolution
78 imageSettings.setImageDPI(144, 96);
79
80 // this option will recompress images not compressed with
81 // jpeg compression and use the result if the new image
82 // is smaller.
83 imageSettings.forceRecompression(true);
84
85 // this option is not commonly used since it can
86 // potentially lead to larger files. It should be enabled
87 // only if the output compression specified should be applied
88 // to every image of a given type regardless of the output image size
89 // imageSettings.forceChanges(true);
90
91 const optSettings = new PDFNet.Optimizer.OptimizerSettings();
92 optSettings.setColorImageSettings(imageSettings);
93 optSettings.setGrayscaleImageSettings(imageSettings);
94
95 // use the same settings for both color and grayscale images
96 await PDFNet.Optimizer.optimize(doc, optSettings);
97
98 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
99 saveBufferAsPDFDoc(docbuf, inputFilename + '_opt2.pdf');
100 } catch (err) {
101 console.log(err);
102 }
103
104 //--------------------------------------------------------------------------------
105 // Example 3) Use monochrome image settings and default settings
106 // for color and grayscale images.
107 try {
108 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + inputFilename + '.pdf');
109 doc.initSecurityHandler();
110 await doc.lock();
111
112 const monoImageSettings = new PDFNet.Optimizer.MonoImageSettings();
113
114 monoImageSettings.setCompressionMode(PDFNet.Optimizer.MonoImageSettings.CompressionMode.e_jbig2);
115 monoImageSettings.forceRecompression(true);
116
117 const optSettings = new PDFNet.Optimizer.OptimizerSettings();
118
119 optSettings.setMonoImageSettings(monoImageSettings);
120
121 await PDFNet.Optimizer.optimize(doc, optSettings);
122
123 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
124 saveBufferAsPDFDoc(docbuf, inputFilename + '_opt3.pdf');
125 } catch (err) {
126 console.log(err);
127 }
128
129 // ----------------------------------------------------------------------
130 // Example 4) Use Flattener to simplify content in this document
131 // using default settings
132 try {
133 const doc = await PDFNet.PDFDoc.createFromURL(inputPath + 'TigerText.pdf');
134 doc.initSecurityHandler();
135 await doc.lock();
136
137 const fl = await PDFNet.Flattener.create();
138
139 // The following lines can increase the resolution of background
140 // images.
141 // fl.setDPI(300);
142 // fl.setMaximumImagePixels(5000000);
143
144 // This line can be used to output Flate compressed background
145 // images rather than DCTDecode compressed images which is the default
146 // fl.setPreferJPG(false);
147
148 // In order to adjust thresholds for when text is Flattened
149 // the following function can be used.
150 // fl.setThreshold(PDFNet.Flattener.Threshold.e_keep_most);
151
152 // We use e_fast option here since it is usually preferable
153 // to avoid Flattening simple pages in terms of size and
154 // rendering speed. If the desire is to simplify the
155 // document for processing such that it contains only text and
156 // a background image e_simple should be used instead.
157 await fl.process(doc, PDFNet.Flattener.Mode.e_fast);
158
159 const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
160 saveBufferAsPDFDoc(docbuf, 'TigerText_flatten.pdf');
161 } catch (err) {
162 console.log(err);
163 }
164 };
165
166 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
167 PDFNet.runWithCleanup(main);
168 };
169 exports.runOptimizerTest();
170})(window);
171// eslint-disable-next-line spaced-comment
172//# sourceURL=OptimizerTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales