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-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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales