---
title: "HTML to PDF with headers, footers, and page numbers in C#"
url: https://recursiveai.org/onepdf-html/cookbook/add-page-numbers-header-footer-html-to-pdf/
description: "Convert HTML to PDF in C# with a repeating header and a Page X of Y footer. See how to set margins and move the number to a different position."
version: "1.0.0"
---

# HTML to PDF with headers, footers, and page numbers in C#

**OnePdf.Html** is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Html.Render) · [GitHub](https://github.com/recursive-ai-oss/onepdf-html-csharp) · [Docs](https://recursiveai.org/onepdf-html/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf-html/cookbook/add-page-numbers-header-footer-html-to-pdf/add-page-numbers-header-footer-html-to-pdf.zip)

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

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

```csharp
using OnePdf.Html.Render;

var options = new HtmlToPdfOptions {
    Header = HtmlHeaderFooter
        .Centered("Stock report"),
    Footer = HtmlHeaderFooter.PageXOfY(),
    Margins = HtmlMargins.FromInches(1)
};
var result = HtmlToPdf.ConvertFile(
    "report.html", "numbered.pdf", options);
Console.WriteLine($"Pages: {result.PageCount}");
```

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onepdf-html/cookbook/add-page-numbers-header-footer-html-to-pdf/add-page-numbers-header-footer-html-to-pdf.zip) and `dotnet run`.

The snippet writes `numbered.pdf`. Save this as `report.html` next to `Program.cs`, or use the copy in the download. The `break-before` rule starts the second warehouse on a new page.

```html
<!doctype html>
<html lang="en">
<head><title>Stock report</title></head>
<body>
  <h1>North warehouse</h1>
  <p>24 boxes ready to ship.</p>
  <h2 style="break-before: page">South warehouse</h2>
  <p>16 boxes ready to ship.</p>
</body>
</html>
```



`dotnet run` prints:

```text
Pages: 2
```

Open `numbered.pdf`: “Stock report” is at the top of both pages, and the footers read “Page 1 of 2” and “Page 2 of 2.” If a header or footer doesn’t fit, increase the top or bottom margin instead of adding blank lines to the report body.

## Variations

**Put the number on the right and a label on the left.** Replace the `Footer` assignment in the snippet at the top. The converter fills in the span text for each page:

```csharp
Footer = new HtmlHeaderFooter
{
    Left = "Inventory",
    Right = "Page <span class='pageNumber'></span>"
}
```

## Related recipes

- [Convert a web page to PDF](https://recursiveai.org/onepdf-html/cookbook/convert-url-to-pdf/)
- [Convert HTML to PDF/UA](https://recursiveai.org/onepdf-html/cookbook/convert-html-to-accessible-pdf-ua/)

---

## The short answer

Set `Header` and `Footer` on `HtmlToPdfOptions` when you call `HtmlToPdf.ConvertFile` in C#. `HtmlHeaderFooter.Centered` puts a label in the middle of the top margin when used as the header. `HtmlHeaderFooter.PageXOfY()` in the footer puts the current page and the total in the middle of the bottom margin. The numbers come from the final PDF layout, not the number of headings in the HTML.

## How do I repeat a header and footer on every PDF page?

Use `Header` and `Footer` for text that belongs to the printed page rather than to the flow of the document. A heading in the HTML body appears where the content places it; a running header such as “Stock report” shows up in the top margin of each page. Keep the warehouse headings in the body so a reader can tell which section each page covers.

The [header and footer options](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#headers-and-footers) give you three positions: `Left`, `Center`, and `Right`. For a logo and a label in one layout, use [the `Html` property](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#headers-and-footers) instead, with all three position properties left unset. That fragment spans the width of the page area, and its images follow the same resource-loading rules as the rest of the document.

## How do I add page numbers to the right or show only the current page?

Use the `Right` template in the variation. The `pageNumber` span is replaced with the page you’re on; add a `totalPages` span only if you want the denominator too:

```html
Page <span class='pageNumber'></span> of <span class='totalPages'></span>
```

Put those spans in the header template instead if your page number belongs at the top. The special classes are for the margin template, not regular paragraphs in the input HTML.

For a label that changes when the document changes, a span with class `title` inserts the document title. A span with class `date` inserts the date string you supply in `HtmlHeaderFooter.Date`; it doesn’t read the clock. That lets you print a report’s reporting date instead of the day someone happened to generate the file.

## How do I set PDF margins so the footer fits?

The top and bottom margins are the space available to the templates. More space at the bottom gives the footer room, but leaves less vertical space for the body and may change where the content breaks. If you only need more room below the content, use the four-argument `HtmlMargins.FromInches` or `FromMillimeters` overload to set the bottom separately. The order is top, right, bottom, left.

If the report already has an `@page` margin in CSS, the [explicit margins you pass in C#](https://recursiveai.org/onepdf-html/docs/html-to-pdf/#pages-size-orientation-and-margins) take precedence. Choose either the report’s print stylesheet or the conversion options as the place where you maintain margins, and render a multipage sample before reusing the settings in another report.
