Some test text!

Discord Logo

Chat with us

PDFTron is now Apryse, learn more here.

PDF image JBIG2 compression in Swift (lossy & lossless)

More languages

More languages
Java (Android)
C++
C#
C# (.NET Core)
Go
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample Swift code for using PDFTron 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. Learn more about our Swift PDF Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import PDFNet
import Foundation

// This sample project illustrates how to recompress bi-tonal images in an
// existing PDF document using JBIG2 compression. The sample is not intended
// to be a generic PDF optimization tool.
//
// You can download the entire document using the following link:
//   http://www.pdftron.com/net/samplecode/data/US061222892.pdf
//
func runJBIG2Test() -> Int {
    return autoreleasepool {
        
        
        do {
            try PTPDFNet.catchException {
                let pdf_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "US061222892-a", ofType: "pdf"))
                pdf_doc.initSecurityHandler()
                
                let cos_doc: PTSDFDoc = pdf_doc.getSDFDoc()
                let num_objs = cos_doc.xRefSize()
                for i in 1..<num_objs {
                    guard let obj: PTObj = cos_doc.getObj(UInt32(i)) else {
                        continue
                    }
                    if !obj.isFree() && obj.isStream() {
                        // Process only images
                        var itr: PTDictIterator = obj.find("Subtype")
                        if !itr.hasNext() || !(itr.value().getName() == "Image") {
                            continue
                        }
                        
                        let input_image: PTImage = PTImage(image_xobject: obj)
                        // Process only gray-scale images
                        if input_image.getComponentNum() != 1 {
                            continue
                        }
                        let bpc: Int32 = input_image.getBitsPerComponent()
                        if bpc != 1 {
                            // Recompress only 1 BPC images
                            continue
                        }
                        
                        // Skip images that are already compressed using JBIG2
                        itr = obj.find("Filter")
                        if itr.hasNext() && itr.value().isName() && (itr.value().getName() == "JBIG2Decode") {
                            continue
                        }
                        
                        let filter: PTFilter = obj.getDecodedStream()
                        let reader = PTFilterReader(filter: filter)
                        
                        let hint_set: PTObjSet = PTObjSet()   // A hint to image encoder to use JBIG2 compression
                        let hint: PTObj = hint_set.createArray()
                        
                        hint.pushBackName("JBIG2")
                        hint.pushBackName("Lossless")
                        
                        let new_image: PTImage = PTImage.create(withFilterData: cos_doc, image_data: reader, width: input_image.getWidth(), height: input_image.getHeight(), bpc: 1, color_space: PTColorSpace.createDeviceGray(), encoder_hints: hint)
                        
                        let new_img_obj: PTObj = new_image.getSDFObj()
                        itr = obj.find("Decode")
                        if itr.hasNext() {
                            new_img_obj.put("Decode", obj: itr.value())
                        }
                        itr = obj.find("ImageMask")
                        if itr.hasNext() {
                            new_img_obj.put("ImageMask", obj: itr.value())
                        }
                        itr = obj.find("Mask")
                        if itr.hasNext() {
                            new_img_obj.put("Mask", obj: itr.value())
                        }
                        
                        cos_doc.swap(UInt32(i), obj_num2: new_img_obj.getNum())
                    }
                }
                
                pdf_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("US061222892_JBIG2.pdf").path, flags: e_ptremove_unused.rawValue)
            }
        } catch let e as NSError {
            print("\(e)")
            print("Please make sure that the pathname to the test file is correct.")
        }
        
        print("Done.")
        
        return 0
    }
}