Example_21

Drawing the same address as a QR code with each of the four error correction levels, labeled with how much can be restored and how much data fits.

Example_21.pdf — the PDF this example creates.

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

/**
 * Example_21.cs
 * This example draws the same web address as a QR code with each of the four
 * error correction levels. A higher level lets a scanner read a code that is
 * more damaged or covered, and leaves room for less data in the code.
 */
public class Example_21 {
    public Example_21() {
        PDF pdf = new PDF(new BufferedStream(
                new FileStream("Example_21.pdf", FileMode.Create)));
        pdf.SetCompliance(Compliance.PDF_UA_1);
        pdf.SetTitle("QR Code Error Correction");

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

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

        TextLine text = new TextLine(f2, "QR Code Error Correction");
        text.SetStructureType(StructElem.H1);
        text.SetFontSize(22f);
        text.SetLocation(70f, 80f);
        text.DrawOn(page);

        TextBlock textBlock = new TextBlock(f1,
                "Each QR code below holds the same address, https://pdfjet.com. "
                + "A higher error correction level lets a scanner read the code when "
                + "more of it is damaged or covered, and leaves room for less data, "
                + "so longer data at a higher level makes a larger code.");
        textBlock.SetFontSize(12f);
        textBlock.SetLineSpacing(1.5f);
        textBlock.SetLocation(70f, 95f);
        textBlock.SetWidth(470f);
        textBlock.DrawOn(page);

        ErrorCorrectionLevel[] levels = {
            ErrorCorrectionLevel.L,
            ErrorCorrectionLevel.M,
            ErrorCorrectionLevel.Q,
            ErrorCorrectionLevel.H,
        };
        String[] names = {
            "L (Low)",
            "M (Medium)",
            "Q (Quartile)",
            "H (High)",
        };
        String[] notes = {
            "About 7% can be restored, 78 bytes fit in this size",
            "About 15% can be restored, 62 bytes fit in this size",
            "About 25% can be restored, 46 bytes fit in this size",
            "About 30% can be restored, 34 bytes fit in this size",
        };

        // Two rows of two codes.
        for (int i = 0; i < levels.Length; i++) {
            float x = 70f + (i % 2) * 250f;
            float y = 200f + (i / 2) * 250f;

            QRCode qr = new QRCode("https://pdfjet.com", levels[i]);
            qr.SetModuleLength(5f);
            qr.SetLocation(x, y);
            float[] xy = qr.DrawOn(page);

            text = new TextLine(f2, names[i]);
            text.SetFontSize(12f);
            text.SetLocation(x, xy[1] + 20f);
            text.DrawOn(page);

            text = new TextLine(f1, notes[i]);
            text.SetFontSize(10f);
            text.SetTextColor(Color.dimgray);
            text.SetLocation(x, xy[1] + 35f);
            text.DrawOn(page);
        }

        // GS1 data as a GS1 Digital Link, a web address an ordinary QR code carries.
        Page page2 = new Page(pdf, Letter.PORTRAIT);
        text = new TextLine(f2, "GS1 Digital Link");
        text.SetStructureType(StructElem.H2);
        text.SetFontSize(18f);
        text.SetLocation(70f, 80f);
        text.DrawOn(page2);

        String link = GS1.DigitalLink("https://id.gs1.org", "(01)09506000134352(10)ABC123(17)261231");
        TextBlock note = new TextBlock(f1, "The GS1 data (01)09506000134352(10)ABC123(17)261231, a GTIN, a batch and an expiry date, "
                + "is this web address as a GS1 Digital Link, " + link + ", which an ordinary QR code carries and any phone opens.");
        note.SetFontSize(12f);
        note.SetLineSpacing(1.5f);
        note.SetLocation(70f, 95f);
        note.SetWidth(470f);
        float[] corner = note.DrawOn(page2);

        QRCode code = new QRCode(link, ErrorCorrectionLevel.M);
        code.SetModuleLength(4f);
        code.SetLocation(70f, corner[1] + 30f);
        code.DrawOn(page2);

        pdf.Complete();
    }

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

©2026 PDFjet Software.