SupportedBarcodes.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.Collections.Generic
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Barcode

'' Renders samples of all barcode symbologies supported by the DsBarcode library.
Public Class SupportedBarcodes
    Function CreatePDF(ByVal stream As Stream) As Integer
        '' Simple layout parameters:
        Const margin = 72.0F / 2
        Const pad = 4.0F
        Const gap = 10.0F
        ''
        Dim doc = New GcPdfDocument()
        Dim page As Page = Nothing
        Dim g As GcGraphics = Nothing
        Dim ip As PointF
        '' This will add a new page and set the insertion point initial position:
        Dim newPage As Action =
            Sub()
                page = doc.NewPage()
                g = page.Graphics
                ip = New PointF(margin, margin)
            End Sub
        newPage()
        '' Text formats for showing barcode names and values:
        Dim tfCaption = New TextFormat() With {
                .Font = StandardFonts.Times,
                .FontSize = 12
            }
        Dim tfBarcode = New TextFormat() With {
            .Font = StandardFonts.Helvetica,
            .FontSize = 9
        }
        '' The barcode instance to be used to draw all symbologies,
        '' here we set up its properties that won't change:
        Dim barcode = New GcBarcode() With {
            .TextFormat = tfBarcode,
            .ScaleFactor = 1.5F
        }
        barcode.Options.CaptionPosition = BarCodeCaptionPosition.Below
        barcode.Options.SizeOptions.NarrowWideRatio = 0
        '' Draw a barcode with the specified symbology (code type) and optional CC-A:
        Dim drawBarcode As Action(Of CodeType, String, String) =
            Sub(symboloty, value, cca)
                Dim caption = $"{symboloty}:{vbCrLf}{value}"
                If String.IsNullOrEmpty(cca) Then
                    barcode.Options.GS1Composite.Type = GS1CompositeType.None
                Else
                    '' Composite Component A (CC-A) specified:
                    caption += $"{vbCrLf}Dependent CCA: {cca}"
                    barcode.Options.GS1Composite.Type = GS1CompositeType.CCA
                    barcode.Options.GS1Composite.Value = cca
                End If
                '' Not all barcodes support checksums:
                barcode.Options.CheckSumEnabled = symboloty <> CodeType.Code25intlv AndAlso symboloty <> CodeType.Code_2_of_5 AndAlso symboloty <> CodeType.Matrix_2_of_5
                Dim csize = g.MeasureString(caption, tfCaption)
                barcode.CodeType = symboloty
                barcode.Text = value
                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))
                '' Add new pages as needed:
                If ip.Y + border.Height > page.Size.Height - margin Then
                    newPage()
                    border = New RectangleF(ip, border.Size)
                End If
                '' Draw the barcode:
                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
        '' Draw all supported symbologies:
        drawBarcode(CodeType.Ansi39, "*DSBARCODE*", Nothing)
        drawBarcode(CodeType.Ansi39x, "*DsPdf*", Nothing)
        drawBarcode(CodeType.Codabar, "A12041961D", Nothing)
        drawBarcode(CodeType.Code25intlv, "1234567890", Nothing) '' Interleaved 2 of 5 (ITF)
        drawBarcode(CodeType.Code39, "*GCBARCODE*", Nothing)
        drawBarcode(CodeType.Code39x, "*DsPdf*", Nothing)
        drawBarcode(CodeType.Code49, "DsBarcode+DsPdf", Nothing)
        drawBarcode(CodeType.Code93x, "DsBarcode+DsPdf", Nothing)
        drawBarcode(CodeType.Code_93, "GCBARCODE", Nothing)
        drawBarcode(CodeType.Code_128_A, "DSPDF-2023", Nothing)
        drawBarcode(CodeType.Code_128_B, "DSPdf-2023", Nothing)
        drawBarcode(CodeType.Code_128_C, "1234567890", Nothing)
        drawBarcode(CodeType.Code_128auto, "DsPdf-2023", Nothing)
        drawBarcode(CodeType.Code_2_of_5, "1234567890", Nothing)
        drawBarcode(CodeType.DataMatrix, "DsBarcode+DsPdf", Nothing)
        drawBarcode(CodeType.QRCode, "DsBarcode+DsPdf", Nothing)
        drawBarcode(CodeType.EAN_8, "1234567", Nothing)
        drawBarcode(CodeType.EAN_13, "469" + "87654" + "3210", Nothing)
        drawBarcode(CodeType.EAN128FNC1, $"GcBarcode{vbLf}DsPdf", 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, "DsPdf", Nothing)
        drawBarcode(CodeType.Pdf417, "DsPdf", 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, "DsBarcode+DsPdf", 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