ZugferdInfo.vb
'' 完毕:
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

'' 此示例演示如何从 ZUGFeRD 兼容的 XML 附件中检索发票数据。
'' 所获取的发票数据的选定部分将打印到此示例生成的 PDF 中。
'' See ZugferdInfoExt 以获取打印所有 ZUGFeRD 数据的类似示例。

'' 包含 ZUGFeRD 数据的示例 PDF 发票,该示例将其用作输入
'' 由ZugferdInvoice生成​​。
'' 
'' ZUGFeRD 是基于 PDF 和 XML 文件格式的德国电子发票标准。
'' 它准备改变发票的处理方式,并可供任何类型的企业使用。
'' 它将使发件人和客户的发票处理更加高效。
'' 详细信息请参见什么是ZUGFeRD? 。
'' 
'' 此示例​​使用 ZUGFeRD-csharp 包
'' 解析附加到发票的 ZUGFeRD 兼容 XML。
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