PDF/A

Sample Java code for using Apryse 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). Learn more about our Android SDK and PDF/A Library. A command-line tool for batch conversion and validation is also available.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.PDFNetException;
13import com.pdftron.pdf.PDFNet;
14import com.pdftron.pdf.pdfa.PDFACompliance;
15
16import java.util.ArrayList;
17
18public class PDFATest extends PDFNetSample {
19
20 private static OutputListener mOutputListener;
21
22 private static ArrayList<String> mFileList = new ArrayList<>();
23
24 public PDFATest() {
25 setTitle(R.string.sample_pdfa_title);
26 setDescription(R.string.sample_pdfa_description);
27
28 // The standard library does not include PDF/A validation/conversion,
29 // thus this sample will fail. Please, comment out this call
30 // if using the full libraries.
31 // DisableRun();
32 }
33
34 @Override
35 public void run(OutputListener outputListener) {
36 super.run(outputListener);
37 mOutputListener = outputListener;
38 mFileList.clear();
39 printHeader(outputListener);
40 try{
41 PDFNet.setColorManagement(PDFNet.e_lcms); // Required for proper PDF/A validation and conversion.
42
43 //-----------------------------------------------------------
44 // Example 1: PDF/A Validation
45 //-----------------------------------------------------------
46
47 String filename = "newsletter.pdf";
48 /* The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
49 of object numbers that are collected for particular error codes. The default value is 10
50 in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs. */
51 PDFACompliance pdf_a = new PDFACompliance(false, Utils.getAssetTempFile(INPUT_PATH + filename).getAbsolutePath(), null, PDFACompliance.e_Level2B, null, 10);
52 printResults(pdf_a, filename);
53 pdf_a.destroy();
54 } catch (PDFNetException e) {
55 System.out.println(e.getMessage());
56 }
57
58
59
60 //-----------------------------------------------------------
61 // Example 2: PDF/A Conversion
62 //-----------------------------------------------------------
63 try {
64 String filename = "fish.pdf";
65 PDFACompliance pdf_a = new PDFACompliance(true, Utils.getAssetTempFile(INPUT_PATH + filename).getAbsolutePath(), null, PDFACompliance.e_Level2B, null, 10);
66 filename = "pdfa.pdf";
67 pdf_a.saveAs(Utils.createExternalFile(filename, mFileList).getAbsolutePath(), false);
68 pdf_a.destroy();
69 // output "pdf_a.pdf"
70
71 // Re-validate the document after the conversion...
72 pdf_a = new PDFACompliance(false, Utils.createExternalFile(filename, mFileList).getAbsolutePath(), null, PDFACompliance.e_Level2B, null, 10);
73 printResults(pdf_a, filename);
74 pdf_a.destroy();
75
76 } catch (PDFNetException e) {
77 System.out.println(e.getMessage());
78 }
79
80 mOutputListener.println("PDFACompliance test completed.");
81
82 for (String file : mFileList) {
83 addToFileList(file);
84 }
85 printFooter(outputListener);
86 }
87
88
89 static void printResults(PDFACompliance pdf_a, String filename) {
90 try {
91 int err_cnt = pdf_a.getErrorCount();
92 mOutputListener.print(filename);
93 if (err_cnt == 0) {
94 mOutputListener.print(": OK.\n");
95 } else {
96 mOutputListener.println(" is NOT a valid PDFA.");
97 for (int i = 0; i < err_cnt; ++i) {
98 int c = pdf_a.getError(i);
99 mOutputListener.println(" - e_PDFA " + c + ": " + PDFACompliance.getPDFAErrorMessage(c) + ".");
100 if (true) {
101 int num_refs = pdf_a.getRefObjCount(c);
102 if (num_refs > 0) {
103 mOutputListener.print(" Objects: ");
104 for (int j = 0; j < num_refs; ) {
105 mOutputListener.print(String.valueOf(pdf_a.getRefObj(c, j)));
106 if (++j != num_refs) mOutputListener.print(", ");
107 }
108 mOutputListener.println();
109 }
110 }
111 }
112 mOutputListener.println();
113 }
114 } catch (PDFNetException e) {
115 System.out.println(e.getMessage());
116 }
117 }
118
119}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales