---
title: "Limit PDF filter chains when rendering in C#"
url: https://recursiveai.org/oneimage/cookbook/limit-pdf-filter-chains-when-rendering/
description: "Set a maximum PDF stream filter count in C# with OneImage’s PDF renderer. See a two-filter page rejected at one filter and rendered at two."
version: "1.11.4"
---

# Limit PDF filter chains when rendering in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneImage.Pdf) · [GitHub](https://github.com/recursive-ai-oss/oneimage-csharp) · [Docs](https://recursiveai.org/oneimage/docs/) · [Download example (.zip)](https://recursiveai.org/oneimage/cookbook/limit-pdf-filter-chains-when-rendering/limit-pdf-filter-chains-when-rendering.zip)

A PDF filter is a step that decodes stored data, such as decompressing drawing commands. A long filter chain can slow a render and use extra memory; also set `MaxDecodedStreamBytes` in `PdfRenderOptions.DocumentLimits` to cap how much data a stream can decode to. Try `square.pdf` from the download: its drawing commands use two filters.

```shell
dotnet add package RecursiveAI.OneImage.Pdf
```

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

```csharp
using OneImage;
using OneImage.Pdf;

var limits = new PdfRenderLimits
    { MaxFilterChain = 2 };
var format = new PdfFormat(new()
    { Limits = limits });
var config = new ImageConfiguration();
config.Add(format);
var pdf = Image.Load("square.pdf",
    new() { Configuration = config });
pdf.Frames[0].ToImage().Save("page.png");
```

[The example](https://recursiveai.org/oneimage/cookbook/limit-pdf-filter-chains-when-rendering/limit-pdf-filter-chains-when-rendering.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Writes `page.png` when the first page fits the cap. A page refused by this cap throws `ImageLimitException`.

## The full program

`square.pdf` draws one gray square on a 72 × 72 point page. The page’s content stream contains the drawing operators `0.5 g 18 18 36 36 re f`, stored with this filter array:

```text
/Filter [/ASCIIHexDecode /FlateDecode]
```

```csharp
using OneImage;
using OneImage.Pdf;

Render(maxFilters: 1);
Render(maxFilters: 2);

static void Render(int maxFilters)
{
    var limits = new PdfRenderLimits { MaxFilterChain = maxFilters };
    var format = new PdfFormat(new PdfRenderOptions { Limits = limits });
    var config = new ImageConfiguration().Add(format);
    try
    {
        var pdf = Image.Load("square.pdf", new() { Configuration = config });
        pdf.Frames[0].ToImage().Save("page.png");
        Console.WriteLine($"Cap {maxFilters}: wrote page.png");
    }
    catch (ImageLimitException ex)
    {
        Console.WriteLine($"Cap {maxFilters}: {ex.Message}");
    }
}
```

```text
Cap 1: A stream names 2 filters, more than the 1 allowed by PdfRenderLimits.MaxFilterChain.
Cap 2: wrote page.png
```

A checked stream above the cap raises `ImageLimitException` when you request the page’s pixels; opening the PDF is not enough to exercise this check. This is a rendering limit, not a test that a PDF is safe to use elsewhere.

## Variations

**Keep the renderer’s default cap of 16.** Replace the `format` assignment in the snippet at the top and omit `limits`:

```csharp
var format = new PdfFormat(new PdfRenderOptions());
```

## Related recipes

- [Convert a PDF page to an image](https://recursiveai.org/oneimage/cookbook/pdf-to-image/)
- [Set resource limits when reading PDFs directly with OnePDF](https://recursiveai.org/onepdf/cookbook/limit-resource-use-when-reading-untrusted-pdf/)

## The short answer

Put `MaxFilterChain` in the `PdfRenderLimits` passed through `PdfRenderOptions.Limits` when constructing `PdfFormat`. Request the page’s pixels, for example by saving it as PNG. Before interpreting the page, the renderer checks the page’s content streams and form and image streams (XObjects) reachable through its resources. A chain longer than the cap raises `ImageLimitException`; a chain exactly at the cap is allowed by this check.

## What is a filter in a PDF content stream?

A PDF stream holds bytes, such as instructions to paint a page or pixels for an embedded image. The `/Filter` entry tells the PDF reader how to turn the stored bytes back into usable data. As described in [ISO 32000-1, §7.4](https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf), an array lists filters in decode order. In the download, `ASCIIHexDecode` turns text digits back into bytes, then `FlateDecode` decompresses those bytes into the drawing commands for the square. Both filters are valid; the cap of one is intentionally too strict for this input.

The cap counts entries on each stream, not the total number of streams on a page. A page can have several individually acceptable image streams. Conversely, a named resource can be checked even if a drawing command does not use it. Treat a refusal as a policy decision about the render, not as a finding that the PDF is malformed.

## Does a filter limit also cap decoded stream size?

No. Filter count and decoded byte size measure different things. A stream with a single filter can still produce more bytes than your application allows. `PdfRenderOptions.DocumentLimits` controls the `PdfLimits` passed to the underlying OnePDF reader, including `MaxDecodedStreamBytes`. The filter count setting is on the OneImage renderer, not a `PdfDocument.Open` option, so it does not change what a separate call directly to OnePDF allows. Choose the renderer and document limits together when the same request both reads and rasterizes the PDF.
