Example_14

Drawing Data Matrix barcodes: a web address, text in UTF-8, a rectangular symbol and a larger symbol.

Example_14.pdf — the PDF this example creates.

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

/**
 * Example_14.cs
 * Drawing Data Matrix barcodes.
 */
public class Example_14 {
    public Example_14() {
        PDF pdf = new PDF(new BufferedStream(
            new FileStream("Example_14.pdf", FileMode.Create)));
        pdf.SetCompliance(Compliance.PDF_UA_1);
        pdf.SetTitle("Data Matrix Barcodes");

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

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

        // The heading of the document, above the symbols
        new TextLine(f1, "Data Matrix Barcodes")
                .SetStructureType(StructElem.H1)
                .SetFontSize(18f)
                .SetLocation(50f, 36f)
                .DrawOn(page);

        DataMatrix barcode = new DataMatrix("https://github.com/edragoev1/pdfjet");
        barcode.SetLocation(50f, 50f);
        barcode.SetModuleLength(3f);
        float[] xy = barcode.DrawOn(page);
        TextLine caption = new TextLine(f1, "A web address");
        caption.SetLocation(50f, xy[1] + 20f);
        caption.DrawOn(page);

        barcode = new DataMatrix("Grüße aus München! こんにちは 😀");
        barcode.SetLocation(300f, 50f);
        barcode.SetModuleLength(3f);
        xy = barcode.DrawOn(page);
        caption = new TextLine(f1, "Text in UTF-8");
        caption.SetLocation(300f, xy[1] + 20f);
        caption.DrawOn(page);

        barcode = new DataMatrix("PDFjet 9.0.2", DataMatrix.RECTANGLE);
        barcode.SetLocation(50f, 250f);
        barcode.SetModuleLength(4f);
        barcode.SetModuleColor(Color.blue);
        xy = barcode.DrawOn(page);
        caption = new TextLine(f1, "A rectangular symbol");
        caption.SetLocation(50f, xy[1] + 20f);
        caption.DrawOn(page);

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 20; i++) {
            sb.Append("Line ").Append(i).Append(" of a longer text in a larger symbol.\n");
        }
        barcode = new DataMatrix(sb.ToString());
        barcode.SetLocation(300f, 250f);
        barcode.SetModuleLength(2f);
        xy = barcode.DrawOn(page);
        caption = new TextLine(f1, "A larger symbol");
        caption.SetLocation(300f, xy[1] + 20f);
        caption.DrawOn(page);

        barcode = DataMatrix.FromGS1("(01)09506000134352(17)261231(10)ABC123(21)XYZ-42");
        barcode.SetLocation(50f, 400f);
        barcode.SetModuleLength(3f);
        xy = barcode.DrawOn(page);
        // On two lines, clear of the larger symbol at its right
        caption = new TextLine(f1, "GS1: a GTIN, an expiry date,");
        caption.SetLocation(50f, xy[1] + 20f);
        caption.DrawOn(page);
        caption = new TextLine(f1, "a batch and a serial number");
        caption.SetLocation(50f, xy[1] + 20f + 14f); // A line of 10 points, and its spacing
        caption.DrawOn(page);

        pdf.Complete();
    }

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

©2026 PDFjet Software.