Convert to PDF/A

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-2024 by Apryse Software Inc. All Rights Reserved.
3' Consult legal.txt regarding legal and license information.
4'---------------------------------------------------------------------------------------
5Imports System
6
7Imports PDFTRON
8Imports PDFTRON.PDF
9Imports PDFTRON.PDF.PDFA
10
11'-----------------------------------------------------------------------------------
12' The sample illustrates how to use PDF/A related API-s.
13'-----------------------------------------------------------------------------------
14
15Module mainModule
16 Dim pdfNetLoader As PDFNetLoader
17 Sub New()
18 pdfNetLoader = pdftron.PDFNetLoader.Instance()
19 End Sub
20
21 ' The main entry point for the application.
22 Sub Main()
23
24 PDFNet.Initialize(PDFTronLicense.Key)
25 PDFNet.SetColorManagement(PDFNet.CMSType.e_lcms) 'Required for PDFA validation.
26
27 'Relative path to the folder containing test files.
28 Dim input_path As String = "../../../../TestFiles/"
29 Dim filename As String = "newsletter.pdf"
30 Dim output_path As String = "../../../../TestFiles/Output/"
31
32
33 '//-----------------------------------------------------------
34 '// Example 1: PDF/A Validation
35 '//-----------------------------------------------------------
36 Try
37 filename = "newsletter.pdf"
38 Dim pdf_a As PDFACompliance = New PDFACompliance(False, input_path + filename, Nothing, PDFACompliance.Conformance.e_Level2B, Nothing, 10, False)
39 PrintResults(pdf_a, filename)
40 pdf_a.Dispose()
41 Catch e As PDFTRON.Common.PDFNetException
42 Console.WriteLine(e.Message)
43 End Try
44
45 '//-----------------------------------------------------------
46 '// Example 2: PDF/A Conversion
47 '//-----------------------------------------------------------
48 Try
49 filename = "fish.pdf"
50 Using pdf_a As PDFACompliance = New PDFACompliance(True, input_path + filename, Nothing, PDFACompliance.Conformance.e_Level2B, Nothing, 10, False)
51 filename = "pdfa.pdf"
52 pdf_a.SaveAs(output_path + filename, False)
53 End Using
54
55 '// Re-validate the document after the conversion...
56 filename = "pdfa.pdf"
57 Using pdf_a As PDFACompliance = New PDFACompliance(False, output_path + filename, Nothing, PDFACompliance.Conformance.e_Level2B, Nothing, 10, False)
58 PrintResults(pdf_a, filename)
59 End Using
60 Catch e As pdftron.Common.PDFNetException
61 Console.WriteLine(e.Message)
62 End Try
63 PDFNet.Terminate()
64 Console.WriteLine("PDFACompliance test completed.")
65 End Sub
66
67 Function PrintResults(ByRef pdf_a As PDFACompliance, ByVal filename As String) As Int32
68 PrintResults = 0
69 Dim err_cnt As Int32 = pdf_a.GetErrorCount()
70 If err_cnt = 0 Then
71 Console.WriteLine("{0}: OK.", filename)
72 Else
73 Dim i As Int32
74 Console.WriteLine("{0} is NOT a valid PDFA.", filename)
75 For i = 0 To err_cnt - 1 Step 1
76 Dim c As PDFACompliance.ErrorCode = pdf_a.GetError(i)
77 Console.WriteLine(" - e_PDFA {0}: {1}.", Int(c), PDFACompliance.GetPDFAErrorMessage(c))
78 If True Then
79 Dim num_refs As Int32 = pdf_a.GetRefObjCount(c)
80 If num_refs > 0 Then
81 Console.Write(" Objects: ")
82 Dim j As Int32
83 For j = 0 To num_refs - 1 Step 1
84 Console.Write("{0}", pdf_a.GetRefObj(c, j))
85 If Not (j + 1) = num_refs Then
86 Console.Write(", ")
87 End If
88 Next j
89 Console.WriteLine()
90 End If
91 End If
92 Next i
93 Console.WriteLine()
94 End If
95 End Function
96
97End Module

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales