Example_36

Drawing labeled Code 128, Code 39, UPC-A and EAN-13 linear barcodes, and barcodes drawn from top to bottom and from bottom to top.

Example_36.pdf — the PDF this example creates.

using System;
using System.IO;
using System.Diagnostics;
using PDFjet.NET;

/**
 * Example_36.cs
 * This example draws a Code 128, a Code 39, a UPC-A and an EAN-13 barcode,
 * each next to a label, and then barcodes drawn from top to bottom and from
 * bottom to top.
 */
public class Example_36 {
    public Example_36() {
        PDF pdf = new PDF( new BufferedStream(
                new FileStream("Example_36.pdf", FileMode.Create)));
        pdf.SetCompliance(Compliance.PDF_UA_1);
        pdf.SetTitle("Linear Barcodes");

        Font f1 = new Font(pdf, IBMPlexSans.Regular);
        f1.SetSize(12f);

        Font f2 = new Font(pdf, IBMPlexSans.SemiBold);
        f2.SetSize(12f);

        Page page = new Page(pdf, Letter.PORTRAIT);

        TextLine text = new TextLine(f2, "Linear Barcodes");
        text.SetStructureType(StructElem.H1);
        text.SetFontSize(22f);
        text.SetLocation(70f, 80f);
        text.DrawOn(page);

        String[] labels = {
            "Code 128",
            "Code 39",
            "UPC-A",
            "EAN-13",
            "GS1-128",
        };
        String[] notes = {
            "Letters, digits and symbols",
            "Upper case letters and digits",
            "11 digits, the check digit is added",
            "12 digits, the check digit is added",
            "GS1 data: a GTIN and a batch number",
        };
        Barcode[] barcodes = {
            new Barcode(Barcode.CODE_128, "Hellö, World!"),
            new Barcode(Barcode.CODE_39, "WIKIPEDIA"),
            new Barcode(Barcode.UPC_A, "51234567890"),
            new Barcode(Barcode.EAN_13, "051234567890"),
            new Barcode(Barcode.GS1_128, "(01)09506000134352(10)ABC123"),
        };
        // UPC-A and EAN-13 need wider bars for the digits under them.
        float[] moduleLengths = {0.75f, 0.75f, 1f, 1f, 0.75f};

        float y = 130f;
        float[] xy;
        for (int i = 0; i < barcodes.Length; i++) {
            new TextLine(f2, labels[i]).SetLocation(70f, y + 15f).DrawOn(page);
            TextLine note = new TextLine(f1, notes[i]);
            note.SetFontSize(10f);
            note.SetTextColor(Color.dimgray);
            note.SetLocation(70f, y + 32f);
            note.DrawOn(page);

            barcodes[i].SetLocation(290f, y);
            barcodes[i].SetModuleLength(moduleLengths[i]);
            barcodes[i].SetFont(f1);
            xy = barcodes[i].DrawOn(page);
            y = xy[1] + 30f;
        }

        // The same barcodes can be drawn from top to bottom and from bottom to top.
        new TextLine(f2, "Vertical barcodes").SetLocation(70f, y + 15f).DrawOn(page);

        Barcode barcode = new Barcode(Barcode.CODE_128, "G86513JVW0C");
        barcode.SetLocation(70f, y + 35f);
        barcode.SetModuleLength(0.75f);
        barcode.SetDirection(Direction.TOP_TO_BOTTOM);
        barcode.SetFont(f1);
        xy = barcode.DrawOn(page);

        barcode = new Barcode(Barcode.CODE_39, "CODE39");
        barcode.SetLocation(xy[0] + 60f, y + 35f);
        barcode.SetModuleLength(0.75f);
        barcode.SetDirection(Direction.BOTTOM_TO_TOP);
        barcode.SetFont(f1);
        xy = barcode.DrawOn(page);

        barcode = new Barcode(Barcode.EAN_13, "051234567890");
        barcode.SetLocation(xy[0] + 60f, y + 35f);
        barcode.SetModuleLength(1f);
        barcode.SetDirection(Direction.BOTTOM_TO_TOP);
        barcode.SetFont(f1);
        barcode.DrawOn(page);

        // ITF-14, the GTIN of a carton, framed by bearer bars.
        Page page2 = new Page(pdf, Letter.PORTRAIT);
        TextLine heading2 = new TextLine(f2, "Barcodes for Cartons");
        heading2.SetStructureType(StructElem.H2);
        heading2.SetFontSize(18f);
        heading2.SetLocation(70f, 80f);
        heading2.DrawOn(page2);

        TextLine label = new TextLine(f2, "ITF-14");
        label.SetLocation(70f, 135f);
        label.DrawOn(page2);
        TextLine cartonNote = new TextLine(f1, "The GTIN of a carton, 13 digits and the check digit PDFjet adds, framed by bearer bars");
        cartonNote.SetFontSize(10f);
        cartonNote.SetTextColor(Color.dimgray);
        cartonNote.SetLocation(70f, 152f);
        cartonNote.DrawOn(page2);

        Barcode itf = new Barcode(Barcode.ITF_14, "1540014128876");
        itf.SetLocation(70f, 175f);
        itf.SetModuleLength(1f);
        itf.SetFont(f1);
        itf.DrawOn(page2);

        pdf.Complete();
    }

    public static void Main(String[] args) {
        Stopwatch sw = Stopwatch.StartNew();
        long time0 = sw.ElapsedMilliseconds;
        new Example_36();
        long time1 = sw.ElapsedMilliseconds;
        sw.Stop();
        Console.WriteLine($"Example_36 => {time1 - time0,4} ms");
    }
}   // End of Example_36.cs

©2026 PDFjet Software.