At the end of March, the German Federal Council (Bundesrat) passed a new law that makes it mandatory for companies to receive electronic invoices. The receipt of electronic invoices will be mandatory for domestic B2B transactions from January 1, 2025. The sending of electronic invoices is also mandatory, but there are exceptions and transitional rules that apply.

In addition to being more environmentally friendly than paper invoices, e-invoicing offers many benefits to businesses. For example, e-invoices can be processed automatically for time and cost savings. Moreover, electronic invoices are easier to archive and retrieve than paper invoices.

Electronic invoices are also safer and more secure than paper invoices. They are protected from unauthorized access and cannot be lost or damaged in the mail. To ensure authenticity and integrity, electronic invoices can also be digitally signed.

Creating Electronic Invoices

Electronic invoices can be created in various formats, such as ZUGFeRD, XRechnung, and Factur-X. These formats are based on the XML standard and can be read and processed by machines. This makes it easier for companies to automate their invoicing processes and reduce the risk of errors.

TX Text Control provides all the basic technologies required to create these PDF documents, as it fully supports PDF/A-3b, the required format for electronic invoices.

XRechnung or ZUGFeRD?

XRechnung is a pure XML format for processing digital invoices. ZUGFeRD combines the human-readable PDF format with a machine-readable XML format. Since the release of ZUGFeRD version 2.1, both the XML resulting from XRechnung and the way the XML is integrated into a PDF file can be combined.

A sample application discussed in another article implements the XRechnung class, which holds all the data needed to create the invoice, and also uses the ZUGFeRD-csharp library to generate the necessary XML.

Learn More

This article shows how to create valid XRechnung and ZUGFeRD invoices with ASP.NET Core C#. The invoice is created with the help MailMerge component and the ZUGFeRD-csharp library.

Creating Valid XRechnung / ZUGFeRD Invoices with ASP.NET Core C#

The following C# code shows how to embed the ZUGFeRD XML in a document and export it to a PDF/A file:

string xmlZugferd = xRechnung.CreateXML();
// load Xml file
string metaData = File.ReadAllText("metadata.xml");
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings();
// create a new embedded file
var zugferdInvoice = new TXTextControl.EmbeddedFile(
"ZUGFeRD-invoice.xml",
Encoding.UTF8.GetBytes(xmlZugferd),
metaData);
zugferdInvoice.Description = "ZUGFeRD-invoice";
zugferdInvoice.Relationship = "Alternative";
zugferdInvoice.MIMEType = "application/xml";
zugferdInvoice.LastModificationDate = DateTime.Now;
// set the embedded files
saveSettings.EmbeddedFiles = new TXTextControl.EmbeddedFile[] { zugferdInvoice };
view raw test.cs hosted with ❤ by GitHub

A template is merged with the XRechnung object to generate the human readable version of the invoice.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
tx.Create();
// load the document
tx.Load("template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
// merge the data
using (MailMerge mm = new MailMerge())
{
mm.TextComponent = tx;
mm.MergeObject(xRechnung);
}
// save the document
tx.Save("test.pdf", TXTextControl.StreamType.AdobePDFA, saveSettings);
}
view raw test.cs hosted with ❤ by GitHub

Opening the created PDF document in Acrobat Reader displays the visual representation and embedded XML in the Attachments pane:

ZUGFeRD XML Attachment

Extracting ZUGFeRD XML from PDF

TX Text Control provides a feature to extract attachments from PDF documents.

The following GetXmlAttachment method uses TX Text Control to load the PDF document using the Load TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
ServerTextControl Class
Load Method
Loads text in a certain format.
method. The EmbeddedFiles TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
LoadSaveSettingsBase Class
EmbeddedFiles Property
Specifies an array of EmbeddedFile objects which will be embedded in the saved document.
property of the LoadSettings contains an array of EmbeddedFile TX Text Control .NET Server for ASP.NET
TXTextControl Namespace
EmbeddedFile Class
The EmbeddedFile class represents a file embedded in another document.
objects after the PDF is loaded.

private string GetXmlAttachment(string Filename) {
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// load documment
LoadSettings ls = new TXTextControl.LoadSettings() {
PDFImportSettings = PDFImportSettings.LoadEmbeddedFiles
};
tx.Load(Filename, TXTextControl.StreamType.AdobePDF, ls);
// all attachments
var embeddedFiles = ls.EmbeddedFiles;
// find the "alternative" xml representation
foreach (EmbeddedFile embeddedFile in embeddedFiles) {
if (embeddedFile.Relationship == "Alternative" &&
embeddedFile.MIMEType == "text/xml") {
// return converted XML
return Encoding.UTF8.GetString((byte[])embeddedFile.Data);
}
}
return null; //something went wrong
}
}
view raw test.cs hosted with ❤ by GitHub

Each attachment is checked against the Relationship and MIMEType requirements to return the associated embedded XML document.

To extract the alternative XML invoice from a PDF/A-3b document, the above method can be called as in the code below:

var xml = GetXmlAttachment("facturx_invoice_pdfa3b_01.pdf");
view raw test.cs hosted with ❤ by GitHub

Free Consulting

Do you have any questions about electronic invoicing or how to create electronic invoices with TX Text Control? Contact us for a free consultation. We are happy to help you with your project.

Contact Us

Conclusion

Electronic invoicing will become mandatory in Germany in 2025. Companies that are not yet prepared for this change should start planning now. TX Text Control provides all the necessary tools to create electronic invoices in various formats, such as ZUGFeRD and XRechnung. With TX Text Control, you can automate your invoicing processes and reduce the risk of errors.