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}
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
18class PDFATest : PDFNetSample() {
19 init {
20 setTitle(R.string.sample_pdfa_title)
21 setDescription(R.string.sample_pdfa_description)
22
23 // The standard library does not include PDF/A validation/conversion,
24 // thus this sample will fail. Please, comment out this call
25 // if using the full libraries.
26 // DisableRun();
27 }
28
29 override fun run(outputListener: OutputListener?) {
30 super.run(outputListener)
31 mOutputListener = outputListener
32 mFileList.clear()
33 printHeader(outputListener!!)
34 try {
35 PDFNet.setColorManagement(PDFNet.e_lcms) // Required for proper PDF/A validation and conversion.
36
37 //-----------------------------------------------------------
38 // Example 1: PDF/A Validation
39 //-----------------------------------------------------------
40
41 val filename = "newsletter.pdf"
42 /* The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
43 of object numbers that are collected for particular error codes. The default value is 10
44 in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs. */
45 val pdf_a = PDFACompliance(false, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + filename)!!.absolutePath, null, PDFACompliance.e_Level2B, null, 10)
46 printResults(pdf_a, filename)
47 pdf_a.destroy()
48 } catch (e: PDFNetException) {
49 println(e.message)
50 }
51
52 //-----------------------------------------------------------
53 // Example 2: PDF/A Conversion
54 //-----------------------------------------------------------
55 try {
56 var filename = "fish.pdf"
57 var pdf_a = PDFACompliance(true, Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + filename)!!.absolutePath, null, PDFACompliance.e_Level2B, null, 10)
58 filename = "pdfa.pdf"
59 pdf_a.saveAs(Utils.createExternalFile(filename, mFileList).absolutePath, false)
60 pdf_a.destroy()
61
62 // Re-validate the document after the conversion...
63 pdf_a = PDFACompliance(false, Utils.createExternalFile(filename, mFileList).absolutePath, null, PDFACompliance.e_Level2B, null, 10)
64 printResults(pdf_a, filename)
65 pdf_a.destroy()
66
67 } catch (e: PDFNetException) {
68 println(e.message)
69 }
70
71 mOutputListener!!.println("PDFACompliance test completed.")
72
73 for (file in mFileList) {
74 addToFileList(file)
75 }
76 printFooter(outputListener)
77 }
78
79 companion object {
80
81 private var mOutputListener: OutputListener? = null
82
83 private val mFileList = ArrayList<String>()
84
85 internal fun printResults(pdf_a: PDFACompliance, filename: String) {
86 try {
87 val err_cnt = pdf_a.errorCount
88 mOutputListener!!.print(filename)
89 if (err_cnt == 0) {
90 mOutputListener!!.print(": OK.\n")
91 } else {
92 mOutputListener!!.println(" is NOT a valid PDFA.")
93 for (i in 0 until err_cnt) {
94 val c = pdf_a.getError(i)
95 mOutputListener!!.println(" - e_PDFA " + c + ": " + PDFACompliance.getPDFAErrorMessage(c) + ".")
96 if (true) {
97 val num_refs = pdf_a.getRefObjCount(c)
98 if (num_refs > 0) {
99 mOutputListener!!.print(" Objects: ")
100 var j = 0
101 while (j < num_refs) {
102 mOutputListener!!.print(pdf_a.getRefObj(c, j).toString())
103 if (++j != num_refs) mOutputListener!!.print(", ")
104 }
105 mOutputListener!!.println()
106 }
107 }
108 }
109 mOutputListener!!.println()
110 }
111 } catch (e: PDFNetException) {
112 println(e.message)
113 }
114
115 }
116 }
117
118}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales