Convert to PDF/UA - C# (.Net) 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//
4
5using System;
6using pdftron;
7using pdftron.SDF;
8using pdftron.PDF;
9using pdftron.PDF.PDFUA;
10
11//---------------------------------------------------------------------------------------
12// The following sample illustrates how to make sure a file meets the PDF/UA standard, using the PDFUAConformance class object.
13// Note: this feature is currently experimental and subject to change
14//
15// DataExtractionModule is required (Mac users can use StructuredOutputModule instead)
16// https://docs.apryse.com/documentation/core/info/modules/#data-extraction-module
17// https://docs.apryse.com/documentation/core/info/modules/#structured-output-module (Mac)
18//---------------------------------------------------------------------------------------
19namespace PDFUATestCS
20{
21 class PDFUATest
22 {
23 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
24 static PDFUATest() {}
25
26 // Relative path to the folder containing test files.
27 static string input_path = "../../../../TestFiles/";
28 static string output_path = "../../../../TestFiles/Output/";
29
30 // DataExtraction library location, replace if desired, should point to a folder that includes the contents of <DataExtractionModuleRoot>/Lib.
31 // If using default, unzip the DataExtraction zip to the parent folder of Samples, and merge with existing "Lib" folder
32 static string extraction_module_path = "../../../../../Lib/";
33
34
35 /// <summary>
36 /// The main entry point for the application.
37 /// </summary>
38 [STAThread]
39 static void Main(string[] args)
40 {
41 try {
42 PDFNet.Initialize(PDFTronLicense.Key);
43
44 String input_file1 = input_path + "autotag_input.pdf";
45 String input_file2 = input_path + "table.pdf";
46 String output_file1 = output_path + "autotag_pdfua.pdf";
47 String output_file2 = output_path + "table_pdfua_linearized.pdf";
48
49 //-----------------------------------------------------------
50 // Example: PDF/UA Conversion
51 //-----------------------------------------------------------
52 Console.WriteLine("AutoConverting...");
53
54 PDFNet.AddResourceSearchPath(extraction_module_path);
55 if(!DataExtractionModule.IsModuleAvailable(DataExtractionModule.DataExtractionEngine.e_doc_structure))
56 {
57 Console.Error.WriteLine("Unable to run PDFUATest: Apryse SDK Data Extraction module not available.");
58 Console.Error.WriteLine("---------------------------------------------------------------");
59 Console.Error.WriteLine("The Data Extraction module is an optional add-on, available for download");
60 Console.Error.WriteLine("at https://apryse.com/. If you have already downloaded this");
61 Console.Error.WriteLine("module, ensure that the SDK is able to find the required files");
62 Console.Error.WriteLine("using the PDFNet::AddResourceSearchPath() function.");
63 Console.Error.WriteLine("");
64 return;
65 }
66
67 PDFUAConformance pdf_ua = new PDFUAConformance();
68
69 Console.WriteLine("Simple Conversion...");
70 {
71 // Perform conversion using default options
72 pdf_ua.AutoConvert(input_file1, output_file1);
73 }
74
75 Console.WriteLine("Converting With Options...");
76 {
77 PDFUAOptions pdf_ua_opts = new PDFUAOptions();
78 pdf_ua_opts.SetSaveLinearized(true); // Linearize when saving output
79 // Note: if file is password protected, you can use pdf_ua_opts.setPassword()
80
81 // Perform conversion using the options we specify
82 pdf_ua.AutoConvert(input_file2, output_file2, pdf_ua_opts);
83 }
84
85 }
86 catch (pdftron.Common.PDFNetException e)
87 {
88 Console.Error.WriteLine(e.GetMessage());
89 Environment.Exit(-1);
90 }
91 finally
92 {
93 PDFNet.Terminate();
94 Console.WriteLine("PDFUAConformance test completed.");
95 }
96 }
97 }
98}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales