Recursive AI

Add a QR code to a PDF in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Terminal
dotnet add package RecursiveAI.OnePdf.Layout

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf.Layout;
 
LayoutDocument.Compose(doc => doc.Page(page =>
    page.Content().Width(144).QrCode("https://example.com/invoice/1042").Contain()))
    .Save("qr-code.pdf");

Writes qr-code.pdf with a QR code for the address, 144 pt (2 inches) wide including its margin. QrCode needs version 1.25.0 or later.

Unzip the example, then dotnet run.

The full program

The download builds a small invoice page with a QR code that links to the invoice and a Code 128 barcode for the order number. There’s no input file.

C#
using OnePdf.Layout;
 
const string invoiceUrl = "https://example.com/invoice/1042";
 
var invoice = LayoutDocument.Compose(doc =>
{
    doc.Title("Invoice 1042").Language("en-US");
    doc.Page(page =>
    {
        page.Size(PageSize.A6);
        page.Margin(28);
        page.Content().Column(col =>
        {
            col.Spacing(10);
            col.Item().Heading(1).Text("Invoice 1042").FontSize(16).Bold();
            col.Item().Text("Scan the code to view and pay this invoice online.");
            col.Item().Width(120).QrCode(invoiceUrl)
                .ErrorCorrection(QrErrorCorrection.Quartile)  // level Q; default M
                .Contain();                                   // fills 120 pt
            col.Item().Barcode(BarcodeSymbology.Code128, "ORDER-1042")
                .BarHeight(36)
                .HumanReadableText();                         // text under bars
        });
    });
});
 
invoice.Save("invoice-1042.pdf");
Console.WriteLine("Wrote invoice-1042.pdf");
Console.WriteLine($"Layout notes: {invoice.Diagnostics.Count}");
Text
Wrote invoice-1042.pdf
Layout notes: 0

The page in invoice-1042.pdf: the heading “Invoice 1042”, a QR code, and a Code 128 barcode with ORDER-1042 printed under it.

A barcode reader reads the QR code as https://example.com/invoice/1042 and the barcode as ORDER-1042. Data a symbol can’t hold, such as a character Code 128 can’t encode or more than fits a QR code at the chosen level, throws PdfBarcodeException when the document is laid out.

Variations

A fixed module size. In place of Width(120) and Contain(), set the size of one dark square in points:

C#
col.Item().QrCode(invoiceUrl).ModuleSize(3);

Without ModuleSize or Contain(), a module is 2 pt.

Your own alternate text. In a tagged document the code is a figure whose alternate text is the value. To describe it instead:

C#
col.Item().Width(120).QrCode(invoiceUrl).Contain().AltText("Link to invoice 1042");

An EAN-13 product barcode. Give the 12 digits and the check digit is added; the digits are printed under the bars by default:

C#
col.Item().Barcode(BarcodeSymbology.Ean13, "400638133393");

If you pass all 13 digits, the check digit is verified, and a wrong one throws PdfBarcodeException.


The short answer

Add the RecursiveAI.OnePdf.Layout package and call QrCode(value) on any layout slot. OnePDF encodes the value itself and draws the code as vector rectangles: there’s no image in the file and no separate QR library to install. Contain() makes the code as large as the slot allows, quiet zone included. The same call works in RecursiveAI.OnePdf.Layout.Core, the package without bundled fonts, because a QR code needs no font.

How do I generate a QR code in C#?

With OnePDF Layout the encoder is part of the package and follows ISO/IEC 18004. It picks the smallest QR version (1 to 40) that holds your data, chooses between numeric, alphanumeric and byte segments to keep it small, and scores all eight masks. Text is encoded as UTF-8 by default. MinVersion, MaxVersion or Version(n) bound the version if your reader needs a fixed size.

Because the result is vector, it stays sharp at any zoom and when printed, and every module has exactly the same size: a module is never rounded.

Which error correction level should I use?

QR codes have four levels: L, M, Q and H. Higher levels let a reader recover from more damage but need a larger symbol for the same data. OnePDF uses M by default. The full program sets Q with ErrorCorrection(QrErrorCorrection.Quartile). If the data doesn’t fit version 40 at the level you chose, layout throws PdfBarcodeException rather than drawing a code that won’t scan.

How big should a QR code be in a PDF?

Large enough for the reader’s camera at the distance it will be scanned from, with a clear margin around it. That margin, the quiet zone, is 4 modules for QR codes, and OnePDF includes it in the element’s box, so the white space around the code is already there. QuietZone(modules) changes it. Readers also need contrast and a light background; Colors(foreground, background) changes the colors, and the library does not check the contrast for you.

Can I add other barcodes to a PDF?

Yes. Barcode(symbology, value) draws Code 128 (with automatic code set switching), GS1-128, EAN-13, EAN-8, UPC-A and Code 39. Linear barcodes are 40 modules tall unless you set BarHeight, and HumanReadableText() prints the value under the bars. EAN and UPC digits are set in the layout of the standard, and printed by default. All options are in the layout documentation.

Can screen readers read a QR code in a PDF?

In a tagged document the QR code is a Figure, and its alternate text is the encoded value unless you set AltText(...). A screen reader announces that text, so a person who can’t scan the code still gets the address. Layout output is tagged by default. If the code is decoration, Artifact() leaves it out of the structure.

All onepdf recipes