ExpertPdf Anywhere ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Anywhere - ZUGFeRD / Factur-X Electronic Invoice - C# / ASP.NET Sample

A ZUGFeRD or Factur-X invoice is a hybrid invoice: a PDF/A-3 document that people read, with the same invoice embedded as structured XML that accounting software reads without retyping. This sample converts an HTML invoice to PDF/A-3 and embeds its Factur-X XML (profile EN 16931) with Document.AddZugferdInvoice.

The invoice is PDF/A-3a, which is also tagged and accessible.

Invoice page:

C# code of this sample

Pages/ZugferdInvoice.cshtml.cs
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.Anywhere.HtmlToPdf;
using ExpertPdf.Anywhere.PdfDocument;

namespace ExpertPdf.Samples.Pages
{
    public class ZugferdInvoiceModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = Helper.WWWRoot(Request) + Url.Content("~/files/invoice.html");
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        public IActionResult OnPost()
        {
            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };
            // converter.LicenseKey = "your license key";

            converter.PdfDocumentOptions.LeftMargin = 20;
            converter.PdfDocumentOptions.RightMargin = 20;
            converter.PdfDocumentOptions.TopMargin = 20;
            converter.PdfDocumentOptions.BottomMargin = 20;
            converter.PdfDocumentInfo.Title = "Invoice F-2026-1017";
            converter.PdfDocumentInfo.AuthorName = "Alpine Data Systems GmbH";

            // ZUGFeRD / Factur-X invoices are PDF/A-3; this one is also tagged and accessible (PDF/A-3a)
            converter.PdfStandardSubset = PdfStandardSubset.Pdf_A_3a;
            converter.Language = "en-US";

            // convert the invoice page to a Document object
            Document doc = converter.GetPdfDocumentObjectFromUrl(TxtUrl);

            // embed the invoice XML as factur-x.xml and write the Factur-X metadata; the profile must match the XML
            // (EN 16931, also called COMFORT) and Alternative says the XML is another form of the visible invoice
            string xmlFile = Path.Combine(Startup.WebRootPath, "files", "factur-x.xml");
            doc.AddZugferdInvoice(xmlFile, ZugferdProfile.En16931, AttachmentRelationship.Alternative);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Invoice-F-2026-1017.pdf";
            return fileResult;
        }
    }
}