Print some basic data from a ZUGFeRD compliant invoice

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