Recursive AI

Use a different first-page header in HTML to PDF with C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Set the repeating header in @page and override it only on page one with @page :first.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

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

C#
using OneRender.Pdf;
 
var html = """
<style>
@page { margin: 20mm;
  @top-center { content: "Report" } }
@page :first {
  @top-center { content: "Cover" } }
h2 { break-before: page }
</style>
<h1>Summary</h1><h2>Details</h2>
""";
using var pdf = File.Create("report.pdf");
var result = HtmlPdf.ToPdf(html, pdf);
Console.WriteLine($"{result.PageCount} pages");

Writes report.pdf with “Cover” on page one and “Report” on page two.

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
2 pages

The HTML is in the program; no files are needed as input. Open report.pdf and look above “Summary” and “Details.” Margin boxes are areas in the page margins for running headers and footers: @top-center is above the body, and @bottom-right is below it. If you see only “Cover,” check the page count: this example uses break-before: page to put “Details” on a second page.

Variations

Hide the header on page one instead. Replace the example’s @page :first rule with:

CSS
@page :first {
  @top-center { content: none }
}

Change the space above the body on page one. Set margin-top on the same page rule, not inside @top-center:

CSS
@page :first {
  margin-top: 30mm;
  @top-center { content: "Cover" }
}

The short answer

To print a different header on the first page, give @page a repeating header in a margin box such as @top-center. Then give @page :first a different content value in that same box. Pass the entire HTML document to HtmlPdf.ToPdf once. Page one gets the override; later pages keep the default. There’s no need to render the cover separately and merge two PDFs.

Why is my header the same on every page?

A plain @page rule applies to the first page as well as the rest. The :first selector targets the first printed page, not the first heading or a particular <header> in the HTML. Changing a heading in the body won’t change literal text in a margin box. Put the alternate text in the content declaration for the same box: overriding @top-left won’t replace a header set in @top-center.

On a first page named cover, a header set in @page cover overrides the same header in @page :first, no matter which rule comes first.

How do I hide the header on my first page?

Set content: none for the header’s margin box on @page :first, as in the first variation. Omit that declaration and the default header still applies. With this override only @top-center changes, so footers in bottom margin boxes keep their regular content. The first page also keeps its top margin unless you change it separately. Use the second variation’s margin-top if the cover needs different spacing above the body.

When a cover title carries information readers need, put it in the body as a heading. The PDF output docs explain that running headers and page numbers are drawn as artifacts and aren’t part of the PDF’s structure tree.

All onerender recipes