Some test text!

< iOS samples

JBIG2Test - Swift

The sample project illustrates how to recompress bi-tonal (black and white) images in existing PDF documents using JBIG2 compression. The sample is intended to show how to specify hint information for image encoder and is not meant to be a generic PDF optimization tool. PDFNet supports both loss-less and lossy JBIG2 compression. To give you a feeling of compression rates possible with PDFNet we re-compressed a document containing 17 scanned pages. The original input document is ~1.4MB and is using standard CCITT Fax compression. Loss-less JBIG2 compression shrunk down the file to 641KB. Lossy JBIG2 compression shrunk down the file to 176KB.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2017 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
    }
}