---
title: "Set raster fallback DPI for PDF-to-HTML export in C#"
url: https://recursiveai.org/onepdf-html/cookbook/set-raster-resolution-in-pdf-to-html-output/
description: "Set the DPI of PDF-to-HTML raster fallback images in C#. Change picture resolution without changing the fixed-layout page size."
version: "1.0.0"
---

# Set raster fallback DPI for PDF-to-HTML export in C#

**OnePdf.Html** is a free, open-source .NET library from Recursive AI that converts PDF to HTML and HTML to PDF. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePdf.Html) · [GitHub](https://github.com/recursive-ai-oss/onepdf-html-csharp) · [Docs](https://recursiveai.org/onepdf-html/docs/) · [Download example (.zip)](https://recursiveai.org/onepdf-html/cookbook/set-raster-resolution-in-pdf-to-html-output/set-raster-resolution-in-pdf-to-html-output.zip)

Set the resolution of fallback pictures in `swatches.pdf` from the example zip, not the page size.

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

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

```csharp
using OnePdf.Html;
PdfHtml.Convert("swatches.pdf", "swatches.html",
    new() { Mode = PdfHtmlMode.FixedLayout,
            RasterDpi = 288 });
```

Writes `swatches.html` with the fallback rendered at 288 DPI.

[The example](https://recursiveai.org/onepdf-html/cookbook/set-raster-resolution-in-pdf-to-html-output/set-raster-resolution-in-pdf-to-html-output.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`.

`RasterDpi` defaults to 144 DPI and accepts 36–600. The converter uses fallback pictures for
features the browser can’t express, such as certain shadings and transparency groups.
SVG graphics are unaffected.

## The full program

`swatches.pdf` has one 240 × 200 point page titled **Overlapping swatches**. Its red and blue rectangles overlap in a transparency group where the later shape replaces earlier paint. That group triggers the picture fallback. The program exports the PDF at two resolutions and prints the CSS page size and converter diagnostic.

```csharp
using OnePdf.Html;
using System.Text.RegularExpressions;

Export(144, "swatches-144.html");
Export(288, "swatches-288.html");

static void Export(int dpi, string output)
{
    var options = new PdfHtmlOptions
    {
        Mode = PdfHtmlMode.FixedLayout,
        RasterDpi = dpi,
    };
    var result = PdfHtml.Convert("swatches.pdf", output, options);
    string html = File.ReadAllText(output);
    var page = Regex.Match(html, @"width:([\d.]+px);height:([\d.]+px)");
    Console.WriteLine($"{output}: page {page.Groups[1]} × {page.Groups[2]}");
    foreach (var diagnostic in result.Diagnostics)
        if (diagnostic.Code.Length != 0)
            Console.WriteLine(diagnostic.Code);
}
```

```text
swatches-144.html: page 320px × 266.667px
raster-fallback
swatches-288.html: page 320px × 266.667px
raster-fallback
```

The embedded picture is 480 × 400 pixels at 144 DPI and 960 × 800 at 288 DPI; the HTML page box stays the same size. Values outside 36–600 throw `ArgumentOutOfRangeException` when conversion starts.

## Variations

To enlarge the displayed page without increasing the fallback DPI, add `Zoom` to the options initializer while keeping `RasterDpi = 288`:

```csharp
Zoom = 1.5,
```

The same PDF now has a 480 × 400 CSS-pixel page box.

## Related recipes

- [Convert PDF to HTML and keep the layout](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/)
- [Lazy-render PDF pages in an HTML export](https://recursiveai.org/onepdf-html/cookbook/lazy-render-pages-in-pdf-to-html-output/)

## The short answer

Set `PdfHtmlOptions.RasterDpi` before calling `PdfHtml.Convert` in fixed-layout mode. The default is 144 DPI (dots per inch). Changing it changes the resolution of fallback pictures, not the size of the page box on screen. Use `Zoom` when you want to change the page box and everything inside it.

## How do I set the DPI for PDF graphics exported as HTML?

First distinguish pictures produced during conversion from graphics the browser can already draw. In fixed layout, ordinary paths and many other marks are emitted as SVG. Raising `RasterDpi` doesn’t redraw SVG at a different resolution or increase the detail available in a source photograph. It controls pictures made for features the browser can’t express in the output, including certain PDF shadings and some transparency groups. The `raster-fallback` diagnostic tells you when the converter took that path. In semantic mode, the option also sets the resolution for vector figures that become pictures.

## Which DPI should I use for the fallback pictures?

Start at the default of 144. Consider raising it if a fallback picture looks too coarse at the size your readers use, or lowering it if the pictures make the HTML too large. Twice the DPI produces twice the width and height in pixels before any cap, so it takes four times as many pixels to represent the same region. The encoded file won’t necessarily grow by the same factor.

A large page can run into `MaxRasterPixels` even when the requested DPI is valid. The converter then draws the page picture at a lower resolution and reports `raster-reduced`. If the picture looks smaller or softer than you expected, look for that diagnostic before raising the DPI again.
