---
title: "Extract tables from a PDF as HTML in C#"
url: https://recursiveai.org/onepdf-html/cookbook/extract-pdf-tables-as-html/
description: "Extract tables from a PDF as HTML in C# with OnePdf.Html. Keep rows and cells as HTML elements instead of turning the table into a picture."
version: "1.0.0"
---

# Extract tables from a PDF as 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/extract-pdf-tables-as-html/extract-pdf-tables-as-html.zip)

By default (semantic mode), OnePdf.Html turns recognized tables with selectable text into HTML rows and cells, not pictures.

```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("inventory.pdf",
    "inventory.html");
```

[The example](https://recursiveai.org/onepdf-html/cookbook/extract-pdf-tables-as-html/extract-pdf-tables-as-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`.

This writes `inventory.html`. The download includes `inventory.pdf`, a one-page PDF with a heading and this table:

| Item | In stock |
|---|---:|
| Notebooks | 24 |
| Pencils | 60 |

## The full program

```csharp
using OnePdf.Html;

PdfHtml.Convert("inventory.pdf", "inventory.html");
Console.WriteLine("Wrote inventory.html.");
```

```text
Wrote inventory.html.
```

Open `inventory.html` in a browser, or view its source. The first two rows of its `<table>` are shown below, with line breaks added for readability:

```html
<tr>
  <th scope="col"><span class="wf0">Item</span></th>
  <th scope="col"><span class="wf0">In stock</span></th>
</tr>
<tr>
  <td><span class="wf1">Notebooks</span></td>
  <td><span class="wf1">24</span></td>
</tr>
```

If an untagged grid isn’t recognized as a table, look for its text in the HTML before assuming the conversion lost it.

## Variations

Replace the `Convert` line in the snippet at the top.

**Get the converted document as a string** to inspect the markup without writing a file:

```csharp
string html = PdfHtml.Convert("inventory.pdf");
```

**Get only the table markup.** Use OneHtml, which is already a dependency of `RecursiveAI.OnePdf.Html`. Add `using RecursiveAI.OneHtml;` at the top, then replace the conversion call with:

```csharp
string html = PdfHtml.Convert("inventory.pdf");
var doc = Document.Parse(html);
foreach (var table in doc.Root.QuerySelectorAll("table"))
    Console.WriteLine(doc.SerializeNode(table));
```

## Related recipes

- [Convert a scanned PDF to HTML with OCR](https://recursiveai.org/onepdf-html/cookbook/convert-scanned-pdf-to-html-with-ocr/)
- [Convert a PDF to HTML, one file per page](https://recursiveai.org/onepdf-html/cookbook/convert-pdf-to-html-page-per-file/)

## The short answer

Call `PdfHtml.Convert` in its default semantic mode. It writes a full HTML document with `<table>`, `<tr>`, `<th>`, and `<td>` elements for the tables it recognizes. It doesn’t write a separate file for each table. If you need only the tables, take the `table` elements from the converted document rather than using the page’s fixed-layout output.

## Is there a way to extract tables from a PDF that has no table tags?

Yes, if the converter can infer the structure from the page. A tagged PDF can tell the converter which content is a row, a header cell, or a data cell. With an untagged PDF, OnePdf.Html looks at drawn rules and fills for grids and word alignment for tables without borders. A table doesn’t need lines around every cell, but aligned text by itself isn’t necessarily a table. The converter deliberately avoids treating prose columns, charts, and mostly empty grids as tables.

Search for `<table` in the output source, then spot-check that each value is under the right header. Finding all the words isn’t the same as recovering the rows. If you control how the PDF is made, use a PDF export that preserves table structure instead of flattening the grid to a picture.

A scanned table is a different case: a photo of a grid may have no text layer at all. Use [OCR with the optional package](https://recursiveai.org/onepdf-html/docs/pdf-to-html/#text-sources-and-ocr) to recognize text first, then check that the result has rows and cells. Recognizing the words does not guarantee that the layout gives the converter enough information to reconstruct the grid.

## How do you convert a PDF table to HTML without losing its structure?

Choose semantic mode when the result needs to behave like a table on the web. The converter maps tagged table headers to `<th>` and data cells to `<td>`, with `colspan` and `rowspan` for merged cells. In the sample, `scope="col"` says the headers apply to columns. The output reflows, so don’t expect the PDF’s exact column widths or line breaks. Fixed-layout mode instead writes positioned content to preserve the look of the page.

Check where the table ends if it runs across a page break. Semantic conversion can join a continued table with matching columns into one HTML table on the page where it started. If the next PDF page repeats the header, the joined HTML table does not repeat it. That makes the result one grid to read or import, even when the PDF needed several printed pages.
