IRONSOFTWAREHOME

Headers and Footers on Large PDFs in Chunks

Curtis Chau
Curtis Chau
Updated: August 21, 2026

Adding headers and footers to a very large PDF in a single pass can be slow. Process the document in fixed-size page chunks, then merge the results, to cut 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 (300 pages each below). This decides how many pages each rendering pass has to handle.

2. Copy each batch into its own document

Extract the pages for the current batch with CopyPages so each chunk is an independent PdfDocument.

3. Add headers and footers with continuous numbering

Set FirstPageNumber to the chunk's starting page. That keeps page numbers running across the whole document instead of restarting at 1 in each chunk.

4. Save each chunk and dispose it

Write each chunk out to a temp file and dispose it. Freeing the chunk before the next batch keeps memory in check.

5. Merge the chunks and save 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#

globalStartPage is derived from the first index in each batch, so FirstPageNumber picks up exactly where the previous chunk left off.

Notes and Limitations

  • Continuous numbering: FirstPageNumber is what carries page numbers across chunks. Without it, each chunk restarts at page 1.
  • Chunk size is tunable: 300 is a starting point; the size where chunking begins to pay off depends on the workload.
  • Merging adds a pass: total time is the sum of per-chunk rendering plus the final merge.
  • Very large jobs: periodically forcing garbage collection during the merge and tuning the browser pool (BrowserPool.MaxIdleTabs, MaxDynamicHFPagesPerBatch) can help keep memory under control.
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 21,228,841Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > 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
C# PDF DLL
Download DLL

Version: 2026.9

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"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • 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

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
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > 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
C# PDF DLL
Download DLL

Version: 2026.9

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"

Licenses from $999