Example_21

Using QR code 2D barcodes with Low, Medium, High and Very High error correction levels.

Example_21.pdf — the PDF this example creates.

/*
 * Example_21.java
 *
 * Copyright (c) 2026 PDFjet Software
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package examples;

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

/**
 * Example_21.java
 */
public class Example_21 {
    public Example_21() throws Exception {
        PDF pdf = new PDF(
                new BufferedOutputStream(new FileOutputStream("Example_21.pdf")));

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

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

        TextLine text = new TextLine(f1,
                "QR codes encoded with Low, Medium, High and Very High error correction level");
        text.setLocation(100.0f, 30.0f);
        text.drawOn(page);

        // Please note:
        // The higher the error correction level - the shorter the string that you can encode.
        QRCode qr = new QRCode(
                "https://kazuhikoarase.github.io/qrcode-generator/js/demo",
                ErrorCorrectionLevel.L);   // Low
        qr.setModuleLength(3f);
        qr.setLocation(100f, 100f);
        // qr.setModuleColor(Color.blue);
        qr.drawOn(page);

        qr = new QRCode(
                "https://github.com/kazuhikoarase/qrcode-generator",
                ErrorCorrectionLevel.M);   // Medium
        qr.setLocation(400f, 100f);
        qr.setModuleLength(3f);
        qr.drawOn(page);

        qr = new QRCode(
                "https://github.com/kazuhikoarase/jaconv",
                ErrorCorrectionLevel.Q);   // High
        qr.setLocation(100f, 400f);
        qr.setModuleLength(3f);
        qr.drawOn(page);

        qr = new QRCode(
                "https://github.com/kazuhikoarase",
                ErrorCorrectionLevel.H);   // Very High
        qr.setLocation(400f, 400f);
        qr.setModuleLength(3f);
        qr.drawOn(page);

        pdf.complete();
    }

    public static void main(String[] args) throws Exception {
        long time0 = System.currentTimeMillis();
        new Example_21();
        long time1 = System.currentTimeMillis();
        System.out.printf("Example_21 => %4d ms%n", time1 - time0);
    }
}   // End of Example_21.java

©2026 PDFjet Software.