Attach ZUGFeRD invoice XML to a PDF

Embed an existing XML invoice in a PDF using Aspose.PDF for C++. The example sets the attachment MIME type and associated-file relationship, adds the XML to the document, and converts the result to PDF/A-3A.

Attach invoice XML and convert to PDF/A-3A

Supply an existing PDF invoice and its XML data. Configure your project to include and link Aspose.PDF for C++, and create the output directory before running the function.

  1. Open the source PDF as a Document.
  2. Create a FileSpecification with the XML file path and a description.
  3. Set the MIME type to text/xml on the FileSpecification.
  4. Set the AFRelationship to Alternative.
  5. Add the attachment with EmbeddedFileCollection::Add() using the key factur.
  6. Derive the conversion log path from the output file path.
  7. Call Document::Convert() with PdfFormat set to PDF_A_3A and ConvertErrorAction set to Delete.
  8. Save the output PDF with Document::Save().

#include <iostream>
#include <Aspose.PDF.Cpp/AFRelationship.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/EmbeddedFileCollection.h>
#include <Aspose.PDF.Cpp/FileSpecification.h>
#include <Aspose.PDF.Cpp/IO/ConvertStrategies/ConvertErrorAction.h>
#include <Aspose.PDF.Cpp/IO/ConvertStrategies/PdfFormat.h>

using namespace System;
using namespace Aspose::Pdf;

void AttachInvoiceZugferdFormat(System::String inputFile, System::String invoiceFile, System::String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    String description = u"Invoice metadata conforming to ZUGFeRD standard";
    auto fileSpecification = MakeObject<FileSpecification>(invoiceFile, description);

    fileSpecification->set_MIMEType(u"text/xml");
    fileSpecification->set_AFRelationship(AFRelationship::Alternative);
    document->get_EmbeddedFiles()->Add(u"factur", fileSpecification);

    String logPath = outputFile.Replace(u".pdf", u"_log.xml");
    document->Convert(logPath, PdfFormat::PDF_A_3A, ConvertErrorAction::Delete);
    document->Save(outputFile);

    std::cout << "ZUGFeRD invoice attached to " << outputFile << std::endl;
}