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-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9void PrintResults(PTPDFACompliance *pdf_a, NSString *filename) {
10 unsigned long err_cnt = [pdf_a GetErrorCount];
11 if (err_cnt == 0)
12 {
13 NSLog(@"%@: OK.", filename);
14 }
15 else
16 {
17 NSLog(@"%@ is NOT a valid PDFA.", filename);
18 int i = 0;
19 for (; i<err_cnt; ++i)
20 {
21 int c = [pdf_a GetError: i];
22 NSLog(@" - e_PDFA %d: %@.", c, [PTPDFACompliance GetPDFAErrorMessage: c]);
23 if (true)
24 {
25 unsigned long num_refs = [pdf_a GetRefObjCount: c];
26 if (num_refs > 0)
27 {
28 NSString *str = @" Objects: ";
29 int j=0;
30 for (; j<num_refs; ++j)
31 {
32 str = [str stringByAppendingFormat: @"%lu", [pdf_a GetRefObj: c err_idx: j]];
33 if (j<num_refs-1)
34 str = [str stringByAppendingString: @", "];
35 }
36 NSLog(@"%@", str);
37 }
38 }
39 }
40 NSLog(@"\n");
41 }
42}
43
44//---------------------------------------------------------------------------------------
45// The following sample illustrates how to parse and check if a PDF document meets the
46// PDFA standard, using the PTPDFACompliance class object.
47//---------------------------------------------------------------------------------------
48int main(int argc, char *argv[])
49{
50 @autoreleasepool {
51
52 int ret = 0;
53
54 [PTPDFNet Initialize: 0];
55 [PTPDFNet SetColorManagement: e_ptlcms]; // Enable color management (required for PDFA validation).
56
57 //-----------------------------------------------------------
58 // Example 1: PDF/A Validation
59 //-----------------------------------------------------------
60 @try
61 {
62 NSString *filename = @"newsletter.pdf";
63 // The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
64 // of object numbers that are collected for particular error codes. The default value is 10
65 // in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs.
66 PTPDFACompliance *pdf_a = [[PTPDFACompliance alloc] initWithConvert: NO file_path: @"../../TestFiles/newsletter.pdf" password: @"" conf: e_ptLevel2B exceptions: 0 num_exceptions: 10 max_ref_objs: 10 first_stop: NO];
67 PrintResults(pdf_a, filename);
68 }
69 @catch (NSException *e)
70 {
71 NSLog(@"%@", e.reason);
72 ret = 1;
73 }
74
75 //-----------------------------------------------------------
76 // Example 2: PDF/A Conversion
77 //-----------------------------------------------------------
78 @try
79 {
80 NSString *filename = @"fish.pdf";
81 PTPDFACompliance *pdf_a = [[PTPDFACompliance alloc] initWithConvert: YES file_path: @"../../TestFiles/fish.pdf" password: @"" conf: e_ptLevel2B exceptions: 0 num_exceptions: 10 max_ref_objs: 10 first_stop: NO];
82 filename = @"../../TestFiles/Output/pdfa.pdf";
83 [pdf_a SaveAsFile: filename linearized: NO];
84
85 // Re-validate the document after the conversion...
86 PTPDFACompliance *comp =[[PTPDFACompliance alloc] initWithConvert: NO file_path: filename password: @"" conf: e_ptLevel2B exceptions: 0 num_exceptions: 10 max_ref_objs: 10 first_stop: NO];
87 filename = @"pdfa.pdf";
88 PrintResults(comp, filename);
89 }
90 @catch (NSException *e)
91 {
92 NSLog(@"%@", e.reason);
93 ret = 1;
94 }
95
96 NSLog(@"PDFACompliance test completed.");
97 [PTPDFNet Terminate: 0];
98 return ret;
99 }
100}
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 DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales