Some test text!

menu
search
chevron_right iOS samples

Convert PDF to PDF/A in Swift

Sample Swift code for using PDFTron SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/A files, or to validate PDF/A compliance. Supports all three PDF/A parts (PDF/A-1, PDF/A-2, PDF/A-3), and covers all conformance levels (A, B, U). Learn more about our PDF/A Library. A command-line tool for batch conversion and validation is also available.

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

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

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close