Skip to content
English
  • There are no suggestions because the search field is empty.

IronPDF: How to Add Headers and Footers Faster by Processing in Chunks for Large PDFs

Overview

Adding headers and footers to a very large PDF in a single pass can be slow. This article shows how to process the document in fixed-size page chunks and merge the results, which reduces the rendering time. After following it, you can add headers and footers to a large document with continuous page numbering across all chunks.


Version Metadata

  • Minimum Version: N/A

  • Superseded-in Version: N/A


Steps

  1. Choose a chunk size and split the page indexes into batches. Group the document's page indexes into fixed-size batches (for example, 300 pages each).
  2. Copy each batch into its own document. Use CopyPages to extract the pages for the current batch.
  3. Add the headers and footers to the chunk, with continuous page numbering. Set FirstPageNumber to the chunk's starting page so that page numbers stay continuous across the whole document instead of restarting at 1 in each chunk.
  4. Save each chunk to a temp file and dispose it. Writing each chunk out and disposing it frees memory before the next batch.
  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);

Notes and Limitations

  • FirstPageNumber is what keeps page numbers continuous across chunks. Without it, each chunk would restart at page 1.
  • The chunk size (300 above) is tunable, and the point at which chunking begins to pay off depends on the workload.
  • Merging adds a final pass, so total time is the sum of per-chunk rendering plus the merge.
  • For very large jobs, periodically forcing garbage collection during the merge and tuning the browser pool (BrowserPool.MaxIdleTabs, MaxDynamicHFPagesPerBatch) can help keep memory in check.