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
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 Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales