Recursive AI

Add a barcode 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.

Draw a Code 128 barcode from an order ID while creating a new PDF. OnePDF 1.25.0 can stamp a barcode image onto an existing PDF if you already have the barcode as a PNG.

Terminal
dotnet add package RecursiveAI.OnePdf.Layout

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

C#
using OnePdf.Layout;
using static OnePdf.Layout.BarcodeSymbology;
 
LayoutDocument.Compose(d => d.Page(p =>
    p.Content().Barcode(Code128, "ORDER-2041")
        .BarHeight(36))).Save("barcode.pdf");
Console.WriteLine("Wrote barcode.pdf");

Unzip the example, then dotnet run.

This writes a new barcode.pdf with a linear barcode for ORDER-2041.

Text
Wrote barcode.pdf

The full program

The download uses a small A6 page with margins and puts the order ID below the bars so it’s readable without a scanner.

C#
using OnePdf.Layout;
 
var pdf = LayoutDocument.Compose(doc => doc.Page(page =>
{
    page.Size(PageSize.A6);
    page.Margin(24);
    page.Content()
        .Barcode(BarcodeSymbology.Code128, "ORDER-2041")
        .BarHeight(36).HumanReadableText();
}));
pdf.Save("barcode.pdf");
Console.WriteLine("Wrote barcode.pdf");
Text
Wrote barcode.pdf

If the value contains a character Code 128 can’t encode, layout throws PdfBarcodeException.

Variations

Replace the page.Content() chain in the download with one of these:

Draw EAN-13 from 12 digits. The encoder adds the final check digit. The digits below the bars are on by default.

C#
page.Content().Barcode(BarcodeSymbology.Ean13,
    "400638133393");

Set the width of the narrowest bar. This asks for 0.75 points per module and bars 36 points high, without text below the code.

C#
page.Content()
    .Barcode(BarcodeSymbology.Code128, "ORDER-2041")
    .ModuleSize(0.75).BarHeight(36);

The short answer

Use Barcode in a OnePDF layout slot and save the layout as a PDF. A slot can be the entire page content, as above, or an item in a column next to a shipping address or order details. OnePDF draws the bars as vector rectangles rather than placing a bitmap.

What is Code 128 used for?

Code 128 works for order and shipment identifiers that mix digits and letters, such as ORDER-2041. OnePDF’s encoder supports ASCII characters 0–127. Non-ASCII text such as accented names isn’t supported by this encoder. Use the identifier your scanner or downstream system expects, not just a string that happens to fit.

If a receiving system asks for GS1-128, use that format rather than passing the same string to Code 128. OnePDF’s GS1-128 encoder takes parenthesized application identifiers, such as (01) for a trade item number. It checks the length of supported fixed-length fields, but it doesn’t verify the check digits inside them. For retail codes, OnePDF also supports EAN-13, EAN-8, and UPC-A. For those three formats it adds a missing check digit or rejects an incorrect one you supply. See the barcode format rules.

What are the quiet-zone requirements for a barcode?

The quiet zone is the clear area beside the bars. OnePDF reserves ten modules on each side for Code 128; a module is the width of the narrowest bar. EAN-13 uses different margins on the left and right. Leave the format’s default quiet zone in place and keep other artwork out of it.

Width and height are separate settings. ModuleSize sets the width of a module; BarHeight sets how tall the bars are. OnePDF can shrink a barcode if its slot is too small, even when you set the module size. If you’re printing labels, give the symbol room in the layout and try scanning a print at the size you’ll actually use. The library doesn’t check contrast, so keep the bars dark on a light background.

All onepdf recipes