---
title: "Set PDF page labels in C#"
url: https://recursiveai.org/onepdf/cookbook/set-pdf-page-labels/
description: "Set PDF page labels in C# with OnePDF. Use Roman numerals for a preface and restart at 1 for the main text without printing numbers on the pages."
version: "1.25.0"
---

# Set PDF page labels 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) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/set-pdf-page-labels/set-pdf-page-labels.zip)

Page labels name pages in the viewer, not on paper. Start the preface at `i` and the text at `1`. For an existing PDF, [edit the page labels in the document catalog](#how-do-i-add-page-labels-in-a-pdf).

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

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

```csharp
using OnePdf;
using static OnePdf.PdfPageLabelStyle;
using var file = File.Create("labels.pdf");
using var pdf = new PdfDocumentWriter(file);
pdf.BeginPage(612, 792).Commit();
pdf.AddPageLabelRange(0, LowerRoman);
pdf.BeginPage(612, 792).Commit();
pdf.AddPageLabelRange(1, Arabic);
pdf.Finish();
Console.WriteLine("Wrote labels.pdf");
```

Unzip [the example](https://recursiveai.org/onepdf/cookbook/set-pdf-page-labels/set-pdf-page-labels.zip), then `dotnet run`.

This writes two blank pages with viewer labels `i` and `1`.

```text
Wrote labels.pdf
```

## The full program

The download writes a heading on each page but no printed page number. It reopens `labels.pdf` and reads the labels back from the file.

```csharp
using OnePdf;

using (var file = File.Create("labels.pdf"))
using (var writer = new PdfDocumentWriter(file))
{
    AddPage("Preface");
    writer.AddPageLabelRange(0, PdfPageLabelStyle.LowerRoman);
    AddPage("Chapter one");
    writer.AddPageLabelRange(1, PdfPageLabelStyle.Arabic);
    AddPage("Chapter two");
    writer.Finish();

    void AddPage(string heading)
    {
        var page = writer.BeginPage(612, 792);
        page.SetFont(writer.AddStandardFont("Helvetica"), 18);
        page.DrawText(heading, 72, 720);
        page.Commit();
    }
}

using var pdf = PdfDocument.Open("labels.pdf");
for (int i = 0; i < pdf.Pages.Count; i++)
    Console.WriteLine($"Sheet {i + 1}: {pdf.Pages[i].Label}");
```

```text
Sheet 1: i
Sheet 2: 1
Sheet 3: 2
```

Adding a range before its first page exists, adding ranges out of order, or passing a starting number below `1` throws `ArgumentOutOfRangeException`.

## Variations

Replace the line beginning `writer.AddPageLabelRange(1,` in the download to change the labels on sheets 2 and 3:

**Start at a different number and add a prefix.** The labels become `A-5` and `A-6`.

```csharp
writer.AddPageLabelRange(1, PdfPageLabelStyle.Arabic,
    prefix: "A-", start: 5);
```

**Use just a name.** Both sheets get the same label, `Appendix`, until another range starts. For distinct labels on each page, use a numbering style instead.

```csharp
writer.AddPageLabelRange(1, PdfPageLabelStyle.None,
    prefix: "Appendix");
```

## Related recipes

- [Add Page N of M numbers to a PDF](https://recursiveai.org/onepdf/cookbook/add-page-n-of-m-numbers/)
- [Create a large PDF one page at a time](https://recursiveai.org/onepdf/cookbook/stream-large-pdf-creation/)

## The short answer

Call `AddPageLabelRange` on `PdfDocumentWriter` as you create a PDF. Pass the zero-based page index and the numbering style. A range applies from that page onward until the next range starts. The example sets labels on a new PDF, not on a file opened for editing.

## How do I add page labels in a PDF?

Start a range after you’ve called `BeginPage` for its first page. Pass `0` to start on the first physical page, `1` for the second, and so on. The `start` argument sets the first **label number** in the range and defaults to `1`. It’s not the same as the page index. For example, with two pages of front matter, start the main range at index `2` and leave `start` alone. The third physical page is then labeled `1`.

You don’t need a range for every page. In the full program, the second and third sheets use the same decimal range and read back as `1` and `2`. If you add your first range after index `0`, earlier pages get plain decimal labels starting at `1`. The writer records the ranges in the PDF’s `/PageLabels` entry; reopening the PDF and reading `page.Label` retrieves the labels without trying to extract any printed text. See [page labels in the writer reference](https://recursiveai.org/onepdf/docs/content-creation/#streaming-creation).

To relabel an existing PDF, open it with editing enabled and replace the catalog’s `/PageLabels` entry. This replaces the old ranges with one range starting at index `0`, so the `labels.pdf` from the full program reads back as `A-1`, `A-2`, and `A-3` in the saved copy.

```csharp
using OnePdf;

using var pdf = PdfDocument.Open("labels.pdf",
    new() { AllowEditing = true });
var range = new PdfDictionary();
range.SetEntry("S", new PdfName("D")); // decimal
range.SetEntry("P", PdfString.FromText("A-"));
var nums = new PdfArray();
nums.Append(new PdfInteger(0)); // first page
nums.Append(range);
var labels = new PdfDictionary();
labels.SetEntry("Nums", nums);
pdf.Catalog.SetEntry("PageLabels", labels);
pdf.Save("relabeled.pdf");
```

## How do I use Roman numerals for PDF page numbers?

Use `LowerRoman` for `i`, `ii`, `iii` or `UpperRoman` for `I`, `II`, `III`. Switch to `Arabic` for `1`, `2`, `3`. These styles control what a viewer that supports page labels can show in its navigation. They don’t change the order of the pages or draw a footer that appears on paper.

If someone needs to cite a page from a printed copy, draw the number into the page content too. Decide whether printed numbers should match the viewer labels, then use the same numbering scheme for both. For a footer that counts physical pages, use [Page N of M numbering](https://recursiveai.org/onepdf/cookbook/add-page-n-of-m-numbers/) instead of changing the labels.
