|
<< Click to Display Table of Contents >> Navigation: Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Enhancement > Convert to a ZUGFeRD compatible invoice |
In this section we present example code to convert a PDF invoice into a ZUGFeRD compliant one. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.
The product supports generation of Level B (PDF/A-3b) complaint PDF, and the input PDF should already be PDF/A3 complaint (for example all fonts should be embedded). You can download the input files referenced in the example code from the links given below:
3.PDFA3B_With_ZUGFerd_xml_v_2_2_Sample_Metadata.xml
Given below is the sample code.
string inputXMLMetadataFile = @"PDFA3B_With_ZUGFerd_xml_v_2_2_Sample_Metadata.xml";
string xmlAttachment = @"factur-x.xml";
string inputFileName = @"InputInvoiceForZUGFeRD.pdf";
string outputFileName = @"OutputInvoice_ZUGFeRD.pdf";
using (var pdfDoc = new PDF())
{
pdfDoc.LoadDocument(inputFileName);
// Attach the ZUGFeRD invoice xml
byte[] attachmentFileBytes = File.ReadAllBytes(xmlAttachment);
pdfDoc.AttachDocument(fileName: "factur-x.xml", attachmentFileBytes, lastModifiedDateAndTime: DateTime.Now, description: "Factur-X/ZUGFeRD-Rechnung",
associatedFileRelationship: AFRelationship.Alternative, mimeType: "text/xml", isAssociatedFile: true);
// Optionally, set the PageMode to UseAttachments to show the attachments tab in reader applications such as Adobe Reader when the document is opened.
pdfDoc.PageMode = PDFPageMode.UseAttachments;
// Set the XML metadata
var inputXMLMetadata = File.ReadAllText(inputXMLMetadataFile, Encoding.UTF8);
pdfDoc.SetXMLMetadata(inputXMLMetadata);
// set color profile options such as document's OutputIntent
var colorProfileOptions = new PDFColorProfileOptions
{
DocumentOutputIntent = PDFCmykColorProfile.FromStandard(StandardCmykColorProfile.ISOCoatedV2),
FallbackRgbResource = PDFRgbColorProfile.FromStandard(StandardRgbColorProfile.SRgb2014),
FallbackCmykResource = PDFCmykColorProfile.FromStandard(StandardCmykColorProfile.ISOCoatedV2),
PreserveExistingResources = false
};
pdfDoc.ColorProfileOptions = colorProfileOptions;
// Save the document as PDF/A-3b
pdfDoc.Save(outputFileName);
}