By default (semantic mode), OnePdf.Html turns recognized tables with selectable text into HTML rows and cells, not pictures.
dotnet add package RecursiveAI.OnePdf.HtmlRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html; PdfHtml.Convert("inventory.pdf", "inventory.html");
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.
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
using OnePdf.Html; PdfHtml.Convert("inventory.pdf", "inventory.html"); Console.WriteLine("Wrote inventory.html.");
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:
<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:
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:
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
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 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.