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 UWP SDK and PDF/A Library. A command-line tool for batch conversion and validation is also available.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron;
11using pdftron.PDF;
12using pdftron.PDF.PDFA;
13using pdftron.SDF;
14
15using PDFNetUniversalSamples.ViewModels;
16
17namespace PDFNetSamples
18{
19 public sealed class PDFATest : Sample
20 {
21 public PDFATest() :
22 base("PDF/A", "This sample illustrates how to use PDF/A add-on to validate existing PDF documents for PDF/A compliance as well as to convert generic PDF documents to PDF/A format.")
23 {
24 }
25
26 public override IAsyncAction RunAsync()
27 {
28 return Task.Run(new System.Action(async () =>
29 {
30 WriteLine("--------------------------------");
31 WriteLine("Starting PDF/A Test...");
32 WriteLine("--------------------------------\n");
33
34 PDFNet.SetColorManagement(CMSType.e_lcms); // Required for PDFA validation.
35
36 //-----------------------------------------------------------
37 // Example 1: PDF/A Validation
38 //-----------------------------------------------------------
39 try
40 {
41 string filename = Path.Combine(InputPath, "newsletter.pdf");
42 PDFACompliance pdf_a = new PDFACompliance(false, filename, "", PDFAComplianceConformance.e_Level1B, null, 10, false);
43 PrintResults(pdf_a, filename);
44 }
45 catch (Exception e)
46 {
47 WriteLine(GetExceptionMessage(e));
48 }
49
50 //-----------------------------------------------------------
51 // Example 2: PDF/A Conversion
52 //-----------------------------------------------------------
53 try
54 {
55 string filename = Path.Combine(InputPath, "fish.pdf");
56 PDFACompliance pdf_a = new PDFACompliance(true, filename, "", PDFAComplianceConformance.e_Level1B, null, 10, false);
57 filename = Path.Combine(OutputPath, "pdfa.pdf");
58 pdf_a.SaveAs(filename, true);
59 WriteLine("Saved PDF/A converted doc to" + filename);
60 await AddFileToOutputList(filename).ConfigureAwait(false);
61
62 // Re-validate the document after the conversion...
63 pdf_a = new PDFACompliance(false, filename, "", PDFAComplianceConformance.e_Level1B, null, 10, false);
64 PrintResults(pdf_a, filename);
65 }
66 catch (Exception e)
67 {
68 WriteLine(GetExceptionMessage(e));
69 }
70
71 WriteLine("\n--------------------------------");
72 WriteLine("Done PDF/A Test.");
73 WriteLine("--------------------------------\n");
74 })).AsAsyncAction();
75 }
76
77 void PrintResults(PDFACompliance pdf_a, String filename)
78 {
79 int err_cnt = pdf_a.GetErrorCount();
80 if (err_cnt == 0)
81 {
82 WriteLine(string.Format("{0}: OK.", filename));
83 }
84 else
85 {
86 WriteLine(string.Format("{0} is NOT a valid PDFA.", filename));
87 for (int i=0; i<err_cnt; ++i)
88 {
89 PDFAComplianceErrorCode c = pdf_a.GetError(i);
90 WriteLine(string.Format(" - e_PDFA{0}: {1}.", c, PDFACompliance.GetPDFAErrorMessage(c)));
91
92 if (true)
93 {
94 int num_refs = pdf_a.GetRefObjCount(c);
95 if (num_refs > 0)
96 {
97 Write(" Objects:");
98 for (int j=0; j<num_refs; )
99 {
100 Write(string.Format("{0}", pdf_a.GetRefObj(c, j)));
101 if (++j != num_refs) Write(", ");
102 }
103 WriteLine("");
104 }
105 }
106 }
107 }
108 }
109 }
110}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales