Export pages 2 and 3 from report.pdf in the example zip without splitting it.
dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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.
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);
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:
var pages = PdfHtmlPageRanges.FromIndices([0, 2]);To export page 3 through the end, leave the end of the range open instead:
var pages = PdfHtmlPageRanges.Parse("3-");Related recipes
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.