IronPDF: How to Reduce Output File Size with Base64 Image Headers and Footers for Large Multi-page PDFs
Overview
This article shows how to add an image-based header or footer to a PDF without inflating the output file size. After following it, you can keep a Base64 logo on every page while the file stays close to its original size.
Version Metadata
-
Minimum Version: N/A
-
Superseded-in Version: N/A
Steps
- Split the header or footer into two HTML fragments. Put the image — and any text that never changes — in a static fragment. Put per-page content such as
{page}in a dynamic fragment. Give both fragments the same table layout (same cell widths) so the dynamic text lands in the correct position over the static layer. - Add the static fragment first. Call
AddHtmlHeadersAndFooterswith only the static fragment. The Base64 image is rendered and embedded once. - Add the dynamic fragment in a second pass. Call
AddHtmlHeadersAndFootersagain with only the dynamic fragment. Because this pass contains no image, the per-page content stays small. If you render the static and dynamic parts together, the engine treats all of the HTML as dynamic and re-embeds 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 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"); - (Optional) Use an external image file or an SVG instead of a Base64 string.
If you don't need the image inlined as Base64, referencing an image file or using an SVG can reduce the output size further.
Notes and Limitations
- 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.
- An external image file or an SVG can reduce file size further than a Base64 string.
- The same technique applies to headers: keep the image in the static pass and place any dynamic header text in the second pass.