When OnePDF can’t safely redact a page, it throws PdfRedactionException; PageIndex counts pages from zero. Use original.pdf from the zip. MaxOperatorsPerPage is set to 1 to trigger the error. An operator is one PDF drawing instruction.
dotnet add package RecursiveAI.OnePdfRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf; var options = new PdfOpenOptions { AllowEditing = true, Limits = new() { MaxOperatorsPerPage = 1 } }; using var doc = PdfDocument.Open("original.pdf", options); var area = new PdfRectangle(70, 655, 240, 676); try { // Zero-based index 1 is Page 2. doc.Redact([new(1, area)]); doc.Save("redacted.pdf"); } catch (PdfRedactionException error) { int page = error.PageIndex + 1; Console.WriteLine( $"Page {page}: not published."); Console.WriteLine(error.Message); }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Page 2: not published. Page 2 could not be redacted safely: the page has more content operators than the limit allows.
The PdfRectangle numbers are the left, bottom, right, and top edges in PDF points from the bottom-left corner of the sample page. The rectangle covers “Demo account 1234” and leaves “Keep this line” outside. The limit is deliberately low to exercise the catch block. The redaction guide says that when a page’s content cannot be read completely, “the call throws PdfRedactionException naming the page and the document is unchanged.” The example doesn’t write an output on this path.
Variations
Let the same valid sample use the normal limits. Remove the Limits assignment and keep the catch block:
var options = new PdfOpenOptions { AllowEditing = true };
The demo now writes redacted.pdf without entering the catch block.
Related recipes
The short answer
Catch PdfRedactionException around Redact (or ApplyRedactAnnotations) and read error.PageIndex. Add one for a page number in a message. Keep saving and publishing on the success path; a failed call leaves the document unchanged, so saving from the catch block would risk writing an unredacted copy.
Why won’t my PDF let me apply redactions?
Redaction needs to read enough of the page to decide which content intersects each rectangle. An undecodable content stream, an unreadable form or annotation appearance, or an interpretation limit can stop that work. In the example the PDF is valid, but the limit is smaller than the page needs. In a real job, use the exception message to diagnose the cause and get a readable source or review your processing policy before retrying. Don’t treat an unreadable page as if it were empty.
How do I find the page where PDF redaction failed?
PageIndex is the index in the entire document, not the position in the list of regions. If you redact only the ninth page, its index is still 8. Keep the index when looking up the page in code; show the index plus one when telling someone which physical page to inspect. A PDF can also have printed labels or page labels that differ from physical page numbers, so label your message if the distinction matters.
What happens to other pages if one redaction fails?
OnePDF plans the page rewrites first and applies them only after every planned page succeeds. A failure partway through isn’t a partially redacted document. It also doesn’t delete any output left on disk by a previous job. In a batch pipeline, give each attempt its own candidate path and publish only from a successful attempt, rather than treating the presence of a file at a reused path as evidence of success.