Articles in this section
Category / Section

How to embed ZUGFeRD information in ASP.NET Core PDF document?

4 mins read

The Syncfusion Essential® PDF is a feature-rich and high performance ASP.NET CorePDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can add ZUGFeRD information in an existing PDF document using C#.

Steps to add ZUGFeRD information in an existing PDF document programmatically:

  1. Create a new console application project.Screenshot (1337).png
  2. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference to your console application from Nuget.org.
    Screenshot (1521).png
  3. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using SkiaSharp;
using Syncfusion.Pdf.Interactive;
  1. Use the following code sample in Program.cs to add a ZUGFeRD compliance in an existing PDF document.

C#

//Load an existing PDF document
FileStream fileStream = new FileStream(@"../../../Invoice.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
//Subscripe the SubstitueFont event for supplying font. 
loadedDocument.SubstituteFont += LoadedDocument_SubstituteFont;
//Convert the existing PDF to PDF/A-3B document.  
loadedDocument.ConvertToPDFA(Syncfusion.Pdf.PdfConformanceLevel.Pdf_A3B);

MemoryStream stream = new MemoryStream();
//Save and close the document
loadedDocument.Save(stream);
loadedDocument.Close(true);

//Load the converted PDF stream
loadedDocument = new PdfLoadedDocument(stream);
//Create a new PDF document with A3B
PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A3B);
//Merge the existing document
PdfDocumentBase.Merge(document, loadedDocument);
//Set the ZUGFeRD conformance level
document.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic;
//Set the ZUGFeRD version
document.ZugferdVersion = ZugferdVersion.ZugferdVersion2_0;

//Get stream from the XML file
FileStream invoiceStream = new FileStream(Path.GetFullPath("../../../ZUGFeRD_invoice.xml"), FileMode.Open, FileAccess.Read);

//Create an attachment
PdfAttachment attachment = new PdfAttachment("zugferd-invoice.xml", invoiceStream);
//Add the attachment relationship
attachment.Relationship = PdfAttachmentRelationship.Alternative;
//Set modification date
attachment.ModificationDate = DateTime.Now;
//Set description and mime type
attachment.Description = "zugferd-invoice";
attachment.MimeType = "text/xml";

//Add the attachment to the document. 
document.Attachments.Add(attachment);
MemoryStream ms = new MemoryStream();
//Save and close the new PDF document
document.Save(ms);
document.Close(true);
//Close the loaded document
loadedDocument.Close(true);
//Save the PDF file in disk
File.WriteAllBytes("ZUGFeRD_PDF.pdf", ms.ToArray());


//Add the event to supply the missing fonts
static void LoadedDocument_SubstituteFont(object sender, PdfFontEventArgs args)
{
   //get the font name
   string fontName = args.FontName.Split(',')[0];
   //get the font style
   PdfFontStyle fontStyle = args.FontStyle;
   SKFontStyle sKFontStyle = SKFontStyle.Normal;

   if (fontStyle != PdfFontStyle.Regular)
   {
       if (fontStyle == PdfFontStyle.Bold)
       {
           sKFontStyle = SKFontStyle.Bold;
       }
       else if (fontStyle == PdfFontStyle.Italic)
       {
           sKFontStyle = SKFontStyle.Italic;
       }
       else if (fontStyle == (PdfFontStyle.Italic | PdfFontStyle.Bold))
       {
           sKFontStyle = SKFontStyle.BoldItalic;
       }
   }
   //Get the font stream using Skiasharp
   SKTypeface typeface = SKTypeface.FromFamilyName(fontName, sKFontStyle);
   SKStreamAsset typeFaceStream = typeface.OpenStream();
   MemoryStream memoryStream = null;
   if (typeFaceStream != null && typeFaceStream.Length > 0)
   {
       //Create the fontData from the type face stream.	 
       byte[] fontData = new byte[typeFaceStream.Length - 1];
       typeFaceStream.Read(fontData, typeFaceStream.Length);
       typeFaceStream.Dispose();
       //Create the new memory stream from the font data.	 
       memoryStream = new MemoryStream(fontData);
   }
   //set the font stream to the event args.	
   args.FontStream = memoryStream;
}

A complete working sample can be downloaded from Adding_ZUGFeRD_properties.zip

By executing the program, you will get the PDF document as follows.Screenshot (1524).png

Take a moment to peruse the documentation. You will also find other options like create PDF/A-1b and PDF/X-1a document and features like converting Word to PDF, Excel to PDF, and HTML to PDF with code examples.

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Conclusion

I hope you enjoyed learning about how to embed ZUGFeRD information in ASP.NET Core PDF document.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied