Convert to PDF/UA - Java Sample Code

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.

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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales