Convert to PDF/A - Go 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-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15//---------------------------------------------------------------------------------------
16// The following sample illustrates how to parse and check if a PDF document meets the
17// PDFA standard, using the PDFACompliance class object.
18//---------------------------------------------------------------------------------------
19
20func PrintResults(pdfa PDFACompliance, filename string){
21 errCnt := pdfa.GetErrorCount()
22 if errCnt == 0{
23 fmt.Println(filename + ": OK.")
24 }else{
25 fmt.Println(filename + " is NOT a valid PDFA.")
26 i := int64(0)
27 for i < errCnt{
28 c := pdfa.GetError(i)
29 str1 := " - e_PDFA " + strconv.Itoa(int(c)) + ": " + PDFAComplianceGetPDFAErrorMessage(c) + "."
30 if true{
31 num_refs := pdfa.GetRefObjCount(c)
32 if num_refs > int64(0){
33 str1 = str1 + "\n Objects: "
34 j := int64(0)
35 for j < num_refs{
36 str1 = str1 + strconv.Itoa(int(pdfa.GetRefObj(c, j)))
37 if j < num_refs-1{
38 str1 = str1 + ", "
39 }
40 j = j + 1
41 }
42 }
43 }
44 fmt.Println(str1)
45 i = i + 1
46 }
47 fmt.Println("")
48 }
49}
50
51func main(){
52 // Relative path to the folder containing the test files.
53 inputPath := "../../TestFiles/"
54 outputPath := "../../TestFiles/Output/"
55
56 PDFNetInitialize(PDFTronLicense.Key)
57 PDFNetSetColorManagement() // Enable color management (required for PDFA validation).
58
59 //-----------------------------------------------------------
60 // Example 1: PDF/A Validation
61 //-----------------------------------------------------------
62 filename := "newsletter.pdf"
63 var cErrorCode PdftronPDFPDFAPDFAComplianceErrorCode
64 // The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
65 // of object numbers that are collected for particular error codes. The default value is 10
66 // in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs.
67 pdfa := NewPDFACompliance(false, inputPath + filename, "", PDFAComplianceE_Level2B, &cErrorCode, 0, 10)
68 PrintResults(pdfa, filename)
69 pdfa.Destroy()
70
71 //-----------------------------------------------------------
72 // Example 2: PDF/A Conversion
73 //-----------------------------------------------------------
74 filename = "fish.pdf"
75 pdfa = NewPDFACompliance(true, inputPath + filename, "", PDFAComplianceE_Level2B, &cErrorCode, 0, 10)
76 filename = "pdfa.pdf"
77 pdfa.SaveAs(outputPath + filename, false)
78 pdfa.Destroy()
79
80 // Re-validate the document after the conversion...
81 pdfa = NewPDFACompliance(false, outputPath + filename, "", PDFAComplianceE_Level2B, &cErrorCode, 0, 10)
82 PrintResults(pdfa, filename)
83 pdfa.Destroy()
84
85 PDFNetTerminate()
86 fmt.Println("PDFACompliance test completed.")
87}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales