IRONSOFTWAREHOME

AccessViolationException After InsertPdf with HTML Headers/Footers

Curtis Chau
Curtis Chau
Updated: July 6, 2026

An AccessViolationException is thrown when calling AddHtmlHeaders on a PdfDocument after InsertPdf was used to merge pages from another document. The exception does not occur in Debug builds or when a debugger is attached.

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at IronPdf.PdfDocument.AddHtmlHeaders(...)
Text

In Release builds, the .NET JIT optimizer allows the garbage collector to reclaim managed objects before their lexical scope ends, when it can prove those objects will not be referenced again by managed code. A PdfDocument passed to InsertPdf can be collected while IronPDF's native Chrome layer is still reading it. The subsequent AddHtmlHeaders call then accesses freed memory, producing the access violation.

Solutions

Reorder operations so headers and footers are written to the base document before any pages are inserted:

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = "<div style='text-align:right; font-size:10px'>Page {page} of {total-pages}</div>"
};

var basePdf = renderer.RenderHtmlAsPdf(baseHtml);

basePdf.AddHtmlHeaders(renderer.RenderingOptions.HtmlHeader);

var supplementPdf = PdfDocument.FromFile("supplement.pdf");
basePdf.InsertPdf(supplementPdf, 0);

basePdf.SaveAs("output.pdf");
C#

Option 2: Use GC.KeepAlive to prevent early collection

If the operation order cannot change, extend the managed lifetime of the source PdfDocument past the AddHtmlHeaders call:

var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = "<div style='text-align:right; font-size:10px'>Page {page} of {total-pages}</div>"
};

var basePdf = renderer.RenderHtmlAsPdf(baseHtml);
var supplementPdf = PdfDocument.FromFile("supplement.pdf");

basePdf.InsertPdf(supplementPdf, 0);
basePdf.AddHtmlHeaders(renderer.RenderingOptions.HtmlHeader);

GC.KeepAlive(supplementPdf);

basePdf.SaveAs("output.pdf");
C#

GC.KeepAlive(supplementPdf) placed after AddHtmlHeaders prevents the JIT from scheduling collection of supplementPdf before the native merge is complete.

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,878,335Version: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.

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