Convert to PDF/UA - Go Sample Code

Sample code for using Apryse SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/UA files. Supports PDF/UA-1. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF/UA Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "testing"
10 "flag"
11 . "github.com/pdftron/pdftron-go/v2"
12)
13
14var licenseKey string
15var modulePath string
16
17func init() {
18 flag.StringVar(&licenseKey, "license", "", "License key for Apryse SDK")
19 flag.StringVar(&modulePath, "modulePath", "", "Module path for Apryse SDK")
20}
21
22//---------------------------------------------------------------------------------------
23// The following sample illustrates how to make sure a file meets the PDF/UA standard, using the PDFUAConformance class object.
24// Note: this feature is currently experimental and subject to change
25//
26// DataExtractionModule is required (Mac users can use StructuredOutputModule instead)
27// https://docs.apryse.com/documentation/core/info/modules/#data-extraction-module
28// https://docs.apryse.com/documentation/core/info/modules/#structured-output-module (Mac)
29//---------------------------------------------------------------------------------------
30
31// Relative path to the folder containing the test files.
32var inputPath = "../TestFiles/"
33var outputPath = "../TestFiles/Output/"
34
35func TestPDFUA(t *testing.T) {
36
37 inputFile1 := inputPath + "autotag_input.pdf"
38 inputFile2 := inputPath + "table.pdf"
39 outputFile1 := outputPath + "autotag_pdfua.pdf"
40 outputFile2 := outputPath + "table_pdfua_linearized.pdf"
41
42 PDFNetInitialize(licenseKey)
43
44 fmt.Println("AutoConverting...")
45
46 PDFNetAddResourceSearchPath(modulePath)
47
48 if !DataExtractionModuleIsModuleAvailable(DataExtractionModuleE_DocStructure) {
49 fmt.Println("")
50 fmt.Println("Unable to run Data Extraction: PDFTron SDK Structured Output module not available.")
51 fmt.Println("-----------------------------------------------------------------------------")
52 fmt.Println("The Data Extraction suite is an optional add-on, available for download")
53 fmt.Println("at https://docs.apryse.com/documentation/core/info/modules/. If you have already")
54 fmt.Println("downloaded this module, ensure that the SDK is able to find the required files")
55 fmt.Println("using the PDFNetAddResourceSearchPath() function.")
56 fmt.Println("")
57 PDFNetTerminate()
58 return
59 }
60
61 pdfua := NewPDFUAConformance()
62
63 fmt.Println("Simple Conversion...")
64
65 // Perform conversion using default options
66 pdfua.AutoConvert(inputFile1, outputFile1)
67
68 fmt.Println("Converting With Options...")
69
70 pdfuaOpts := NewPDFUAOptions()
71 pdfuaOpts.SetSaveLinearized(true) // Linearize when saving output
72 // Note: if file is password protected, you can use pdfuaOpts.SetPassword()
73
74 // Perform conversion using the options we specify
75 pdfua.AutoConvert(inputFile2, outputFile2, pdfuaOpts)
76
77 PDFNetTerminate()
78 fmt.Println("PDFUAConformance test completed.")
79}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales