Convert to PDF/A - Java 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
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.pdf.pdfa.*;
9
10public class PDFATest {
11
12 // Relative path to the folder containing test files.
13 public static final String input_path = "../../TestFiles/";
14 public static final String output_path = "../../TestFiles/Output/";
15
16 public static void main(String[] args) {
17 try{
18 PDFNet.initialize(PDFTronLicense.Key());
19 PDFNet.setColorManagement(PDFNet.e_lcms); // Required for proper PDF/A validation and conversion.
20
21 //-----------------------------------------------------------
22 // Example 1: PDF/A Validation
23 //-----------------------------------------------------------
24
25 String filename = "newsletter.pdf";
26 /* The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
27 of object numbers that are collected for particular error codes. The default value is 10
28 in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs. */
29 PDFACompliance pdf_a = new PDFACompliance(false, input_path + filename, null, PDFACompliance.e_Level2B, null, 10);
30 printResults(pdf_a, filename);
31 pdf_a.destroy();
32 } catch (PDFNetException e) {
33 System.out.println(e.getMessage());
34 }
35
36
37
38 //-----------------------------------------------------------
39 // Example 2: PDF/A Conversion
40 //-----------------------------------------------------------
41 try {
42 String filename = "fish.pdf";
43 PDFACompliance pdf_a = new PDFACompliance(true, input_path + filename, null, PDFACompliance.e_Level2B, null, 10);
44 filename = "pdfa.pdf";
45 pdf_a.saveAs(output_path + filename, false);
46 pdf_a.destroy();
47 // output "pdf_a.pdf"
48
49 // Re-validate the document after the conversion...
50 pdf_a = new PDFACompliance(false, output_path + filename, null, PDFACompliance.e_Level2B, null, 10);
51 printResults(pdf_a, filename);
52 pdf_a.destroy();
53
54 PDFNet.terminate();
55 } catch (PDFNetException e) {
56 System.out.println(e.getMessage());
57 }
58
59 System.out.println("PDFACompliance test completed.");
60 }
61
62 static void printResults(PDFACompliance pdf_a, String filename) {
63 try {
64 int err_cnt = pdf_a.getErrorCount();
65 System.out.print(filename);
66 if (err_cnt == 0) {
67 System.out.print(": OK.\n");
68 } else {
69 System.out.println(" is NOT a valid PDFA.");
70 for (int i = 0; i < err_cnt; ++i) {
71 int c = pdf_a.getError(i);
72 System.out.println(" - e_PDFA " + c + ": " + PDFACompliance.getPDFAErrorMessage(c) + ".");
73 if (true) {
74 int num_refs = pdf_a.getRefObjCount(c);
75 if (num_refs > 0) {
76 System.out.print(" Objects: ");
77 for (int j = 0; j < num_refs; ) {
78 System.out.print(String.valueOf(pdf_a.getRefObj(c, j)));
79 if (++j != num_refs) System.out.print(", ");
80 }
81 System.out.println();
82 }
83 }
84 }
85 System.out.println();
86 }
87 } catch (PDFNetException e) {
88 System.out.println(e.getMessage());
89 }
90 }
91
92}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales