Recursive AI

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.

Terminal
dotnet add package RecursiveAI.OnePdf.Html.Render

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

C#
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 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:

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

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.

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 give you three positions: Left, Center, and Right. For a logo and a label in one layout, use the Html property 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.

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

All onepdf-html recipes