Creating PDF/3-A compliant PDF and attaching ZUGFeRD invoice in C#

Attach ZUGFeRD to PDF

The following code snippet also work with Aspose.PDF.Drawing library.

We recommend following steps to attach ZUGFeRD to PDF:

  • Define a path variable that points to a folder where the input and output PDF files are located.
  • Create a document object by loading an existing PDF file (e.g. “ZUGFeRD-test.pdf”) from the path.
  • Create a FileSpecification object by providing the path and description of another file named “factur-x.xml”, which contains invoice metadata conforming to the ZUGFeRD standard.
  • Add properties to the file specification object, such as the description, MIME type, and AF relationship. The AF relationship indicates how the embedded file is related to the PDF document. In this case, it is set to “Alternative”, meaning the embedded file is an alternative representation of the PDF content.
  • Add the file specification object to the document’s embedded files collection. File name should be specified to ZUGFeRD standard, e.g. “factur-x.xml”.
  • Convert the document to PDF/A-3B format, a subset of PDF that ensures the long-term preservation of electronic documents. PDF/A-3B allows embedding files of any format in PDF documents.
  • Save the converted document as a new PDF file (e.g. “ZUGFeRD-res.pdf”).
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AttachZUGFeRD()
{
    // The path to the documents directory
    string dataDir = RunExamples.GetDataDir_AsposePdf();
    // Open document
    using (var document = new Aspose.Pdf.Document(dataDir + "ZUGFeRD-test.pdf"))
    {
        // Setup new file to be added as attachment
        var description = "Invoice metadata conforming to ZUGFeRD standard";
        var fileSpecification = new Aspose.Pdf.FileSpecification(dataDir + "factur-x.xml", description)
        {
            Description = "Zugferd",
            MIMEType = "text/xml",
            Name = "factur-x.xml"
        };
        // Add attachment to document's attachment collection
        document.EmbeddedFiles.Add(fileSpecification);
        document.Convert(new MemoryStream(), Aspose.Pdf.PdfFormat.ZUGFeRD, Aspose.Pdf.ConvertErrorAction.Delete);
        // Save document
        document.Save(dataDir + "ZUGFeRD-res.pdf");
    }
}

The convert method takes a stream, a PDF format, and a convert error action as parameters. The stream parameter can be used to save the conversion log. The convert error action parameter specifies what to do if any errors occur during the conversion. In this case, it is set to “Delete”, which means that any elements that are not compliant with the PDF/A-3B format will be deleted from the document.