---
title: "Add a barcode to a PDF in C#"
url: https://recursiveai.org/onepdf/cookbook/draw-barcode-in-pdf/
description: "Add a Code 128 barcode to a PDF in C# with OnePDF. Draw vector bars from an order ID, adjust their size, or generate an EAN-13 barcode."
version: "1.25.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Layout) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/draw-barcode-in-pdf/draw-barcode-in-pdf.zip)

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](https://recursiveai.org/onepdf/cookbook/add-watermark/#variations) if you already have the barcode as a PNG.

```shell
dotnet add package RecursiveAI.OnePdf.Layout
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onepdf/cookbook/draw-barcode-in-pdf/draw-barcode-in-pdf.zip), 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.

```csharp
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.

```csharp
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.

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

## Related recipes

- [Create a PDF in C#](https://recursiveai.org/onepdf/cookbook/create-pdf/)
- [Add a QR code to a PDF](https://recursiveai.org/onepdf/cookbook/add-qr-code-to-pdf/)

## 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](https://recursiveai.org/onepdf/docs/layout/#barcodes).

## 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.
