Optimize & compress PDFs using JavaScript

To optimize a PDF with default settings.

JavaScript

1async function main() {
2 const doc = await PDFNet.PDFDoc.createFromFilePath(filename);
3 await PDFNet.Optimizer.optimize(doc);
4}
5PDFNet.runWithCleanup(main);

Compress & optimize PDF files
Full code sample which shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology.

About optimize and compress

Compression as a subset of optimization represents encoding specific data using fewer bits than the original content by reducing the size of the data. This is distinct from optimize (which modifies all images) because you have the ability to choose individual images and to selectively choose the compression type for each.

Apryse SDK supports all basic and advanced compression filters allowed in PDF including:

  • JPEG2000
  • JBIG2
  • CCITT Fax
  • Flate/PNG
  • JPEG/DCT
  • Crypt Filters

Compress images in a PDF document

To compress images using JBIG2 compression inside a PDF.

JavaScript

1async function main() {
2 const pdf_doc = await PDFNet.PDFDoc.createFromURL(filename);
3 pdf_doc.initSecurityHandler();
4
5 const cos_doc = await pdf_doc.getSDFDoc();
6 const num_objs = await cos_doc.xRefSize();
7 for (let i = 1; i < num_objs; ++i) {
8 const obj = await cos_doc.getObj(i);
9 if (obj && !(await obj.isFree()) && await obj.isStream()) {
10 // Process only images
11 var itr = await obj.find("Subtype");
12 if (!(await itr.hasNext()) || await (await itr.value()).getName() !== "Image")
13 continue;
14 const input_image = await PDFNet.Image.createFromObj(obj);
15 // Process only gray-scale images
16 if (await input_image.getComponentNum() != 1)
17 continue;
18 if (await input_image.getBitsPerComponent() != 1) // Recompress only 1 BPC images
19 continue;
20
21 // Skip images that are already compressed using JBIG2
22 itr = await obj.find("Filter");
23 if (await itr.hasNext()) {
24 const value = await itr.value();
25 if (await value.isName() && await value.getName() === "JBIG2Decode") continue;
26 }
27
28 const filter = await obj.getDecodedStream();
29 const reader = await PDFNet.FilterReader.create(filter);
30
31 const hint_set = await PDFNet.ObjSet.create();
32 const hint = await hint_set.createArray();
33
34 hint.pushBackName("JBIG2");
35 hint.pushBackName("Lossless");
36
37 const new_image = await PDFNet.Image.createFromStream(cos_doc, reader, await input_image.getImageWidth(),
38 await input_image.getImageHeight(), 1, await PDFNet.ColorSpace.createDeviceGray(), hint);
39
40 const new_img_obj = await new_image.getSDFObj();
41 itr = await obj.find("Decode");
42 if (await itr.hasNext())
43 new_img_obj.put("Decode", await itr.value());
44 itr = await obj.find("ImageMask");
45 if (await itr.hasNext())
46 new_img_obj.put("ImageMask", await itr.value());
47 itr = await obj.find("Mask");
48 if (await itr.hasNext())
49 new_img_obj.put("Mask", await itr.value());
50
51 await cos_doc.swap(i, await new_img_obj.getObjNum());
52 }
53 }
54}
55PDFNet.runWithCleanup(main);

PDF image JBIG2 compression
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales