ShippingLabels.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Barcode
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 此示例打印一组包含一些条形码的运输标签。
'' 另请注意使用制表位来垂直对齐数据。
Public Class ShippingLabels
    '' 客户信息。此示例为每位客户打印一个运输标签。
    Private Class Client
        Public Property Name As String
        Public Property Addr As String
        Public Property City As String
        Public Property Country As String
        Sub New(ByVal name_ As String, ByVal addr_ As String, ByVal city_ As String, ByVal country_ As String)
            Name = name_
            Addr = addr_
            City = city_
            Country = country_
        End Sub
    End Class

    '' 客户群。
    Shared ReadOnly s_clients As New List(Of Client) From
    {
        New Client("Simons bistro", "Vinb�ltet 34", "K�benhavn", "Denmark"),
        New Client("Richter Supermarkt", "Starenweg 5", "Gen�ve", "Switzerland"),
        New Client("Bon app'", "12, rue des Bouchers", "Marseille", "France"),
        New Client("Rattlesnake Canyon Grocery", "2817 Milton Dr.", "Albuquerque", "USA"),
        New Client("Lehmanns Marktstand", "Magazinweg 7", "Frankfurt a.M.", "Germany"),
        New Client("LILA-Supermercado", "Carrera 52 con Ave. Bol�dim #65-98 Llano Largo", "Barquisimeto", "Venezuela"),
        New Client("Ernst Handel", "Kirchgasse 6", "Graz", "Austria"),
        New Client("Pericles Comidas cl�sicas", "Calle Dr. Jorge Cash 321", "M�xico D.F.", "Mexico"),
        New Client("Drachenblut Delikatessen", "Walserweg 21", "Aachen", "Germany"),
        New Client("Queen Cozinha", "Alameda dos Can�rios, 891", "S�o Paulo", "Brazil"),
        New Client("Tortuga Restaurante", "Avda. Azteca 123", "M�xico D.F.", "Mexico"),
        New Client("Save-a-lot Markets", "187 Suffolk Ln.", "Boise", "USA"),
        New Client("Franchi S.p.A.", "Via Monte Bianco 34", "Torino", "Italy")
    }

    '' 用于呈现标签的 consts 和 vars:
    Const hmargin = 24.0F, vmargin = 36.0F
    Dim _labelWidth, _labelHeight As Single
    Dim _pBold, _pNorm As GCDRAW.Pen
    Dim _fontReg, _fontBold As GCTEXT.Font
    Dim _tfSmall, _tfSmallB, _tfLarge As TextFormat
    Dim _tsHeader, _tsFrom, _tsCodes As List(Of TabStop)
    Dim _logo As GCDRAW.Image
    Dim _ia As ImageAlign
    Dim bcTop, bcBottom As GcBarcode

    '' 主要示例驱动程序。
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Init(doc)
        '' 循环客户端,每页最多打印 4 个标签:
        Dim i = 0
        For pg = 0 To ((s_clients.Count + 3) / 4) - 1
            Dim page = doc.NewPage()
            PrintLabel(s_clients(i), page, New RectangleF(hmargin, vmargin, _labelWidth, _labelHeight))
            i += 1
            If (i < s_clients.Count) Then
                PrintLabel(s_clients(i), page, New RectangleF(hmargin + _labelWidth, vmargin, _labelWidth, _labelHeight))
                i += 1
            End If
            If (i < s_clients.Count) Then
                PrintLabel(s_clients(i), page, New RectangleF(hmargin, vmargin + _labelHeight, _labelWidth, _labelHeight))
                i += 1
            End If
            If (i < s_clients.Count) Then
                PrintLabel(s_clients(i), page, New RectangleF(hmargin + _labelWidth, vmargin + _labelHeight, _labelWidth, _labelHeight))
                i += 1
            End If
        Next
        '' 完毕:
        doc.Save(stream)
        Term()
        Return doc.Pages.Count
    End Function

    '' 用于渲染标签的初始化变量:
    Private Sub Init(ByVal doc As GcPdfDocument)
        _labelWidth = (doc.PageSize.Width - hmargin * 2) / 2
        _labelHeight = _labelWidth
        _pBold = New GCDRAW.Pen(Color.Black, 2)
        _pNorm = New GCDRAW.Pen(Color.Black, 0.5F)
        _logo = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "AcmeLogo-vertical-250px.png"))
        _fontReg = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"))
        _fontBold = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeuib.ttf"))
        _tfSmall = New TextFormat() With {.Font = _fontReg, .FontSize = 8}
        _tfSmallB = New TextFormat() With {.Font = _fontBold, .FontSize = 8}
        _tfLarge = New TextFormat() With {.Font = _fontBold, .FontSize = 14}

        _ia = New ImageAlign(ImageAlignHorz.Right, ImageAlignVert.Center, True, True, True, False, False)
        _tsHeader = New List(Of TabStop) From {New TabStop(24, TabStopAlignment.Leading), New TabStop(108, TabStopAlignment.Leading)}
        _tsFrom = New List(Of TabStop) From {New TabStop(12, TabStopAlignment.Leading), New TabStop(72, TabStopAlignment.Leading)}
        _tsCodes = New List(Of TabStop) From {New TabStop(_labelWidth / 8, TabStopAlignment.Center)}

        bcTop = New GcBarcode() With {
                .TextFormat = _tfSmall,
                .CodeType = CodeType.Code_128_B,
                .HorizontalAlignment = ImageAlignHorz.Center,
                .VerticalAlignment = ImageAlignVert.Center
            }
        bcTop.Options.CaptionPosition = BarCodeCaptionPosition.Below
        bcBottom = New GcBarcode() With {
            .TextFormat = _tfSmall,
            .CodeType = CodeType.Code_128auto,
            .HorizontalAlignment = ImageAlignHorz.Center,
            .VerticalAlignment = ImageAlignVert.Center
        }
        bcBottom.Options.CaptionPosition = BarCodeCaptionPosition.Below
    End Sub

    Private Sub Term()
        _logo.Dispose()
    End Sub

    Private Sub PrintLabel(ByVal client As Client, ByVal page As Page, ByVal bounds As RectangleF)
        '' 用于随机化一些样本数据:
        Dim rnd = Util.NewRandom()

        '' 托运人/发件人数据示例:
        Dim shipper = If(rnd.Next(2) = 0, "United Package", "Speedy Express")

        Dim sender = New With {
            .name = "ACME Inc.", .addr = "1 Main Street", .city = "Metropolis", .country = "USA", .zip = "34567"
        }

        Dim shuttle = rnd.Next(10000, 15000).ToString()

        Dim area = rnd.Next(1, 12).ToString()
        Dim tour = rnd.Next(0, 20).ToString()

        Dim g = page.Graphics

        Dim tl = g.CreateTextLayout()
        tl.DefaultFormat.Font = _tfSmall.Font
        tl.DefaultFormat.FontSize = _tfSmall.FontSize
        tl.LineSpacingScaleFactor = 0.75F
        tl.ParagraphAlignment = ParagraphAlignment.Center

        '' 标头
        Dim hHeader = bounds.Height / 2 / 5
        Dim rHeader = New RectangleF(bounds.X, bounds.Y, bounds.Width, hHeader)
        tl.TabStops = _tsHeader
        tl.Append($"{vbTab}{shipper}")
        tl.Append($"{vbTab}CMR", _tfSmallB)
        tl.Append($"{vbLf}{vbTab}{vbTab}{Util.TimeNow():yyyy-MM-dd}")
        tl.MaxHeight = rHeader.Height
        tl.MaxWidth = rHeader.Width
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, rHeader.Location)

        Dim rLogo = rHeader
        rLogo.Inflate(-5, -5)
        g.DrawImage(_logo, rLogo, Nothing, _ia)

        '' 从
        Dim hFrom = hHeader
        Dim rFrom = New RectangleF(bounds.X, rHeader.Bottom, bounds.Width, hFrom)
        tl.Clear()
        tl.TabStops = _tsFrom
        tl.Append($"{vbTab}From:{vbTab}{sender.name}{vbLf}{vbTab}{vbTab}{sender.addr}{vbLf}{vbTab}{vbTab}{sender.city}, {sender.country} {sender.zip}")
        tl.MaxHeight = rFrom.Height
        tl.MaxWidth = rFrom.Width
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, rFrom.Location)

        '' 到
        Dim hTo = bounds.Height / 2 / 3
        Dim rTo = New RectangleF(bounds.X, rFrom.Bottom, bounds.Width, hTo)
        tl.Clear()
        tl.TabStops = _tsFrom
        tl.Append($"{vbTab}To:{vbTab}{client.Name}{vbLf}{vbTab}{vbTab}{client.Addr}{vbLf}{vbTab}{vbTab}{client.City}{vbLf}{vbTab}{vbTab}{client.Country}")
        tl.MaxHeight = rTo.Height
        tl.MaxWidth = rTo.Width
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, rTo.Location)

        '' 代码
        Dim hCodes = bounds.Height / 2 / (15.0F / 4)
        Dim rCodes = New RectangleF(bounds.X, rTo.Bottom, bounds.Width, hCodes)
        tl.TabStops = _tsCodes
        tl.Clear()
        tl.AppendLine($"{vbTab}Shuttle")
        tl.Append($"{vbTab}{shuttle}", _tfLarge)
        tl.MaxHeight = rCodes.Height
        tl.MaxWidth = rCodes.Width / 4
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, rCodes.Location)

        tl.Clear()
        tl.AppendLine($"{vbTab}Area")
        tl.Append($"{vbTab}{area}", _tfLarge)
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4, rCodes.Y))

        tl.Clear()
        tl.AppendLine($"{vbTab}Exception")
        tl.Append($"{vbTab} ", _tfLarge)
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4 * 2, rCodes.Y))

        tl.Clear()
        tl.AppendLine($"{vbTab}Tour")
        tl.Append($"{vbTab}{tour}", _tfLarge)
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, New PointF(rCodes.X + rCodes.Width / 4 * 3, rCodes.Y))

        '' 条形码
        Dim hBarcodes = bounds.Height / 2
        Dim rBcTop = New RectangleF(bounds.X, rCodes.Bottom, bounds.Width, hBarcodes / 2)
        Dim rBcBottom = New RectangleF(bounds.X, rBcTop.Bottom, bounds.Width, hBarcodes / 2)

        bcTop.Text = client.Country
        g.DrawBarcode(bcTop, rBcTop)

        '' 编写一个较长的“代码”:
        Dim code = $"{client.Name(0)}{client.Addr(0)}{client.City(0)}{client.Country(0)}".ToUpper()
        bcBottom.Text = $"{code}{client.GetHashCode().ToString("X12")}"
        g.DrawBarcode(bcBottom, rBcBottom)

        '' 线路:
        g.DrawLine(rHeader.Left, rHeader.Bottom, rHeader.Right, rHeader.Bottom, _pNorm)
        g.DrawLine(rFrom.Left, rFrom.Bottom, rFrom.Right, rFrom.Bottom, _pNorm)
        g.DrawLine(rTo.Left, rTo.Bottom, rTo.Right, rTo.Bottom, _pNorm)
        g.DrawLine(rCodes.Left, rCodes.Bottom, rCodes.Right, rCodes.Bottom, _pNorm)

        g.DrawLine(rCodes.Left + rCodes.Width / 4, rCodes.Top, rCodes.Left + rCodes.Width / 4, rCodes.Bottom, _pNorm)
        g.DrawLine(rCodes.Left + rCodes.Width / 4 * 2, rCodes.Top, rCodes.Left + rCodes.Width / 4 * 2, rCodes.Bottom, _pNorm)
        g.DrawLine(rCodes.Left + rCodes.Width / 4 * 3, rCodes.Top, rCodes.Left + rCodes.Width / 4 * 3, rCodes.Bottom, _pNorm)

        g.DrawRectangle(bounds, _pBold)
    End Sub
End Class