---
title: "Repeat a logo header on every HTML-to-PDF page in C#"
url: https://recursiveai.org/onerender/cookbook/add-running-logo-header-to-html-pdf/
description: "Repeat a logo and styled HTML table at the top of every PDF page in C# with OneRender, using CSS running elements and an @page margin box."
version: "1.6.4"
---

# Repeat a logo header on every HTML-to-PDF page in 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Pdf) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/add-running-logo-header-to-html-pdf/add-running-logo-header-to-html-pdf.zip)

A running element is an HTML element moved out of the page body. A margin box is an area in the page margin that draws it instead.

```shell
dotnet add package RecursiveAI.OneRender.Pdf
```

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

```csharp
using OneRender.Pdf;
var html = """
<style>
@page {
  margin: 65px 40px;
  @top-left { content: element(brand) }
}
header { position: running(brand) }
td { background: #edf5f2 }
h1 + h1 { break-before: page }
</style>
<header><table><tr><td>
<svg width="16" height="16">
<circle cx="8" cy="8" r="7"
        fill="teal" />
</svg></td><td>Northwind Report</td>
</tr></table></header>
<h1>Summary</h1><h1>Details</h1>
""";
File.WriteAllBytes("report.pdf",
    HtmlPdf.ToPdf(html));
Console.WriteLine("Wrote report.pdf");
```

[The example](https://recursiveai.org/onerender/cookbook/add-running-logo-header-to-html-pdf/add-running-logo-header-to-html-pdf.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
Wrote report.pdf
```

Both pages show the circle logo and “Northwind Report” above their main headings. The inline SVG draws the logo without reading a file.

## The full program

This version adds padding around the two table cells and prints the page count.

```csharp
using OneRender.Pdf;

string html = """
<style>
@page { margin: 70px 40px 40px;
  @top-left { content: element(brand) }
}
header { position: running(brand) }
table { background: #edf5f2 }
td { padding: 4px 8px }
h1 + h1 { break-before: page }
</style>
<header><table><tr><td>
<svg width="16" height="16">
  <circle cx="8" cy="8" r="7"
          fill="#23705d" />
</svg>
</td><td>Northwind Report</td></tr>
</table></header>
<h1>Summary</h1><h1>Details</h1>
""";
using var file =
    File.Create("report.pdf");
var pdf = HtmlPdf.ToPdf(html, file);
Console.WriteLine(
    $"Pages: {pdf.PageCount}");
```

```text
Pages: 2
```

If there’s no header on page one, put the running element before the first content in the HTML and check that both CSS declarations use the same name.

## Variations

**Hide the logo header on the cover.** Add this print rule after the existing `@page` rule:

```css
@page :first {
  @top-left { content: none }
}
```

The “Summary” page then has no header, but “Details” still does. The rule removes the header content, not the top margin.

**Load the logo from a PNG file.** In the full program’s `html` string, replace the entire `<svg>…</svg>` with this `<img>` in the same table cell:

```html
<img src="logo.png" alt="Northwind logo"
     width="16" height="16">
```

Run from the unzipped folder, which contains `logo.png`, or put your own PNG with that name in the folder you run from. Replace the `HtmlPdf.ToPdf` call with this code and keep the `Console.WriteLine` below it:

```csharp
byte[] logo =
    File.ReadAllBytes("logo.png");
const string baseUrl =
    "https://render.example/";
var options = new HtmlPdfOptions
{
    BaseUrl = baseUrl,
    Resources = url =>
        url == baseUrl + "logo.png"
            ? logo
            : null
};
var pdf = HtmlPdf.ToPdf(
    html, file, options);
```

`BaseUrl` resolves `logo.png` to `https://render.example/logo.png`. The `Resources` callback supplies the bytes read from disk. OneRender doesn’t fetch the URL.

```text
Pages: 2
```

Both pages now repeat the PNG logo in `report.pdf`.

## Related recipes

- [Add page headers, footers and page numbers](https://recursiveai.org/onerender/cookbook/paged-media/)
- [Load resources when rendering HTML](https://recursiveai.org/onerender/cookbook/load-html-render-resources/)

## The short answer

Use `position: running(brand)` on the HTML header and `content: element(brand)` inside `@page`’s `@top-left` box. In the example the header carries a two-cell table, with the inline logo in one cell and the report title in the other. The same table, including its CSS, appears on both pages even though the second page starts after a forced break.

## How do I make a header repeat on each PDF page?

Place one copy of the header before the first body content. OneRender assigns the running element to the page where the next laid-out box begins. On later pages without a new element of that name, it carries over the last one. If you put the header just before a chapter that starts on page two, it won’t appear on page one.

Set the top margin large enough for the header. The full example gives it `70px` and uses padding inside the table rather than inserting blank paragraphs above each section. When choosing a logo size, leave enough room for the title to wrap if the printable page is narrow. The [PDF docs](https://recursiveai.org/onerender/docs/pdf/) explain how `@page` controls the page and its margins.

## Can I use a repeating table header for the logo?

Use `<thead>` when you want column labels to follow a table across page breaks. It follows that table, not the whole document: pages before and after the table don’t gain a header. Use a running element when the logo must appear above unrelated sections as well. The table in this recipe is only for laying out the logo and title; the page margin box is what makes it repeat.

If you need just a chapter name, `string-set` and `string()` can carry the text without moving its heading out of the body. Use `element()` when you need to preserve markup, images, or styled cells. Tagged PDFs carry a reading structure for assistive technology. The page margin box is marked as an artifact, meaning it’s left out of that structure. Put information the reader needs in the body as well rather than relying on the repeated header alone.

## How do I stop the header from repeating on the first page?

Use the `@page :first` variation above. It only changes the named margin box on the cover. The running element still exists for subsequent pages, and any other margin boxes, such as a page-number footer, keep their own rules.
