ZugferdInfo.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System;using System.IO;using System.Drawing;using System.Linq;using System.Collections.Generic;using System.Globalization;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Text;using s2industries.ZUGFeRD;using GCTEXT = GrapeCity.Documents.Text;using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos{ // This sample demonstrates how to retrieve invoice data from a ZUGFeRD compliant XML attachment. // Selected portions of the fetched invoice data are printed to the PDF generated by this sample. // See ZugferdInfoExt for a similar sample that prints out all ZUGFeRD data.
// The sample PDF invoice containing ZUGFeRD data which this sample uses as input // was generated by ZugferdInvoice. // // ZUGFeRD is a German e-invoicing standard based around PDF and XML file formats. // Its poised to change the way invoices are handled and can be used by any sort of business. // It will make invoice processing more efficient for senders and customers. // For details please see www.ferd-net.de. // // This sample uses the ZUGFeRD-csharp package // to parse the ZUGFeRD-compatible XML that is attached to the invoice. public class ZugferdInfo { public int CreatePDF(Stream stream) { // The sample invoice with ZUGFeRD data: var invoicePdf = Path.Combine("Resources", "PDFs", "zugferd-invoice.pdf");
// Text formats for output: var tfData = new TextFormat() { Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Regular.ttf")), FontSize = 12 }; var tfStrong = new TextFormat(tfData) { Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Bold.ttf")) }; var tfLabels = new TextFormat(tfData) { Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSans-Italic.ttf")), FontSize = 11 }; float margin = 36;
// Output document: var doc = new GcPdfDocument(); using (FileStream fs = File.OpenRead(invoicePdf)) { // Load the ZUGFeRD compliant invoice PDF: var invoice = new GcPdfDocument(); invoice.Load(fs);
// Get the ZUGFeRD attachment from the invoice by the ZUGFeRD 1.x standard file name: var attachment = invoice.EmbeddedFiles.Values.FirstOrDefault(it => it.File.FileName == "ZUGFeRD-invoice.xml"); if (attachment != null) { using (var xmlstream = attachment.GetStream()) { // Load the invoice descriptor: var descriptor = InvoiceDescriptor.Load(xmlstream); var culture = CultureInfo.CreateSpecificCulture("en-US");
// Print out the invoice data: var page = doc.NewPage(); var g = page.Graphics; var tl = g.CreateTextLayout();
tl.MaxWidth = page.Size.Width; tl.MaxHeight = page.Size.Height; tl.MarginAll = margin;
// The invoice header: tl.Append($"Invoice Number: ", tfLabels); tl.AppendLine($"{descriptor.InvoiceNo}", tfData); tl.Append($"Invoice Date: ", tfLabels); tl.AppendLine($"{descriptor.InvoiceDate.Value:yyyy-MM-dd}", tfData); tl.Append($"Seller Name: ", tfLabels); tl.AppendLine($"{descriptor.Seller.Name}", tfData); tl.Append($"Seller Address: ", tfLabels); tl.AppendLine($"{descriptor.Seller.Street}, {descriptor.Seller.City}, {descriptor.Seller.Postcode}, {descriptor.Seller.Country}", tfData); tl.Append($"Buyer Name: ", tfLabels); tl.AppendLine($"{descriptor.BuyerContact.Name} {descriptor.Buyer.Name}", tfData); tl.Append($"Buyer Address: ", tfLabels); tl.AppendLine($"{descriptor.Buyer.Street}, {descriptor.Buyer.City}, {descriptor.Buyer.Postcode}, {descriptor.Buyer.Country}", tfData); tl.AppendLine();
// The invoice positions: tl.TabStops = new List<TabStop>() { new TabStop(margin, TabStopAlignment.Leading), new TabStop(page.Size.Width - margin * 2 - 144, TabStopAlignment.Trailing), new TabStop(page.Size.Width - margin * 2, TabStopAlignment.Trailing), }; decimal totals = 0; var index = 1; tl.AppendLine("#\tName\tQuantity\tAmount", tfLabels); descriptor.TradeLineItems.ForEach(it => { tl.AppendLine($"{index++}\t{it.Name}\t{Convert.ToInt32(it.BilledQuantity)}\t{it.LineTotalAmount.Value.ToString("C", culture)}", tfData); totals += it.LineTotalAmount.Value; }); // The invoice total: tl.AppendLine($"\tTOTAL:\t\t{totals.ToString("C", culture)}", tfStrong);
g.DrawTextLayout(tl, PointF.Empty);
// Done: doc.Save(stream); return doc.Pages.Count; } } else return 0; } } }}