Convert to PDF/A - C++ Sample Code

Sample code for using Apryse Server 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). Code available in 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// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7//#include <PDF/PDFDoc.h>
8#include <PDF/PDFA/PDFACompliance.h>
9#include <string>
10#include <iostream>
11#include "../../LicenseKey/CPP/LicenseKey.h"
12
13using namespace std;
14using namespace pdftron;
15using namespace pdftron::PDF;
16using namespace pdftron::PDF::PDFA;
17
18void PrintResults(PDFACompliance& pdf_a, UString filename)
19{
20 int err_cnt = static_cast<int>(pdf_a.GetErrorCount());
21 if (err_cnt == 0)
22 {
23 cout << filename << ": OK.\n";
24 }
25 else
26 {
27 cout << filename << " is NOT a valid PDFA.\n";
28 for (int i=0; i<err_cnt; ++i)
29 {
30 PDFACompliance::ErrorCode c = pdf_a.GetError(i);
31 cout << " - e_PDFA " << c << ": " << PDFACompliance::GetPDFAErrorMessage(c) << ".\n";
32 if (true)
33 {
34 int num_refs = static_cast<int>(pdf_a.GetRefObjCount(c));
35 if (num_refs > 0)
36 {
37 cout << " Objects: ";
38 for (int j=0; j<num_refs; ++j)
39 {
40 cout << pdf_a.GetRefObj(c, j);
41 if (j<num_refs-1)
42 cout << ", ";
43 }
44 cout << endl;
45 }
46 }
47 }
48 cout << endl;
49 }
50}
51
52
53
54//---------------------------------------------------------------------------------------
55// The following sample illustrates how to parse and check if a PDF document meets the
56// PDFA standard, using the PDFACompliance class object.
57//---------------------------------------------------------------------------------------
58int main(int argc, char *argv[])
59{
60 int ret = 0;
61 UString input_path("../../TestFiles/");
62 UString output_path("../../TestFiles/Output/");
63 PDFNet::Initialize(LicenseKey);
64 PDFNet::SetColorManagement(); // Enable color management (required for PDFA validation).
65
66 //-----------------------------------------------------------
67 // Example 1: PDF/A Validation
68 //-----------------------------------------------------------
69 try
70 {
71 UString filename("newsletter.pdf");
72 /* The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
73 of object numbers that are collected for particular error codes. The default value is 10
74 in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs. */
75 PDFACompliance pdf_a(false, input_path+filename, 0, PDFACompliance::e_Level2B, 0, 0, 10);
76 PrintResults(pdf_a, filename);
77 }
78 catch (Common::Exception& e)
79 {
80 cout << e << endl;
81 ret = 1;
82 }
83 catch (...) {
84 cout << "Unknown Exception" << endl;
85 ret = 1;
86 }
87
88 //-----------------------------------------------------------
89 // Example 2: PDF/A Conversion
90 //-----------------------------------------------------------
91 try
92 {
93 UString filename("fish.pdf");
94 PDFACompliance pdf_a(true, input_path+filename, 0, PDFACompliance::e_Level2B, 0, 0, 10);
95 filename = "pdfa.pdf";
96 pdf_a.SaveAs(output_path + filename);
97
98 // Re-validate the document after the conversion...
99 PDFACompliance comp(false, output_path + filename, 0, PDFACompliance::e_Level2B, 0, 0, 10);
100 PrintResults(comp, filename);
101 }
102 catch (Common::Exception& e)
103 {
104 cout << e << endl;
105 ret = 1;
106 }
107 catch (...) {
108 cout << "Unknown Exception" << endl;
109 ret = 1;
110 }
111
112 cout << "PDFACompliance test completed." << endl;
113 PDFNet::Terminate();
114 return ret;
115}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales