Convert to PDF/UA - C++ Sample Code

Requirements
View Demo

Sample code for using Apryse SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/UA files. Supports PDF/UA-1. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Implementation steps

To convert to PDF/UA with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework
Step 2: Download the Data Extraction Module
Step 3: Add the sample code provided in this guide

To use this feature in production, your license key will need the Accessibility Package. Trial keys already include all packages.

Learn more about our Server SDK and PDF/UA Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5#include <iostream>
6#include <PDF/PDFNet.h>
7#include <PDF/DataExtractionModule.h>
8#include "../../LicenseKey/CPP/LicenseKey.h"
9#include <PDF/PDFUA/PDFUAConformance.h>
10#include <PDF/PDFUA/PDFUAOptions.h>
11using namespace std;
12using namespace pdftron;
13using namespace pdftron::PDF;
14using namespace pdftron::PDF::PDFUA;
15//---------------------------------------------------------------------------------------
16// The following sample illustrates how to make sure a file meets the PDF/UA standard, using the PDFUAConformance class object.
17// Note: this feature is currently experimental and subject to change
18//
19// DataExtractionModule is required (Mac users can use StructuredOutputModule instead)
20// https://docs.apryse.com/documentation/core/info/modules/#data-extraction-module
21// https://docs.apryse.com/documentation/core/info/modules/#structured-output-module (Mac)
22//---------------------------------------------------------------------------------------
23int main(int argc, char *argv[])
24{
25 // Relative path to the folder containing test files.
26 static UString input_path("../../TestFiles/");
27 static UString output_path("../../TestFiles/Output/");
28 // DataExtraction library location, replace if desired, should point to a folder that includes the contents of <DataExtractionModuleRoot>/Lib.
29 // If using default, unzip the DataExtraction zip to the parent folder of Samples, and merge with existing "Lib" folder.
30 static UString extraction_module_path("../../../Lib/");
31 UString input_file1 = input_path + "autotag_input.pdf";
32 UString input_file2 = input_path + "table.pdf";
33 UString output_file1 = output_path + "autotag_pdfua.pdf";
34 UString output_file2 = output_path + "table_pdfua_linearized.pdf";
35 int ret = 0;
36 try
37 {
38 PDFNet::Initialize(LicenseKey);
39 cout << "AutoConverting..." << endl;
40 PDFNet::AddResourceSearchPath(extraction_module_path);
41 if (!DataExtractionModule::IsModuleAvailable(DataExtractionModule::e_DocStructure))
42 {
43 cout << endl;
44 cout << "Unable to run PDFUATest: Apryse SDK Data Extraction module not available." << endl;
45 cout << "---------------------------------------------------------------" << endl;
46 cout << "The Data Extraction module is an optional add-on, available for download" << endl;
47 cout << "at https://apryse.com/. If you have already downloaded this" << endl;
48 cout << "module, ensure that the SDK is able to find the required files" << endl;
49 cout << "using the PDFNet::AddResourceSearchPath() function." << endl << endl;
50 return 1;
51 }
52 PDFUAConformance pdf_ua;
53 cout << "Simple Conversion..." << endl;
54 {
55 // Perform conversion using default options
56 pdf_ua.AutoConvert(input_file1, output_file1);
57 }
58 cout << "Converting With Options..." << endl;
59 {
60 PDFUAOptions pdf_ua_opts;
61 pdf_ua_opts.SetSaveLinearized(true); // Linearize when saving output
62 // Note: if file is password protected, you can use pdf_ua_opts.SetPassword()
63 // Perform conversion using the options we specify
64 pdf_ua.AutoConvert(input_file2, output_file2, pdf_ua_opts);
65 }
66 }
67 catch (Common::Exception& e)
68 {
69 cout << e << endl;
70 ret = 1;
71 }
72 catch (...) {
73 cout << "Unknown Exception" << endl;
74 ret = 1;
75 }
76 cout << "PDFUAConformance test completed." << endl;
77 PDFNet::Terminate();
78 return ret;
79}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales