ZUGFeRD Invoice
TX Text Control supports the embedding of attachments in PDF/A-3b documents and also the extraction of attachments. The helper class TXTextControl.DocumentServer.PDF.Zugferd.Invoice creates a valid ZUGFeRD XML. The method CreateSampleInvoice returns the required XML that is embedded into the created PDF document.
Embedded ZUGFeRD XML:
This demo uses the MVC version of TXTextControl.Web.MVC.DocumentViewer included in TX Text Control .NET Server for ASP.NET. The functionality is compatible across all supported platforms.
Version 32.0 SP4 (NuGet Version 32.4.0)
Sample Source Code
<div class="demo"> | |
<div class="row"> | |
<div class="col-4"> | |
<button onclick="CreateDocument()" class="btn mt-3 btn-success">Create ZUGFeRD</button> | |
<!-- Info area --> | |
<div class="alert alert-info mt-3" role="alert"> | |
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> | |
<span class="alert-text"></span> | |
</div> | |
<div class="mb-5" id="buttonArea"></div> | |
<p>Embedded ZUGFeRD XML:</p> | |
<div id="jsonEditor"></div> | |
</div> | |
<div class="col-8"> | |
<div class="tx-container"> | |
@Html.TXTextControl().DocumentViewer(settings => { | |
settings.Dock = DocumentViewerSettings.DockStyle.Fill; | |
settings.IsSelectionActivated = false; | |
settings.ShowThumbnailPane = false; | |
}).Render() | |
</div> | |
</div> | |
</div> | |
</div> |
private Invoice CreateSampleInvoice() { | |
// new zugferd invoice | |
Invoice invoice = new Invoice("A12345", DateTime.Now, CurrencyCode.USD); | |
invoice.Type = InvoiceType.Invoice; | |
invoice.Profile = TXTextControl.DocumentServer.PDF.Zugferd.Profile.Comfort; | |
// buyer | |
invoice.Buyer = new TradeParty { | |
ID = "TX_1", | |
Name = "Text Control GmbH", | |
ContactName = "Peter Paulsen", | |
City = "Bremen", | |
Postcode = "28217", | |
Country = CountryCode.DE, | |
Street = "Überseetor 18" | |
}; | |
// seller | |
invoice.Seller = new TradeParty { | |
ID = "TX_2", | |
Name = "Text Control, LLC", | |
ContactName = "Jack Jackson", | |
City = "Charlotte, NC", | |
Postcode = "28210", | |
Country = CountryCode.US, | |
Street = "6926 Shannon Willow Rd, Suite 400", | |
}; | |
// add tax id's | |
invoice.Seller.SpecifiedTaxRegistrations.Add( | |
new TaxID() { ID = "US12367623", Scheme = TaxScheme.VA }); | |
// add products | |
List<LineItem> lineItems = new List<LineItem>(); | |
lineItems.Add(new LineItem() { | |
Price = 200, | |
ProductID = "A123", | |
Name = "Product A", | |
Quantity = 5, | |
Total = 1000, | |
UnitCode = QuantityCodes.C62 | |
}); | |
lineItems.Add(new LineItem() { | |
Price = 460, | |
ProductID = "A456", | |
Name = "Product B", | |
Quantity = 2, | |
Total = 920, | |
UnitCode = QuantityCodes.C62 | |
}); | |
// add line items to invoice | |
foreach (LineItem item in lineItems) | |
invoice.LineItems.Add(item); | |
// set the total amount | |
invoice.TotalAmount = 1000; | |
return invoice; | |
} | |
[HttpPost] | |
public ActionResult PDFZugferd() { | |
byte[] bDocument; | |
// create temporary ServerTextControl | |
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { | |
tx.Create(); | |
TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge(); | |
mm.TextComponent = tx; | |
tx.Load(Server.MapPath("~/App_Data/Documents/zugferd.tx"), TXTextControl.StreamType.InternalUnicodeFormat); | |
Invoice invoice = CreateSampleInvoice(); | |
// merge data into template | |
mm.MergeJsonData(JsonConvert.SerializeObject(invoice)); | |
// create the XML | |
string xmlZugferd = invoice.CreateXml(); | |
// get the required meta data | |
string metaData = MetaData.GetMetaData(); | |
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[] { | |
new TXTextControl.EmbeddedFile( | |
"ZUGFeRD-invoice.xml", | |
Encoding.UTF8.GetBytes(xmlZugferd), | |
metaData) }; | |
// export to PDF | |
tx.Save(out bDocument, BinaryStreamType.AdobePDFA, saveSettings); | |
// return as Base64 encoded string | |
return Json(new { Document = Convert.ToBase64String(bDocument), XML = xmlZugferd }); | |
} | |
} |
function CreateDocument() { | |
$('.alert .alert-text').text("Creating ZUGFeRD PDF. Please wait..."); | |
$('.alert').show(); | |
var serviceURL = "@Url.Action("PDFZugferd", "TX")"; | |
// send document to controller | |
$.ajax({ | |
type: "POST", | |
url: serviceURL, | |
contentType: "application/json", | |
success: successFunc, | |
error: errorFunc | |
}); | |
} | |
function successFunc(data, status) { | |
TXDocumentViewer.loadDocument(data.Document, "results.pdf"); | |
createDownloadLink("results.pdf", data.Document); | |
editor.setValue(data.XML, null, 2); | |
$('.alert').hide(); | |
} | |
function errorFunc() { | |
alert("Error"); | |
} | |
function createDownloadLink(name, content) { | |
$("#buttonArea").empty(); | |
var dlink = document.createElement("a"); | |
dlink.download = name; | |
dlink.textContent = "Download PDF"; | |
dlink.classList.add("btn", "mt-3", "btn-primary"); | |
dlink.href = "data:application/pdf;base64," + content; | |
console.log(dlink.href); | |
dlink.onclick = function (e) { | |
var that = this; | |
setTimeout(function () { | |
window.URL.revokeObjectURL(that.href); | |
}, 1500); | |
}; | |
$("#buttonArea").append(dlink); | |
} |
No code required.
© 2025 Copyright Text Control GmbH and Text Control, LLC. All rights reserved. We ♥ documents.