PDF/A

Sample 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 Server 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//
4
5using System;
6using pdftron;
7using pdftron.SDF;
8using pdftron.PDF;
9using pdftron.PDF.PDFA;
10
11//-----------------------------------------------------------------------------------
12// The sample illustrates how to use PDF/A related API-s.
13//-----------------------------------------------------------------------------------
14namespace PDFATestCS
15{
16 class Class1
17 {
18 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
19 static Class1() {}
20
21 // Relative path to the folder containing test files.
22 static string input_path = "../../../../TestFiles/";
23 static string output_path = "../../../../TestFiles/Output/";
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 [STAThread]
29 static void Main(string[] args)
30 {
31 PDFNet.Initialize(PDFTronLicense.Key);
32 PDFNet.SetColorManagement(PDFNet.CMSType.e_lcms); // Required for PDFA validation.
33
34 //-----------------------------------------------------------
35 // Example 1: PDF/A Validation
36 //-----------------------------------------------------------
37 try
38 {
39 string filename = "newsletter.pdf";
40 using (PDFACompliance pdf_a = new PDFACompliance(false, input_path+filename, null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
41 {
42 PrintResults(pdf_a, filename);
43 }
44 }
45 catch (pdftron.Common.PDFNetException e)
46 {
47 Console.WriteLine(e.Message);
48 }
49
50 //-----------------------------------------------------------
51 // Example 2: PDF/A Conversion
52 //-----------------------------------------------------------
53 try
54 {
55 string filename = "fish.pdf";
56 using (PDFACompliance pdf_a = new PDFACompliance(true, input_path+filename, null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
57 {
58 filename = "pdfa.pdf";
59 pdf_a.SaveAs(output_path + filename, false);
60 }
61
62 // Re-validate the document after the conversion...
63 filename = "pdfa.pdf";
64 using (PDFACompliance pdf_a = new PDFACompliance(false, output_path + filename, null, PDFACompliance.Conformance.e_Level2B, null, 10, false))
65 {
66 PrintResults(pdf_a, filename);
67 }
68 }
69 catch (pdftron.Common.PDFNetException e)
70 {
71 Console.WriteLine(e.Message);
72 }
73 PDFNet.Terminate();
74 Console.WriteLine("PDFACompliance test completed.");
75
76 }
77
78 static void PrintResults(PDFACompliance pdf_a, String filename)
79 {
80 int err_cnt = pdf_a.GetErrorCount();
81 if (err_cnt == 0)
82 {
83 Console.WriteLine("{0}: OK.", filename);
84 }
85 else
86 {
87 Console.WriteLine("{0} is NOT a valid PDFA.", filename);
88 for (int i=0; i<err_cnt; ++i)
89 {
90 PDFACompliance.ErrorCode c = pdf_a.GetError(i);
91 Console.WriteLine(" - e_PDFA {0}: {1}.",
92 (int)c, PDFACompliance.GetPDFAErrorMessage(c));
93
94 if (true)
95 {
96 int num_refs = pdf_a.GetRefObjCount(c);
97 if (num_refs > 0)
98 {
99 Console.Write(" Objects: ");
100 for (int j=0; j<num_refs; )
101 {
102 Console.Write("{0}", pdf_a.GetRefObj(c, j));
103 if (++j!=num_refs) Console.Write(", ");
104 }
105 Console.WriteLine();
106 }
107 }
108 }
109 Console.WriteLine();
110 }
111 }
112 }
113}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales