Products Technologies Demo Docs Blog Support Company

Store Documents as PDF/A using C# - A Future-Proof Archiving Format

PDF/A is an international ISO standard for the preservation of electronic and digital documents. This article will explain the reasons and motivations and will show you how to create PDF/A compliant documents in .NET C# applications.

Store Documents as PDF/A using C# - A Future-Proof Archiving Format

At a very early stage, almost 10 years ago, we began to invest in the PDF/A standard for archiving. The goal of the PDF/A format is to guarantee the same rendering even after years of electronic archiving. Some features are disabled: Font linking, Javascript, encryption and transparent layers.

Why is such a standard important? The idea is to provide a document format that will guarantee the readability of your documents, even if the technology changes in the future. Many companies, organizations, and governments use this standard to archive documents or mandate this format for communication and digital transformation.

PDF/A Benefits

Like PDF, PDF/A is a platform-independent format that encapsulates all the information that is necessary for the rendering of the document. For example, when creating PDF/A documents, only embeddable fonts are allowed because all fonts must be embedded. The following features are not allowed:

  • JavaScript and launching executable files are prohibited.
  • Encryption is not allowed.
  • For unlimited, universal rendering, all fonts must be embedded and legally embeddable.
  • Transparent layers are not allowed.
  • Audio and video content is not allowed.

PDF/A vs. PDF/A-3b

PDF/A-1 does not allow embedded files. However, PDF/A-3 allows the embedding of any file format, including XML, which is important for electronic invoices such as ZUGFeRD and Factur X. This enables the integration of archiving processes into the PDF document creation process, such as invoices, as part of the digital transformation.

Learn More

Invoice creation and processing is one of the main applications for Text Control's digital document processing components. This article summarizes the most common scenarios.

Typical Use-Case: Invoice Generation with TX Text Control in C#

Create PDF/A Programmatically

This article describes how to programmatically generate PDF documents from HTML content in a .NET Console App.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

Preparing the Application

For the purposes of this demo, a .NET 6 console application is built.

  1. In Visual Studio, create a new Console App using .NET 6.

  2. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    Create PDF

Adding the Code

  1. Open the Program.cs file and add the following code:

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
    
        TXTextControl.Selection selection = new TXTextControl.Selection();
        selection.Text = "Hello, PDF/A!";
        selection.FontSize = 360;
        selection.FontName = "Arial";
        selection.ForeColor = System.Drawing.Color.Red;
    
        tx.Selection = selection;
    
        tx.Save("output.pdf", TXTextControl.StreamType.AdobePDFA);
    }

Running the Application

  1. Run the application and check the output folder for the generated PDF document.

PDF/A created with TX Text Control

When the file is opened in Acrobat Reader, the application informs the user that the file claims to be PDF/A compliant. Therefore, the file has been opened as read-only. The Standards sidebar informs about the conformance and the used standard (in this case PDF/A-1B).

Restricted Fonts

With PDF/A, all fonts must be part of the document. So what happens if a restricted font is used and the document is to be exported to PDF/A? Let us take a look at the properties of a font that is restricted and cannot be embedded for licensing reasons.

Restricted Font

If this restricted font is used to generate a PDF/A document, an exception is thrown.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    tx.Create();
    
    TXTextControl.Selection selection = new TXTextControl.Selection();
    selection.Text = "Hello, PDF/A!";
    selection.FontSize = 360;
    selection.FontName = "Celtic Garamond the 2nd";
    selection.ForeColor = System.Drawing.Color.Red;

    tx.Selection = selection;

    try
    {
        tx.Save("output.pdf", TXTextControl.StreamType.AdobePDFA);
    } catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

The console output will show the exception:

TXTextControl.FilterException: Text filter error: The document cannot be saved, because it contains a non-embeddable font.
(1-1D0D)

There are two different ways to avoid the exception and do font substitution. The first is automatic replacement of non-embeddable fonts. This can be done by setting the FontSettings.EmbeddableFontsOnly property to true. In this case, TX Text Control will do the font substitution in accordance with a specific mapping table.

Another way to handle substitution is to trap the AdaptFont event, which allows you to define a replacement font.

The following code checks for the unsupported font name and replaces it with Arial.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    tx.Create();

    tx.FontSettings.EmbeddableFontsOnly = true;
    tx.FontSettings.AdaptFontEvent = false;
    tx.AdaptFont += Tx_AdaptFont;

    TXTextControl.Selection selection = new TXTextControl.Selection();
    selection.Text = "Hello, PDF/A!";
    selection.FontSize = 360;
    selection.FontName = "Celtic Garamond the 2nd";
    selection.ForeColor = System.Drawing.Color.Red;

    tx.Selection = selection;

    try
    {
        tx.Save("output.pdf", TXTextControl.StreamType.AdobePDFA);
    } catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

void Tx_AdaptFont(object sender, TXTextControl.AdaptFontEventArgs e)
{
    if (e.FontName == "Celtic Garamond the 2nd")
    {
        e.AdaptedFontName = "Arial";
    }
}

Read the following article to learn more about how TX Text Control can be used to create sophisticated PDF documents.

Learn More

TX Text Control allows developers to create PDF files programmatically using C#. This article shows various ways to create Adobe PDF documents.

Creating PDF Files using TX Text Control .NET in C#

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CorePDF

PDF is Dead, Long Live PDF: Why PDF Remains Relevant in a Changing Digital World

Despite being on the market for more than 30 years, the Portable Document Format (PDF) remains a dominant force in industries such as legal, healthcare, and finance. This blog post addresses the…


ASP.NETASP.NET CorePDF

Create PDF File with .NET C#

This article explains how to use the TX Text Control to create PDF documents in .NET applications, including ASP.NET Core and Windows applications. It covers setting up a console project in Visual…


ASP.NETASP.NET CoreDOCX

Sign Documents with a Self-Signed Digital ID From Adobe Acrobat Reader in…

This article shows how to create a self-signed digital ID using Adobe Acrobat Reader and how to use it to sign documents in .NET C#. The article also shows how to create a PDF document with a…


ASP.NETASP.NET CoreConvert

Programmatically Convert MS Word DOCX Documents to PDF in .NET C#

This article shows how to convert MS Word DOCX documents to PDF in .NET C# using the ServerTextControl component. The example shows how to load a DOCX file from a file or from a variable and how…


ASP.NETGenerative AIOpenAI

Chat PDF - A Generative AI Application for PDF Documents using TX Text…

This article shows how to create a generative AI application for PDF documents using TX Text Control and OpenAI functions in C#. The application uses the OpenAI GPT-3 engine to answer questions on…