---
title: "Add “Page X of Y” to a PDF in C#"
url: https://recursiveai.org/onepdf/cookbook/add-page-n-of-m-numbers/
description: "Add Page X of Y to a PDF footer in C# with OnePDF Layout. Use the final document or section page count without counting pages yourself."
version: "1.23.0"
---

# Add “Page X of Y” 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/add-page-n-of-m-numbers/add-page-n-of-m-numbers.zip)

No input file is needed: `PageNumberText` works only when you generate a PDF, not when you stamp a footer onto an existing file.

For an existing PDF, count its pages and [draw the numbered footer on each page](https://recursiveai.org/onepdf/docs/content-creation/#text).

```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;

var format = "Page {page} of {total}";
var pdf = LayoutDocument.Compose(doc =>
    doc.Page(page =>
    {
        page.Footer().PageNumberText(format);
        page.Content().Text("Summary");
    }));
pdf.Save("report.pdf");
Console.WriteLine("Wrote report.pdf");
```

Unzip [the example](https://recursiveai.org/onepdf/cookbook/add-page-n-of-m-numbers/add-page-n-of-m-numbers.zip), then `dotnet run`.

Writes `report.pdf` with “Page 1 of 1” in the footer.

```text
Wrote report.pdf
```

## The full program

The download adds a page break between “Summary” and “Details,” centers the footer, and reads back the text of both pages.

```csharp
using OnePdf;
using OnePdf.Layout;
using OnePdf.Text;

LayoutDocument.Compose(doc => doc.Page(page =>
{
    page.Margin(36);
    page.Footer()
        .PageNumberText("Page {page} of {total}")
        .AlignCenter();
    page.Content().Column(col =>
    {
        col.Item().Text("Summary");
        col.Item().PageBreak();
        col.Item().Text("Details");
    });
})).Save("report.pdf");

Console.WriteLine("Wrote report.pdf");

using var pdf = PdfDocument.Open("report.pdf");
foreach (var page in pdf.Pages)
    Console.WriteLine(page.ExtractText().GetText(PdfTextMode.ReadingOrder).Trim());
```

```text
Wrote report.pdf
Summary

Page 1 of 2
Details

Page 2 of 2
```

If the format has an unknown placeholder or an unbalanced brace, `PageNumberText` throws `ArgumentException` before the file is saved.

## Variations

Replace the footer call in the full program. For “1/2” and “2/2” at the end of the footer:

```csharp
page.Footer().PageNumberText("{page}/{total}");
```

For a separate total for each section, such as “1 of 3” in a three-page chapter of a longer report:

```csharp
page.Footer()
    .PageNumberText("{page} of {sectionTotal}")
    .AlignCenter();
```

## Related recipes

- [Create a PDF in C#](https://recursiveai.org/onepdf/cookbook/create-pdf/)
- [Generate a PDF table in C#](https://recursiveai.org/onepdf/cookbook/generate-tables/)

## The short answer

Use `PageNumberText("Page {page} of {total}")` in a layout footer. OnePDF lays out the document once and draws the text after the count is known. The same footer definition is used on each page in the section, so adding a new page changes the total throughout the document. This method is for PDFs you generate with Layout; it does not stamp a footer onto an existing PDF.

## How do I show “Page X of Y” in a PDF footer in C#?

Put the call in `page.Footer()`, not `page.Content()`. Content flows between pages; the footer is the slot reserved at the bottom of each page. `PageNumberText` fits one line and aligns to the end of that slot by default. Use `AlignCenter()` to center the finished line or `AlignRight()` to keep it on the physical right, even in a right-to-left layout. Use `page.Header()` instead if the number belongs above the content.

The line is measured with the final digits in a normal, fixed-width footer slot. That matters if the page count grows from 9 to 10: there’s no placeholder gap in “Page 1 of 9,” and the longer “Page 1 of 10” is centered by its own width. In an auto-sized row item, the engine instead estimates the placeholder widths using `ReservedDigits`, which defaults to four. If the finished line is wider than its box, the engine condenses it and reports a note. See the [layout text API](https://recursiveai.org/onepdf/docs/layout/#text).

## How do automatic page numbers work across PDF sections?

Each `doc.Page(...)` call starts a section on a new page. `{page}` uses that section’s numbering style. `{total}` always counts every page in the document and uses decimal numbers. `{sectionTotal}` counts just the pages in the current section, using its numbering style.

For a document with two pages of front matter followed by a three-page report, `{total}` is `5` in both sections. `{sectionTotal}` in the report is `3`. If the report’s page numbers continue from the front matter, its first page number is `3`; if you reset it to `1` with `StartNumber(1)`, you can show “1 of 3.” Decide whether the printed number should tell readers where they are in the entire PDF or just in the chapter before choosing a total.
