Optimize & compress PDFs on Server/Desktop

To optimize a PDF with default settings is quick once you are set up.

1doc := NewPDFDoc(filename)
2OptimizerOptimize(doc)

Compress & optimize PDF files - Full Sample
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. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.

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.

1cosDoc := pdfDoc.GetSDFDoc()
2numObjs := cosDoc.XRefSize()
3
4i := uint(1)
5for i < numObjs{
6 obj := cosDoc.GetObj(i)
7 if obj != nil && ! obj.IsFree() && obj.IsStream(){
8 // Process only images
9 itr := obj.Find("Subtype")
10 //if not itr.HasNext() or not itr.Value().GetName() == "Image":
11 if !itr.HasNext() || !(itr.Value().GetName() == "Image"){
12 i = i + 1
13 continue
14 }
15 inputImage := NewImage(obj)
16 // Process only gray-scale images
17 if inputImage.GetComponentNum() != 1{
18 i = i + 1
19 continue
20 }
21 // Skip images that are already compressed using JBIG2
22 itr = obj.Find("Filter")
23 if (itr.HasNext() && itr.Value().IsName() && itr.Value().GetName() == "JBIG2Decode"){
24 i = i + 1
25 continue
26 }
27
28 filter := obj.GetDecodedStream()
29 reader := NewFilterReader(filter)
30
31 hintSet := NewObjSet() // hint to image encoder to use JBIG2 compression
32 hint := hintSet.CreateArray()
33
34 hint.PushBackName("JBIG2")
35 hint.PushBackName("Lossless")
36
37 newImage := (ImageCreate(cosDoc, reader,
38 inputImage.GetImageWidth(),
39 inputImage.GetImageHeight(),
40 1,
41 ColorSpaceCreateDeviceGray(),
42 hint))
43
44 newImgObj := newImage.GetSDFObj()
45 itr = obj.Find("Decode")
46
47 if itr.HasNext(){
48 newImgObj.Put("Decode", itr.Value())
49 }
50 itr = obj.Find("ImageMask")
51 if itr.HasNext(){
52 newImgObj.Put("ImageMask", itr.Value())
53 }
54 itr = obj.Find("Mask")
55 if itr.HasNext(){
56 newImgObj.Put("Mask", itr.Value())
57 }
58
59 cosDoc.Swap(i, newImgObj.GetObjNum())
60 }
61 i = i + 1
62}

PDF image JBIG2 compression - Full Sample
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales