Image Header or Footer Without File Size Growth

Curtis Chau
Curtis Chau
Updated: October 29, 2026

Adding a Base64 logo to a PDF header or footer can inflate the output file, because the image gets re-embedded on every page. Split the header or footer into a static image layer and a dynamic text layer so the image is embedded once while the per-page content stays small.

Solution

Separate the content by how often it changes. Put the image and any text that never changes in a static fragment, and put per-page content such as {page} in a dynamic fragment. Give both fragments the same table layout, with matching cell widths, so the dynamic text lands in the correct position over the static layer.

2. Add the Static Fragment First

Call AddHtmlHeadersAndFooters with only the static fragment. The Base64 image is rendered and embedded once.

3. Add the Dynamic Fragment in a Second Pass

Call AddHtmlHeadersAndFooters again with only the dynamic fragment. This pass contains no image, so the per-page content stays small. Rendering the static and dynamic parts together makes the engine treat all of the HTML as dynamic and re-embed the image on every page, which is what causes the file to grow.

using IronPdf;
var pdf = PdfDocument.FromFile("input.pdf");
// Static layer: holds the Base64 image. The page-number cell is left empty.
string staticFooter = @"
<table style='width:100%; table-layout:fixed;'>
  <tr>
    <td style='width:20%;'>
      <img src='data:image/png;base64,iVBORw0KGg...' style='width:150px;' />
    </td>
    <td style='width:80%; text-align:right;'></td>
  </tr>
</table>";
// Dynamic layer: holds only {page}. Same layout, but no image.
string dynamicFooter = $@"
<table style='width:100%; table-layout:fixed;'>
  <tr>
    <td style='width:20%;'></td>
    <td style='width:80%; text-align:right; font-size:10px;'>Page {{page}} of {pdf.PageCount}</td>
  </tr>
</table>";
// Pass 1: add the static image footer (image embedded once).
pdf.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
    HtmlFooter = new HtmlHeaderFooter { HtmlFragment = staticFooter, MaxHeight = 40 }
});
// Pass 2: add the dynamic page-number footer (no image, stays small).
pdf.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
    HtmlFooter = new HtmlHeaderFooter { HtmlFragment = dynamicFooter, MaxHeight = 40 }
});
pdf.SaveAs("output.pdf");

The first pass carries the Base64 image in staticFooter; the second pass carries only Page {page} of ... in dynamicFooter. Both tables use identical cell widths so the page number aligns over the static layer.

4. (Optional) Reference an External Image or SVG

To shrink the output further, point the <img> tag at an external image file or use an SVG instead of inlining the image as a Base64 string.

Notes and Limitations

  • Matching layout required: both fragments must share the same table structure and cell widths, or the dynamic text, such as the page number, will not line up over the static layer.
  • Smaller than Base64: an external image file or an SVG can reduce file size further than a Base64 string.
  • Headers too: the same technique applies to headers. Keep the image in the static pass and place any dynamic header text in the second pass.
Please note: From v2026.6.1, IronPDF handles the header and the footer separately. If the image is in the header and {page} is only in the footer, or the reverse, the image is embedded once and you do not need two passes. Use the two-pass method above when the image and {page} are in the same header or the same footer.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

Read More

Ready to Get Started?

NuGet Downloads 22,198,312Version:2026.11just released