Passport Loader

Converting PDF to PDF/A standard using .NET and C#

Introduction

In this tutorial, we will look at how to convert a PDF to PDF/A, the ISO standard for the long-term archiving of electronic documents (ISO 19005).

PDF/A format and its use cases

PDF/A is a subset standard of PDF, like PDF/X (for printing), PDF/VT (for variable data & transactional printing), PDF/UA (for accessibility), PDF/E (for engineering), and PDF/R (for scanning).
PDF/A is recommended and sometimes required by companies and organizations for archiving processes.

The PDF/A specification identifies a profile for electronic documents that ensures the documents can be reproduced exactly the same way using various software in years to come.
Here are some key elements of PDF/A conformance:

  • Audio and video content are forbidden.
  • JavaScript and executable file launches are forbidden.
  • All fonts must be embedded and legally embeddable for unlimited, universal rendering.
  • Encryption is forbidden.

The PDF/A standard includes different versions based on the PDF version they rely on.
It also describes various conformance levels according to industry constraints. The a conformance level is for “accessible,” b for “basic,” and u for “Unicode.” In PDF/A-4, the a, b, and u levels are not used. Instead, the requirements and recommendations have been included directly in the base specification. Additionally, PDF/A-4 introduces two new conformance levels, e for “engineering” and f for “embedded files.”

  • PDF/A-1 (a & b), ISO 19005-1:2005 - based on PDF 1.4.
  • PDF/A-2 (a, b & u), ISO 19005-2:2011 - based on PDF 1.7.
  • PDF/A-3 (a, b & u), ISO 19005-3:2012 - based on PDF 1.7.
  • PDF/A-4 (e & f), ISO 19005-4:2020 - based on PDF 2.0.

You can read more about the features of each version and conformance level on the AvePDF Blog articles about PDF/A conversion and PDF/A-4 (the AvePDF web app is built with PassportPDF).

How to use PassportPDF API and .NET to convert a PDF to PDF/A

Now we’re going to cover how to use PassportPDF API to check the version and conformance level of a PDF/A file. We will be using the following endpoints:

  • DocumentLoadFromURIAsync to load a document from a URI.
  • ConvertToPDFAAsync to convert a document to PDF/A with a chosen version and conformance level.
  • SaveDocumentAsync to download the file converted to PDF/A.

The code below illustrates how to use these endpoints to convert a PDF to PDF/A-2a, a version and conformance level frequently used.

using PassportPDF.Api;
using PassportPDF.Client;
using PassportPDF.Model;

namespace PDFAConversion
{

    public class PDFAConverter
    {
        static async Task Main(string[] args)
        {
            GlobalConfiguration.ApiKey = "YOUR-PASSPORT-CODE";

            PassportManagerApi apiManager = new();
            PassportPDFPassport passportData = await apiManager.PassportManagerGetPassportInfoAsync(GlobalConfiguration.ApiKey);

            if (passportData == null)
            {
                throw new ApiException("The Passport number given is invalid, please set a valid passport number and try again.");
            }
            else if (passportData.IsActive is false)
            {
                throw new ApiException("The Passport number given not active, please go to your PassportPDF dashboard and active your plan.");
            }

            string uri = "https://passportpdfapi.com/test/invoice_with_barcode.pdf";

            DocumentApi api = new();

            Console.WriteLine("Loading document into PassportPDF...");
            DocumentLoadResponse document = await api.DocumentLoadFromURIAsync(new LoadDocumentFromURIParameters(uri));
            Console.WriteLine("Document loaded.");

            PDFApi pdfApi = new();


            // Convert PDF to PDF/A format with PDF/A-2a conformance level
            Console.WriteLine("Launching PDF/A conversion process...");
            PdfConvertToPDFAResponse pdfConvertResponse = await pdfApi.ConvertToPDFAAsync(new PdfConvertToPDFAParameters(document.FileId)
            {
                Conformance = PdfAConformance.PDFA2a
            });

            if (pdfConvertResponse.Error is not null)
            {
                throw new ApiException(pdfConvertResponse.Error.ExtResultMessage);
            }
            else
            {
                Console.WriteLine("Conversion process ended.");
            }

            // Download file with PDF/A-2a conformance level
            Console.WriteLine("Downloading PDF/A file...");
            try
            {
                PdfSaveDocumentResponse saveDocResponse = await pdfApi.SaveDocumentAsync(new PdfSaveDocumentParameters(document.FileId));

                string savePath = Path.Join(Directory.GetCurrentDirectory(), "pdfa_file.pdf");

                File.WriteAllBytes(savePath, saveDocResponse.Data);

                Console.WriteLine("Done downloading PDF/A file. Document has been saved in : {0}", savePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not save PDF/A file! : {0}", ex);
            }
        }
    }
}

Final remarks

PassportPDF API makes converting PDF files to any PDF/A version and conformance level seamless, which can help streamline document conversion processes to conform with industry standards.

For more information about the endpoints used in this tutorial, please visit the PassportPDF API reference.