SupportedBarcodes.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
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 GcWebCommon

'' Renders samples of all barcode symbologies supported by the GcBarcode library.
Public Class SupportedBarcodes
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page As Page = Nothing
        Dim g As GcGraphics = Nothing
        Const margin = 72.0F / 2
        Const pad = 4.0F
        Const gap = 10.0F
        Dim ip = New PointF(margin, margin)

        Dim newPage As Action =
            Sub()
                page = doc.NewPage()
                g = page.Graphics
                ip = New PointF(margin, margin)
            End Sub

        newPage()

        Dim tfCaption = New TextFormat() With {
                .Font = StandardFonts.Times,
                .FontSize = 12
            }
        Dim tfBarcode = New TextFormat() With {
            .Font = StandardFonts.Helvetica,
            .FontSize = 9
        }
        Dim barcode = New GcBarcode() With {
            .TextFormat = tfBarcode,
            .ScaleFactor = 1.5F
        }
        barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below
        barcode.Options.SizeOptions.NarrowWideRatio = 0

        Dim drawBarcode As Action(Of CodeType, String, String) =
            Sub(ct_, txt_, txt2_)
                Dim caption = $"{ct_}:{vbCrLf}{txt_}"
                If String.IsNullOrEmpty(txt2_) Then
                    barcode.Options.GS1Composite.Type = GS1CompositeType.None
                Else
                    caption += $"{vbCrLf}Dependent CCA: {txt2_}"
                    barcode.Options.GS1Composite.Type = GS1CompositeType.CCA
                    barcode.Options.GS1Composite.Value = txt2_
                End If

                barcode.Options.CheckSumEnabled = ct_ <> CodeType.Code25intlv AndAlso ct_ <> CodeType.Code_2_of_5 AndAlso ct_ <> CodeType.Matrix_2_of_5
                Dim csize = g.MeasureString(caption, tfCaption)
                barcode.CodeType = ct_
                barcode.Text = txt_
                Dim size = g.MeasureBarcode(barcode)
                size.Height = Math.Max(size.Height, csize.Height)
                Dim border = New RectangleF(ip, New SizeF(page.Size.Width - margin * 2, size.Height + pad * 2))
                If ip.Y + border.Height > page.Size.Height - margin Then
                    newPage()
                    border = New RectangleF(ip, border.Size)
                End If
                g.DrawRectangle(border, Color.Gray)
                g.DrawString(caption, tfCaption, New PointF(border.Left + pad, border.Top + pad))
                g.DrawBarcode(barcode, New RectangleF(border.Right - size.Width - pad, border.Top + pad, size.Width, size.Height))
                ip.Y = border.Bottom + gap
            End Sub
        ''
        drawBarcode(CodeType.Ansi39, "*GCBARCODE*", Nothing)
        drawBarcode(CodeType.Ansi39x, "*GcPdf*", Nothing)
        drawBarcode(CodeType.Codabar, "A12041961D", Nothing)
        drawBarcode(CodeType.Code25intlv, "1234567890", Nothing) '' Interleaved 2 of 5 (ITF)
        drawBarcode(CodeType.Code39, "*GCBARCODE*", Nothing)
        drawBarcode(CodeType.Code39x, "*GcPdf*", Nothing)
        drawBarcode(CodeType.Code49, "GcBarcode+GcPdf", Nothing)
        drawBarcode(CodeType.Code93x, "GcBarcode+GcPdf", Nothing)
        drawBarcode(CodeType.Code_93, "GCBARCODE", Nothing)
        drawBarcode(CodeType.Code_128_A, "GCPDF-2017", Nothing)
        drawBarcode(CodeType.Code_128_B, "GcPdf-2017", Nothing)
        drawBarcode(CodeType.Code_128_C, "1234567890", Nothing)
        drawBarcode(CodeType.Code_128auto, "GcPdf-2017", Nothing)
        drawBarcode(CodeType.Code_2_of_5, "1234567890", Nothing)
        drawBarcode(CodeType.DataMatrix, "GcBarcode+GcPdf", Nothing)
        drawBarcode(CodeType.QRCode, "GcBarcode+GcPdf", Nothing)
        drawBarcode(CodeType.EAN_8, "1234567", Nothing)
        drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", Nothing)
        drawBarcode(CodeType.EAN128FNC1, $"GcBarcode{vbLf}GcPdf", Nothing)
        drawBarcode(CodeType.IntelligentMail, "00300999999000000001", Nothing)
        drawBarcode(CodeType.JapanesePostal, "TOKYO-10CC-09-1978", Nothing)
        drawBarcode(CodeType.PostNet, "152063949", Nothing)
        drawBarcode(CodeType.RM4SCC, "SE17PB9Z", Nothing)
        drawBarcode(CodeType.Matrix_2_of_5, "1234567890", Nothing)
        drawBarcode(CodeType.MSI, "1234567890", Nothing)
        drawBarcode(CodeType.MicroPDF417, "GcPdf", Nothing)
        drawBarcode(CodeType.Pdf417, "GcPdf", Nothing)
        drawBarcode(CodeType.RSS14, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Stacked, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Stacked, "1234567890", "12345")
        drawBarcode(CodeType.RSS14StackedOmnidirectional, "1234567890", Nothing)
        drawBarcode(CodeType.RSS14Truncated, "1234567890", Nothing)
        drawBarcode(CodeType.RSSExpanded, "12345678901234", Nothing)
        drawBarcode(CodeType.RSSExpandedStacked, "12345678901234", Nothing)
        drawBarcode(CodeType.RSSLimited, "1234567890", Nothing)
        drawBarcode(CodeType.RSSLimited, "1234567890", "12345")
        drawBarcode(CodeType.UCCEAN128, "GcBarcode+GcPdf", Nothing)
        drawBarcode(CodeType.UPC_A, "123456789012", Nothing)
        drawBarcode(CodeType.UPC_E0, "123456789012", Nothing)
        drawBarcode(CodeType.UPC_E1, "123456789012", Nothing)
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class