ZugferdInfo.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using GrapeCity.Documents.Pdf;
  13. using GrapeCity.Documents.Text;
  14. using s2industries.ZUGFeRD;
  15. using GCTEXT = GrapeCity.Documents.Text;
  16. using GCDRAW = GrapeCity.Documents.Drawing;
  17.  
  18. namespace DsPdfWeb.Demos
  19. {
  20. // This sample demonstrates how to retrieve invoice data from a ZUGFeRD compliant XML attachment.
  21. // Selected portions of the fetched invoice data are printed to the PDF generated by this sample.
  22. // See ZugferdInfoExt for a similar sample that prints out all ZUGFeRD data.
  23.  
  24. // The sample PDF invoice containing ZUGFeRD data which this sample uses as input
  25. // was generated by ZugferdInvoice.
  26. //
  27. // ZUGFeRD is a German e-invoicing standard based around PDF and XML file formats.
  28. // Its poised to change the way invoices are handled and can be used by any sort of business.
  29. // It will make invoice processing more efficient for senders and customers.
  30. // For details please see What is ZUGFeRD?.
  31. //
  32. // This sample uses the ZUGFeRD-csharp package
  33. // to parse the ZUGFeRD-compatible XML that is attached to the invoice.
  34. public class ZugferdInfo
  35. {
  36. public int CreatePDF(Stream stream)
  37. {
  38. // The sample invoice with ZUGFeRD data:
  39. var invoicePdf = Path.Combine("Resources", "PDFs", "zugferd-invoice.pdf");
  40.  
  41. // Text formats for output:
  42. var tfData = new TextFormat() {
  43. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
  44. FontSize = 12
  45. };
  46. var tfStrong = new TextFormat(tfData) {
  47. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuib.ttf"))
  48. };
  49. var tfLabels = new TextFormat(tfData) {
  50. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuii.ttf")),
  51. FontSize = 11
  52. };
  53. float margin = 36;
  54.  
  55. // Output document:
  56. var doc = new GcPdfDocument();
  57. using (FileStream fs = File.OpenRead(invoicePdf))
  58. {
  59. // Load the ZUGFeRD compliant invoice PDF:
  60. var invoice = new GcPdfDocument();
  61. invoice.Load(fs);
  62.  
  63. // Get the ZUGFeRD attachment from the invoice by the ZUGFeRD 1.x standard file name:
  64. var attachment = invoice.EmbeddedFiles.Values.FirstOrDefault(it => it.File.FileName == "ZUGFeRD-invoice.xml");
  65. if (attachment != null)
  66. {
  67. using (var xmlstream = attachment.GetStream())
  68. {
  69. // Load the invoice descriptor:
  70. var descriptor = InvoiceDescriptor.Load(xmlstream);
  71. var culture = CultureInfo.CreateSpecificCulture("en-US");
  72.  
  73. // Print out the invoice data:
  74. var page = doc.NewPage();
  75. var g = page.Graphics;
  76. var tl = g.CreateTextLayout();
  77.  
  78. tl.MaxWidth = page.Size.Width;
  79. tl.MaxHeight = page.Size.Height;
  80. tl.MarginAll = margin;
  81.  
  82. // The invoice header:
  83. tl.Append($"Invoice Number: ", tfLabels);
  84. tl.AppendLine($"{descriptor.InvoiceNo}", tfData);
  85. tl.Append($"Invoice Date: ", tfLabels);
  86. tl.AppendLine($"{descriptor.InvoiceDate.Value:yyyy-MM-dd}", tfData);
  87. tl.Append($"Seller Name: ", tfLabels);
  88. tl.AppendLine($"{descriptor.Seller.Name}", tfData);
  89. tl.Append($"Seller Address: ", tfLabels);
  90. tl.AppendLine($"{descriptor.Seller.Street}, {descriptor.Seller.City}, {descriptor.Seller.Postcode}, {descriptor.Seller.Country}", tfData);
  91. tl.Append($"Buyer Name: ", tfLabels);
  92. tl.AppendLine($"{descriptor.BuyerContact.Name} {descriptor.Buyer.Name}", tfData);
  93. tl.Append($"Buyer Address: ", tfLabels);
  94. tl.AppendLine($"{descriptor.Buyer.Street}, {descriptor.Buyer.City}, {descriptor.Buyer.Postcode}, {descriptor.Buyer.Country}", tfData);
  95. tl.AppendLine();
  96.  
  97. // The invoice positions:
  98. tl.TabStops = new List<TabStop>()
  99. {
  100. new TabStop(margin, TabStopAlignment.Leading),
  101. new TabStop(page.Size.Width - margin * 2 - 144, TabStopAlignment.Trailing),
  102. new TabStop(page.Size.Width - margin * 2, TabStopAlignment.Trailing),
  103. };
  104. decimal totals = 0;
  105. var index = 1;
  106. tl.AppendLine("#\tName\tQuantity\tAmount", tfLabels);
  107. descriptor.TradeLineItems.ForEach(it =>
  108. {
  109. tl.AppendLine($"{index++}\t{it.Name}\t{Convert.ToInt32(it.BilledQuantity)}\t{it.LineTotalAmount.Value.ToString("C", culture)}", tfData);
  110. totals += it.LineTotalAmount.Value;
  111. });
  112. // The invoice total:
  113. tl.AppendLine($"\tTOTAL:\t\t{totals.ToString("C", culture)}", tfStrong);
  114.  
  115. g.DrawTextLayout(tl, PointF.Empty);
  116.  
  117. // Done:
  118. doc.Save(stream);
  119. return doc.Pages.Count;
  120. }
  121. }
  122. else
  123. return 0;
  124. }
  125. }
  126. }
  127. }
  128.