PDF/A

Sample Obj-C code for using Apryse 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 iOS SDK and PDF/A Library. A command-line tool for batch conversion and validation is also available.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
8
9func PrintResults(pdf_a: PTPDFACompliance, filename: String) {
10 let err_cnt: UInt = pdf_a.getErrorCount()
11 if err_cnt == 0 {
12 print("\(filename) OK.")
13 }
14 else {
15 print("\(filename) is NOT a valid PDFA.")
16
17 for i in 0..<err_cnt {
18 let c = pdf_a.getError(i)
19 print(" - e_PDFA \(c.rawValue): \(PTPDFACompliance.getPDFAErrorMessage(c)!).")
20 if true {
21 let num_refs: UInt = pdf_a.getRefObjCount(c)
22 if num_refs > 0 {
23 print(" Objects:")
24 var str = ""
25
26 for j in 0..<num_refs {
27 str = str + ("\(pdf_a.getRefObj(c, err_idx: j))")
28 if j < num_refs - 1 {
29 str = str + (", ")
30 }
31 }
32 print("\(str)")
33 }
34 }
35 }
36 }
37}
38
39//---------------------------------------------------------------------------------------
40// The following sample illustrates how to parse and check if a PDF document meets the
41// PDFA standard, using the PTPDFACompliance class object.
42//---------------------------------------------------------------------------------------
43func runPDFATest() -> Int {
44 return autoreleasepool {
45 var ret = 0
46
47
48 PTPDFNet.setColorManagement(e_ptlcms)
49
50 // Enable color management (required for PDFA validation).
51 //-----------------------------------------------------------
52 // Example 1: PDF/A Validation
53 //-----------------------------------------------------------
54 do {
55 try PTPDFNet.catchException {
56 let filename = "newsletter.pdf"
57 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)
58 PrintResults(pdf_a: pdf_a, filename: filename)
59 }
60 } catch let e as NSError {
61 print("\(e)")
62 ret = 1
63 }
64
65 //-----------------------------------------------------------
66 // Example 2: PDF/A Conversion
67 //-----------------------------------------------------------
68 do {
69 try PTPDFNet.catchException {
70 var filename = "fish.pdf"
71 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)
72 filename = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("pdfa.pdf").path
73 pdf_a.save(asFile: filename, linearized: true)
74
75 // Re-validate the document after the conversion...
76 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)
77 PrintResults(pdf_a: comp, filename: filename)
78 }
79 } catch let e as NSError {
80 print("\(e)")
81 ret = 1
82 }
83
84 print("PTPDFACompliance test completed.")
85 return ret
86 }
87}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales