Adding Barcodes

Brief

The article describes how to add and configure barcodes.

Details

You can add the most widely used UPC-A, EAN-13, EAN-8 and GS1-128 barcodes to your documents. Pass a sequence of digits (or Latin letters and symbols in the case of GS1-128), specify the barcode type, and other settings if necessary, and the library will automatically encode the barcode and generate the barcode image.

Barcodes can be added to paragraphs, sections, table cells, and repeating areas.

To add a barcode, use one of the methods of the BarcodesUserAPI class:

By default, barcode lines are black on a white background, and the code data is printed under the barcode lines.

The barcodes have the following settings:

Barcode width

The default barcode width is defined by the type of the barcode. Barcodes are generated as required by the international standards. For example, an EAN-13 barcode with the line width = 1 module (1X) will have an empty left area of 11X, the left delimiter of 3X, the left part of the code containing six digits of 7X each, which makes 42X in total, a delimiter of 5X, the right part of the code of 42X, the right delimiter of 3X, the right empty area of 7X. As a result, the total width is 113X.

So if you do not specify the width for an EAN-13 barcode, it will be 113 pixels by default. You can specify your width in pixels, as for images and inline images, in the width parameter of the AddBarcode() and AddBarcodeToParagraph() methods.

For EAN-8 barcodes, the barcode width is 81X. So if you need to have the line width equal to 1 pixel, you should set the barcode width to 81.

Also, you can configure the barcode width and height, borders, margins and paddings using the methods of the BarcodeInlineBuilder and BarcodeBuilder classes, for example SetWidth, SetMargins, SetPaddings, SetBorder.

GS1-128 barcodes

GS1-128 barcodes (former EAN-128) can encode all ASCII alphanumeric characters in the barcode format. They can have any width and use the Code-128 dictionary developed by International Standard ISO/IEC 15417. The PDFFlow Barcodes library supports Code Sets A, B, and C and provides three corresponding types of GS1-128 barcodes for selection. When adding a GS1-128 barcode, specify the necessary type in the barcodeType parameter: BarcodeType.GS1_128A, BarcodeType.GS1_128B, or BarcodeType.GS1_128C. All other settings and methods for GS1-128 are the same as for EAN-13 barcodes.

Examples

Example 1. Add a barcode with default settings [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddBarcode("4810151021665", BarcodeType.EAN_13)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 2. Add a barcode with a hidden data label [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Some text before barcode")
                        .AddBarcodeToParagraph("90311017", BarcodeType.EAN_8, false)
                    .AddText("Some text after barcode")
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 3. Add a barcode with specified colors [hide]

            DocumentBuilder.New()
                .AddSection()
                        .AddParagraph()
                            .AddBarcode("727783003089", BarcodeType.UPC_A, Color.FromRgba(0.2, 0.35, 1), Color.Yellow, true)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 4. Add a barcode with a specified width [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddBarcode("4810151021665", BarcodeType.EAN_13, true, 200)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 5. Formatting a barcode using the methods of BarcodeInlineBuilder [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph("Some text before barcode")
                        .AddBarcode("90311017", BarcodeType.EAN_8)
                        .SetMarginLeft(10)
                        .SetMarginRight(30)
                .ToParagraph()
                    .AddText("Some text after barcode")
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 6. Using a barcode with a tabulation [hide]

            DocumentBuilder.New()
                .AddSection()
                        .AddParagraph("Some text before barcode")
                            .AddTabSymbol()
                            .AddBarcode("727783003089", BarcodeType.UPC_A, Color.Blue, true, 80)
                        .ToParagraph()
                            .AddTabulationInPercent(100, TabulationType.Right)
                .ToDocument()
                    .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 7. Add a GS1-128A barcode to a paragraph [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddBarcode("(01)95012345678903(3103)000123", BarcodeType.GS1_128A)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 8. Add a GS1-128B barcode to a paragraph [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddBarcode("(01)95012345678903(3103)000123", BarcodeType.GS1_128B)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 9. Add a GS1-128C barcode to a paragraph and set its border (C supports only digits) [hide]

            DocumentBuilder.New()
                .AddSection()
                    .AddParagraph()
                        .AddBarcode("01950123456789033103000123", BarcodeType.GS1_128C)
                        .SetBorder(Stroke.Dotted, Color.Blue)
            .ToDocument()
                .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 10. Add GS1-128 barcodes with all available code sets to a section, to a repeating area, and to a table cell, with and without printing of the input data, and setting borders and margins [hide]

            DocumentBuilder.New()
                .AddSection()
                .AddParagraphToSection("Add GS1-128C barcode to section:")
                    .AddBarcode("115672", BarcodeType.GS1_128C)
                .ToSection()
                    .AddRptAreaLeftToBothPages(100f)
                    .AddParagraphToRepeatingArea("Add GS1-128B barcode to repeating area:")
                        .AddBarcode("Code 128", BarcodeType.GS1_128B, 100f, 0f, false)
                        .SetMarginTop(150f)
                .ToSection()
                    .AddRptAreaLeftToBothPages(5f)
                        .AddLine(1f, 500f)
                .ToSection()
                    .AddTable()
                    .AddColumnToTable("", XUnit.FromPercent(20f))
                    .AddColumnToTable("", XUnit.FromPercent(80f))
                        .AddRow()
                        .AddCellToRow()
                            .AddCell()
                            .AddParagraphToCell("Add GS1-128A barcode to table cell:")
                                .AddBarcode("TEST DATA", BarcodeType.GS1_128A)
                                .SetMarginTop(15f)
                                .SetMarginLeft(25f)
                                .SetBorder(Stroke.Solid, Color.Green, 1f)
                .ToDocument()
                    .Build("Result.pdf");

 The above code will generate the following:
 
 See the document

Example 11. Set the stroke color and background color of a GS1-128 barcode [hide]

        DocumentBuilder.New()
            .AddSection()
                .AddParagraph()
                    .AddBarcode("01950123456789033103000123", BarcodeType.GS1_128C, strokeColor: Color.Red, backColor: Color.FromRgba(1, 0.7, 0.4), true)
        .ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

Example 12. Hide quite zones in a EAN/UPC barcode [hide]

        DocumentBuilder.New()
            .AddSection()
                .AddParagraph()
                    .AddBarcode("4810151021665", BarcodeType.EAN_13, Color.Black, Color.Yellow, true, hasQuiteZones: false)
        .ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

Example 13. Hide quite zones in a GS1-128 barcode [hide]

        DocumentBuilder.New()
            .AddSection()
                .AddParagraph()
                    .AddBarcode("01950123456789033103000123", BarcodeType.GS1_128C, Color.Black, Color.Yellow, true, hasQuiteZones: false)
        .ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

Example 14. Set a custom width for a GS1-128 barcode [hide]

        DocumentBuilder.New()
            .AddSection()
                .AddParagraph("Width = 225:  ")
                    .AddBarcode("01950123456789033103000123", BarcodeType.GS1_128C, width: 225f, 0, true, false)
            .ToSection()
                .AddParagraph("Width = 450:  ")
                    .SetMarginTop(10)
                    .AddBarcode("01950123456789033103000123", BarcodeType.GS1_128C, width: 450f, 0, true, false)
        .ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

Example 15. Cut the barcode height [hide]

        DocumentBuilder.New()
            .AddSection()
                .AddParagraph()
                    .AddTextToParagraph("Standard height:  ")
                    .AddBarcodeToParagraph("4810151021665", BarcodeType.EAN_13, width: 200, heightToCut: 0)
                    .AddTextToParagraph("  Cut 30%:  ")
                    .AddBarcodeToParagraph("4810151021665", BarcodeType.EAN_13, width: 200, heightToCut: XUnit.FromPercent(30))
            .ToSection()
                .AddParagraph()
                    .SetMarginTop(10)
                    .AddTextToParagraph("Standard height:")
                    .AddBarcodeToParagraph("01950123456789033103000123", BarcodeType.GS1_128C, width: 300, heightToCut: 0)
                    .AddTextToParagraph("Cut 20 px:")
                    .AddBarcodeToParagraph("01950123456789033103000123", BarcodeType.GS1_128C, width: 300, heightToCut: 20)
        .ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

Example 16. Barcode rotation [hide]

        DocumentBuilder.New()
.AddSection()
.AddParagraph()
.AddTextToParagraph("Rotate 90 degrees clockwise: ")
.AddBarcodeToParagraph("481015102166", BarcodeType.UPC_A, barcodeRotation: BarcodeRotation.Clockwise_90)
.AddTextToParagraph("  Rotate 270 degrees clockwise: ")
.AddBarcodeToParagraph("01950123456789033103000123", BarcodeType.GS1_128C, hasQuiteZones: false, barcodeRotation: BarcodeRotation.Clockwise_270)
.AddTextToParagraph("  Rotate 180 degrees clockwise: ")
.AddBarcodeToParagraph("90311017", BarcodeType.EAN_8, barcodeRotation: BarcodeRotation.Clockwise_180)
.ToDocument()
            .Build("Result.pdf");
 The above code will generate the following:
 
 See the document

back