---
title: "Convert selected PDF pages to HTML in C#"
url: https://recursiveai.org/onepdf-html/cookbook/convert-selected-pdf-pages-to-html/
description: "Convert selected PDF pages to HTML in C# without splitting the file. Set a page range or a list of page indices with OnePdf.Html."
version: "1.0.0"
---

# Convert selected PDF pages to HTML 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/convert-selected-pdf-pages-to-html/convert-selected-pdf-pages-to-html.zip)

Export pages 2 and 3 from `report.pdf` in the example zip without splitting it.

```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;
var pages = PdfHtmlPageRanges.Parse("2-3");
PdfHtml.Convert("report.pdf", "selected.html",
    new() { PageRanges = pages });
```

Writes `selected.html` with pages 2 and 3.

[The example](https://recursiveai.org/onepdf-html/cookbook/convert-selected-pdf-pages-to-html/convert-selected-pdf-pages-to-html.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`.

## The full program

`report.pdf` has four pages: **Introduction**, **Sales**, **Inventory**, and **Appendix**. The program wraps each exported page in a `<section>` and prints its HTML ID, so you can see which two pages made it into the file.

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

var options = new PdfHtmlOptions
{
    PageRanges = PdfHtmlPageRanges.Parse("2-3"),
    PageBoundaries = PdfHtmlPageBoundaries.Section,
};
var result = PdfHtml.Convert("report.pdf", "selected.html", options);
Console.WriteLine($"Wrote selected.html ({result.PageCount} pages).");

string html = File.ReadAllText("selected.html");
var sections = Regex.Matches(html, "<section id=\"(page-[0-9]+)\"");
Console.WriteLine("Page IDs: " + string.Join(", ", sections.Select(m => m.Groups[1].Value)));
foreach (var diagnostic in result.Diagnostics)
    if (diagnostic.Code.Length != 0)
        Console.WriteLine(diagnostic.Code);
```

```text
Wrote selected.html (2 pages).
Page IDs: page-2, page-3
```

A malformed range throws `ArgumentException`; pages beyond the end of the PDF are skipped with a `pages-out-of-range` diagnostic.

## Variations

Replace the `var pages` line at the top to export the first and third pages. `FromIndices` starts at zero:

```csharp
var pages = PdfHtmlPageRanges.FromIndices([0, 2]);
```

To export page 3 through the end, leave the end of the range open instead:

```csharp
var pages = PdfHtmlPageRanges.Parse("3-");
```

## Related recipes

- [Convert PDF to HTML in C#](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html/)
- [Convert PDF to HTML and keep the layout](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-keeping-layout/)

## The short answer

Pass a page selection to `PdfHtmlOptions.PageRanges` when you call `PdfHtml.Convert`. Use `Parse("2-3")` for the same page numbers a PDF reader shows in its page counter, or `FromIndices` when your code already has zero-based page indices. Both modes accept the option: semantic HTML reflows the text; fixed layout keeps the original page geometry.

## How can I extract a range of pages from a PDF as HTML?

Choose the range when you export, not in a separate PDF editing step. The converter accepts a dash for an inclusive range and a comma to combine ranges with individual pages. For instance, `1-3,7,10-` selects pages 1 through 3, page 7, and page 10 through the end. An open start such as `-3` starts at page 1. The converter writes each selected page once, in the original document order, even when the ranges overlap or you list them out of order.

If an app collects page numbers from a person, keep that numbering and pass the range string to `Parse`. If the app gets indices from a zero-based list, pass the indices to `FromIndices` without adding one. Mixing those two conventions can give you a valid HTML document with the wrong content.

## How does converting a multi-page PDF to HTML affect page links?

In one HTML file, selected pages keep their original number in anchors such as `#page-2`. Exporting page 2 first doesn’t make it `#page-1`. The default semantic output marks the boundary with an empty `<span role="doc-pagebreak">`; the downloaded program makes the bounds of each page explicit with `<section>` instead. Fixed layout wraps each page in a box the size of the PDF page.

An internal link can point to another page included in the export, but not to one you left out. If your excerpt relies on a contents page or a reference to an appendix, include the destination in the range. If you want separate HTML files instead of a single document, `PdfHtmlOutputLayout.PagePerFile` changes where the selected pages are written, not which pages you select.
