Some test text!

< iOS samples

PDFATest - Swift

This sample illustrates how to use PDF/A add-on to validate existing PDF documents for PDF/A compliance as well as to convert generic PDF documents to PDF/A format.

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

import PDFNet
import Foundation

func PrintResults(pdf_a: PTPDFACompliance, filename: String) {
    let err_cnt: UInt = pdf_a.getErrorCount()
    if err_cnt == 0 {
        print("\(filename) OK.")
    }
    else {
        print("\(filename) is NOT a valid PDFA.")
        
        for i in 0..<err_cnt {
            let c = pdf_a.getError(i)
            print(" - e_PDFA \(c.rawValue): \(PTPDFACompliance.getPDFAErrorMessage(c)!).")
            if true {
                let num_refs: UInt = pdf_a.getRefObjCount(c)
                if num_refs > 0 {
                    print("   Objects:")
                    var str = ""
                    
                    for j in 0..<num_refs {
                        str = str + ("\(pdf_a.getRefObj(c, err_idx: j))")
                        if j < num_refs - 1 {
                            str = str + (", ")
                        }
                    }
                    print("\(str)")
                }
            }
        }
    }
}

//---------------------------------------------------------------------------------------
// The following sample illustrates how to parse and check if a PDF document meets the
//    PDFA standard, using the PTPDFACompliance class object.
//---------------------------------------------------------------------------------------
func runPDFATest() -> Int {
    return autoreleasepool {
        var ret = 0
        
        
        PTPDFNet.setColorManagement(e_ptlcms)
        
        // Enable color management (required for PDFA validation).
        //-----------------------------------------------------------
        // Example 1: PDF/A Validation
        //-----------------------------------------------------------
        do {
            try PTPDFNet.catchException {
                let filename = "newsletter.pdf"
                let pdf_a: PTPDFACompliance = PTPDFACompliance(convert: false, file_path: Bundle.main.path(forResource: "newsletter", ofType: "pdf"), password: "", conf: e_ptLevel1B, exceptions: 0, num_exceptions: 10, max_ref_objs: 10, first_stop: false)
                PrintResults(pdf_a: pdf_a, filename: filename)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        //-----------------------------------------------------------
        // Example 2: PDF/A Conversion
        //-----------------------------------------------------------
        do {
            try PTPDFNet.catchException {
                var filename = "fish.pdf"
                let pdf_a: PTPDFACompliance = PTPDFACompliance(convert: true, file_path: Bundle.main.path(forResource: "fish", ofType: "pdf"), password: "", conf: e_ptLevel1B, exceptions: 0, num_exceptions: 10, max_ref_objs: 10, first_stop: false)
                filename = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("pdfa.pdf").path
                pdf_a.save(asFile: filename, linearized: true)
                
                // Re-validate the document after the conversion...
                let comp: PTPDFACompliance = PTPDFACompliance(convert: false, file_path: filename, password: "", conf: e_ptLevel1B, exceptions: 0, num_exceptions: 10, max_ref_objs: 10, first_stop: false)
                PrintResults(pdf_a: comp, filename: filename)
            }
        } catch let e as NSError {
            print("\(e)")
            ret = 1
        }
        
        print("PTPDFACompliance test completed.")
        return ret
    }
}