ZugferdInfo.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Linq
Imports System.Reflection
Imports System.Collections.Generic
Imports System.Globalization
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports s2industries.ZUGFeRD
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 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 What is ZUGFeRD?.
''
'' This sample uses the ZUGFeRD-csharp package
'' to parse the ZUGFeRD-compatible XML that is attached to the invoice.
Public Class ZugferdInfo
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' The sample invoice with ZUGFeRD data:
        Dim invoicePdf = Path.Combine("Resources", "PDFs", "zugferd-invoice.pdf")

        '' Text formats for output:
        Dim tfData = New TextFormat() With {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
                .FontSize = 12
            }
        Dim tfStrong = New TextFormat(tfData) With {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuib.ttf"))
            }
        Dim tfLabels = New TextFormat(tfData) With {
                .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuii.ttf")),
                .FontSize = 11
            }
        Dim margin = 36

        '' Output document:
        Dim doc = New GcPdfDocument()
        Using fs = File.OpenRead(invoicePdf)
            '' Load the ZUGFeRD compliant invoice PDF:
            Dim invoice = New GcPdfDocument()
            invoice.Load(fs)

            '' Get the ZUGFeRD attachment from the invoice by the ZUGFeRD 1.x standard file name:
            Dim attachment = invoice.EmbeddedFiles.Values.FirstOrDefault(Function(it) it.File.FileName = "ZUGFeRD-invoice.xml")
            If attachment IsNot Nothing Then
                Using xmlstream = attachment.GetStream()
                    '' Load the invoice descriptor:
                    Dim descriptor = InvoiceDescriptor.Load(xmlstream)
                    Dim culture = CultureInfo.CreateSpecificCulture("en-US")

                    '' Print out the invoice data:
                    Dim page = doc.NewPage()
                    Dim g = page.Graphics
                    Dim 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(Of TabStop) From
                        {
                            New TabStop(margin, TabStopAlignment.Leading),
                            New TabStop(page.Size.Width - margin * 2 - 144, TabStopAlignment.Trailing),
                            New TabStop(page.Size.Width - margin * 2, TabStopAlignment.Trailing)
                        }
                    Dim totals As Decimal = 0
                    Dim index = 1
                    tl.AppendLine($"#{vbTab}Name{vbTab}Quantity{vbTab}Amount", tfLabels)
                    descriptor.TradeLineItems.ForEach(
                        Sub(it)
                            tl.AppendLine($"{index}{vbTab}{it.Name}{vbTab}{Convert.ToInt32(it.BilledQuantity)}{vbTab}{it.LineTotalAmount.Value.ToString("C", culture)}", tfData)
                            index += 1
                            totals += it.LineTotalAmount.Value
                        End Sub
                    )
                    '' The invoice total:
                    tl.AppendLine($"{vbTab}TOTAL:{vbTab}{vbTab}{totals.ToString("C", culture)}", tfStrong)
                    g.DrawTextLayout(tl, PointF.Empty)
                    '' Done:
                    doc.Save(stream)
                    Return doc.Pages.Count
                End Using
            Else
                Return 0
            End If
        End Using
    End Function
End Class