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

[Internal] Why does SaveAsPdfUA() throw an AccessViolationException after calling SaveAsPdfA()?

Calling SaveAsPdfUA() on the same PdfDocument used for SaveAsPdfA() can cause a memory access error. This is due to internal state corruption and can be avoided by using a fresh PdfDocument instance.

If you're encountering the following error when calling SaveAsPdfUA() after SaveAsPdfA():
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

This occurs because the internal state of the original PdfDocument is modified or disposed during the SaveAsPdfA() process, making it unsafe to reuse for SaveAsPdfUA().

Workaround

To avoid this exception, you can create a new PdfDocument from the original document’s binary data before saving the first PDF/A version. Use the original for PDF/A, and the new one for PDF/UA:

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, world</h1>");
var newPdf = new PdfDocument(pdf.BinaryData);  // Clone before first save

pdf.SaveAsPdfA("pdfa.pdf");    // Save original as PDF/A
newPdf.SaveAsPdfUA("pdfua.pdf");  // Use fresh copy for PDF/UA

This workaround ensures that each save operation works with a clean and stable memory state.