IRONSOFTWAREHOME

Faster Headers and Footers on Large PDFs

Curtis Chau
Curtis Chau
Updated: July 3, 2026

Adding headers and footers to a very large PDF in a single pass is slow. Process the document in fixed-size page chunks, render each batch, then merge the parts back together to reduce total rendering time while keeping page numbers continuous across the whole document.

Solution

1. Split the page indexes into batches

Pick a chunk size and group the document's page indexes into fixed-size batches, for example 300 pages each. Smaller chunks render faster individually but add more merge work at the end.

2. Copy each batch into its own document

Extract the pages for the current batch with CopyPages, giving you a standalone document to render.

3. Render headers and footers with continuous numbering

Set FirstPageNumber to the chunk's starting page so numbering carries across the whole document instead of restarting at 1 in each chunk.

4. Save each chunk and dispose it

Write each chunk to a temp file and dispose it. Releasing the chunk before the next batch keeps memory from climbing across the run.

5. Merge the chunks into the final PDF

Re-open the temp files, append them in order with AppendPdf, save the result, then delete the temp files.

using IronPdf;
var pdf = PdfDocument.FromFile("input.pdf");
string headerHtml = "<div style='font-size:10px;'>Header</div>";
string footerHtml = "<div style='text-align:right;font-size:10px;'>Page {page} of {total-pages}</div>";
int chunkSize = 300;
var pageChunks = Enumerable.Range(0, pdf.PageCount)
    .GroupBy(i => i / chunkSize)
    .Select(g => g.ToArray())
    .ToList();
var tempFiles = new List<string>();
foreach (var indexes in pageChunks)
{
    var chunkPdf = pdf.CopyPages(indexes);
    int globalStartPage = indexes.First() + 1;
    chunkPdf.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
    {
        FirstPageNumber = globalStartPage,          // keeps numbering continuous
        HtmlHeader = new HtmlHeaderFooter { HtmlFragment = headerHtml, MaxHeight = 40 },
        HtmlFooter = new HtmlHeaderFooter { HtmlFragment = footerHtml, MaxHeight = 40 }
    });
    string tempFile = Path.GetTempFileName() + ".pdf";
    chunkPdf.SaveAs(tempFile);
    chunkPdf.Dispose();
    tempFiles.Add(tempFile);
}
// Merge the chunks back into one document.
var finalPdf = new PdfDocument(tempFiles.First());
foreach (var file in tempFiles.Skip(1))
{
    using var part = new PdfDocument(file);
    finalPdf.AppendPdf(part);
}
finalPdf.SaveAs("output.pdf");
finalPdf.Dispose();
// Clean up temp files.
foreach (var file in tempFiles)
    File.Delete(file);
C#

FirstPageNumber is the key: it is set to each chunk's global starting page, so a footer like Page {page} of {total-pages} stays consistent from the first chunk to the last.

Please note: Without FirstPageNumber, every chunk restarts its numbering at page 1.

Debug Tips

  • Tune the chunk size: the value above (300) is adjustable, and the point at which chunking starts to pay off depends on your workload.
  • Account for the merge pass: total time is per-chunk rendering plus the final merge, so factor that extra pass into any comparison.
  • Keep memory in check on very large jobs: periodically forcing garbage collection during the merge and tuning the browser pool with BrowserPool.MaxIdleTabs and MaxDynamicHFPagesPerBatch can help.
Curtis Chau
Technical Writer

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 20,389,208Version:2026.8just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required

Try IronPDF for Free

Get Set Up in 5 Minutes

C# PDF DLL

Download DLL

Download Now

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"
C# NuGet Library for PDF

Install with NuGet

                  Install-Package IronPdf
                
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
Key in blue circle

Get your free 30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Book your free Live Demo
Booking Badge related to IronPDF Product Demo

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required