---
title: "Find the failed PDF redaction page in C#"
url: https://recursiveai.org/onepdf/cookbook/catch-pdf-redaction-exception/
description: "Catch PdfRedactionException in C#, turn its zero-based PageIndex into a page number, and skip writing the output when redaction fails."
version: "1.26.1"
---

# Find the failed PDF redaction page in C#

**OnePDF** is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf) · [GitHub](https://github.com/recursive-ai-oss/onepdf-csharp) · [Docs](https://recursiveai.org/onepdf/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf/cookbook/catch-pdf-redaction-exception/catch-pdf-redaction-exception.zip)

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.

```shell
dotnet add package RecursiveAI.OnePdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onepdf/cookbook/catch-pdf-redaction-exception/catch-pdf-redaction-exception.zip) and `dotnet run`.

```text
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](https://recursiveai.org/onepdf/docs/redaction/) 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:

```csharp
var options = new PdfOpenOptions
{
    AllowEditing = true
};
```

The demo now writes `redacted.pdf` without entering the catch block.

## Related recipes

- [Redact text and images in a PDF](https://recursiveai.org/onepdf/cookbook/redact/)
- [Redo redaction from the original PDF](https://recursiveai.org/onepdf/cookbook/redo-redaction-from-original-pdf/)

## 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.
