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 for ASP.NET.
Preparing the Application
For the purposes of this demo, a .NET 6 console application is built.
-
In Visual Studio, create a new Console App using .NET 6.
-
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
Adding the Code
-
Open the Program.cs file and add the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing (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
-
Run the application and check the output folder for the generated PDF document.
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.
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 Font ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ FontSettings Class
╰ EmbeddableFontsOnly Property
Gets or sets a value specifying that only embeddable fonts can be used in a document. 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 Adapt ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
╰ AdaptFont Event
Occurs for each font that must be adapted, because it is not supported. 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.