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.

package examples;

import java.io.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
import com.pdfjet.datamatrix.*;

/**
 * Example_14.java
 * Drawing Data Matrix barcodes.
 */
public class Example_14 {
    public Example_14() throws Exception {
        PDF pdf = new PDF(
                new BufferedOutputStream(new FileOutputStream("Example_14.pdf")));
        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) throws Exception {
        long time0 = System.currentTimeMillis();
        new Example_14();
        long time1 = System.currentTimeMillis();
        System.out.printf("Example_14 => %4d ms%n", time1 - time0);
    }
}   // End of Example_14.java

©2026 PDFjet Software.