Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

PDF tables: direct-only routes, ODS and CSV, no join across text (#499 review)

cre8ive-marketing430e2214c493
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 6f178546..4295ee0d 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -20,7 +20,7 @@ Filee chooses engines automatically. Settings → *Engines* shows their status,
| **DOCX writer** | built in | **HWPX (and HWP through rhwp), PDF, OFD, HTML, MHTML, EPUB, MOBI/AZW3, FB2, Markdown, TXT, XLSX, XLS, ODS, CSV, PPTX (+ variants) → DOCX** |
| PDF text | built in (PdfPig) | **PDF → TXT, Markdown** (reading order, also two columns; Markdown keeps headings, emphasis, links and the pictures; no OCR) |
| PDF slides | built in (PDFium, PdfPig) | **PDF → PPTX**: a picture of each page on its own slide (looks like the PDF, not editable), the page's text in the speaker notes |
-| PDF tables | built in (PdfPig) | **PDF → XLSX** (#267): each table drawn with lines on its own sheet, numbers and dates as real values, merged cells and column widths kept |
+| PDF tables | built in (PdfPig) | **PDF → XLSX, ODS, CSV** (#267): each table drawn with lines on its own sheet, numbers and dates as real values, merged cells and column widths kept |
| E-mail | built in (MimeKit, MsgReader) | **EML, Outlook MSG → HTML** (headers + body, inline pictures), **TXT**, **ZIP** (the attachments); **MSG → EML**; **MHTML → HTML** (the saved page in one file, pictures inlined; on to PDF, DOCX, HWPX, TXT, Markdown, EPUB from there) |
| Data | built in (System.Text.Json, YamlDotNet, Tomlyn, System.Xml) | **JSON, JSON Lines, YAML, TOML, XML, CSV ↔ each other** (CSV for tables); **vCard, iCalendar ↔ CSV**; spreadsheets, contacts and events reach JSON / YAML / TOML / XML through CSV |
| **E-books** | built in | **EPUB, MOBI/AZW/AZW3/PRC, FB2, HTMLZ, TXTZ → EPUB, HWPX (→ PDF), TXT, HTML, Markdown, FB2, HTMLZ, TXTZ**; HTML, Markdown, TXT, DOCX → EPUB/FB2/HTMLZ/TXTZ; HTML → TXT; comics CBZ/CBR/CB7/CBT/CBC → PDF, EPUB, CBZ; PDF → CBZ; AZW4 → PDF; **EPUB ↔ KEPUB** (Kobo) |
@@ -651,8 +651,8 @@ heights. Text outside the tables is left out, as the XLSX preset's hint says; ta
not found.
- **One sheet per table** ("Table 1", "Table 2" …). A table that goes on at the top of the next page with the same
- columns (widths within 5 %), right after one that ended its page, joins it on one sheet; a header row repeated on
- the new page is left out.
+ columns (widths within 5 %), right after one that ended its page, with no text between them, joins it on one
+ sheet; a header row repeated on the new page is left out.
- **Numbers and dates** become real values with a number format that shows them as the PDF did: `1,234.50`
(`#,##0.00`), `(120)` (negative), `$9.99` / `€1,000.00` / `₩` `£` `¥` `₹` (the symbol in the format), `12.5%`
(`0.0%`), and dates with the year first, `2026-10-23`, `2026/1/5`, `2026. 10. 23.` (Korean style). Everything else
@@ -661,7 +661,8 @@ not found.
- Every cell gets thin borders (the table was drawn with lines), its shading, vertical alignment and bold when all
its text is bold; text wraps. Merged areas keep their borders on every covered cell.
- A PDF with no such table, or a scan (no text), fails with `error.pdf_no_tables` instead of writing an empty
- workbook. Through XLSX, the tables reach ODS and CSV too.
+ workbook. The same tables can be written as ODS, or as CSV (one file per sheet). These edges are direct only: an image
+ made into a PDF has no ruled tables, so no picture reaches a spreadsheet through PDF.
## PDF → PPTX
diff --git a/src/Filee.Engines/Office/SpreadsheetConverter.cs b/src/Filee.Engines/Office/SpreadsheetConverter.cs
index 411c2957..09e031d7 100644
--- a/src/Filee.Engines/Office/SpreadsheetConverter.cs
+++ b/src/Filee.Engines/Office/SpreadsheetConverter.cs
@@ -36,38 +36,45 @@ public sealed class SpreadsheetConverter : IConverter
cancellationToken.ThrowIfCancellationRequested();
progress?.Report(0.6);
- var outputs = new List<string>();
- switch (step.To)
- {
- case "xlsx" or "ods":
- if (step.Output.Allocate(step.To) is { } path)
- {
- if (step.To == "xlsx")
- XlsxWriter.Write(book, path);
- else
- OdsWriter.Write(book, path);
- outputs.Add(path);
- }
- break;
- default:
- // CSV and TSV hold one sheet: a workbook with several sheets becomes one file per sheet.
- var sheets = book.Sheets.Where(s => s.UsedRange() is not null).ToList();
- if (sheets.Count == 0 && book.Sheets.Count > 0)
- sheets = [book.Sheets[0]];
- foreach (var sheet in sheets)
- {
- var file = step.Output.Allocate(step.To, sheets.Count > 1 ? "_" + SafeName(sheet.Name) : null);
- if (file is null)
- continue;
- CsvFormat.Write(sheet, file, step.To == "tsv" ? CsvFormat.Tab : ',');
- outputs.Add(file);
- }
- break;
- }
+ var outputs = Write(book, step);
progress?.Report(1);
return outputs;
}, cancellationToken);
+ /// <summary>Writes a workbook as the step's XLSX, ODS, CSV or TSV (also PdfTablesConverter's output).</summary>
+ internal static List<string> Write(Workbook book, ConversionStep step)
+ {
+ var outputs = new List<string>();
+ switch (step.To)
+ {
+ case "xlsx" or "ods":
+ if (step.Output.Allocate(step.To) is { } path)
+ {
+ if (step.To == "xlsx")
+ XlsxWriter.Write(book, path);
+ else
+ OdsWriter.Write(book, path);
+ outputs.Add(path);
+ }
+ break;
+ default:
+ // CSV and TSV hold one sheet: a workbook with several sheets becomes one file per sheet.
+ var sheets = book.Sheets.Where(s => s.UsedRange() is not null).ToList();
+ if (sheets.Count == 0 && book.Sheets.Count > 0)
+ sheets = [book.Sheets[0]];
+ foreach (var sheet in sheets)
+ {
+ var file = step.Output.Allocate(step.To, sheets.Count > 1 ? "_" + SafeName(sheet.Name) : null);
+ if (file is null)
+ continue;
+ CsvFormat.Write(sheet, file, step.To == "tsv" ? CsvFormat.Tab : ',');
+ outputs.Add(file);
+ }
+ break;
+ }
+ return outputs;
+ }
+
/// <summary>Reads a spreadsheet of a format id this engine accepts.</summary>
internal static Workbook Read(string path, string format) => format switch
{
diff --git a/src/Filee.Engines/Pdf/PdfTablesConverter.cs b/src/Filee.Engines/Pdf/PdfTablesConverter.cs
index fe0e43a1..2b2b6323 100644
--- a/src/Filee.Engines/Pdf/PdfTablesConverter.cs
+++ b/src/Filee.Engines/Pdf/PdfTablesConverter.cs
@@ -16,6 +16,7 @@ using System.Text.RegularExpressions;
using Filee.Core.Conversion;
using Filee.Engines.Hwp.Hwpx;
using Filee.Engines.Infrastructure;
+using Filee.Engines.Office;
using Filee.Engines.Office.Sheets;
namespace Filee.Engines.Pdf;
@@ -30,9 +31,14 @@ public sealed partial class PdfTablesConverter : IConverter
public string DisplayName => "PDF tables (built-in)";
public int MaxParallelism => 0;
- public IReadOnlyList<ConversionEdge> Edges { get; } = [new("pdf", "xlsx")];
+ /// <summary>
+ /// Only from a PDF, and never a step on a longer route: an image turned into a PDF has no ruled tables, so
+ /// jpg → pdf → xlsx would always fail (and show spreadsheet presets on pictures).
+ /// </summary>
+ public IReadOnlyList<ConversionEdge> Edges { get; } =
+ [new("pdf", "xlsx", DirectOnly: true), new("pdf", "ods", DirectOnly: true), new("pdf", "csv", DirectOnly: true)];
- public EngineStatus GetStatus() => EngineStatus.Available("PDF → XLSX", EngineVersions.BuiltIn);
+ public EngineStatus GetStatus() => EngineStatus.Available("PDF → XLSX, ODS, CSV", EngineVersions.BuiltIn);
public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken) =>
Task.Run<IReadOnlyList<string>>(() =>
@@ -43,42 +49,55 @@ public sealed partial class PdfTablesConverter : IConverter
var book = Build(document);
if (book.Sheets.Count == 0)
throw new ConversionRefusedException("error.pdf_no_tables");
- var output = step.Output.Allocate("xlsx");
- if (output is null)
- return [];
- XlsxWriter.Write(book, output);
+ // As the spreadsheet engine writes: CSV gets one file per sheet.
+ var outputs = SpreadsheetConverter.Write(book, step);
progress?.Report(1);
- return [output];
+ return outputs;
}, cancellationToken);
/// <summary>A table and the page it is on (0-based).</summary>
private sealed record Placed(HTable Table, int Page)
{
+ /// <summary>Nothing (no table, no text) comes before it on its page.</summary>
public bool FirstOnPage { get; set; }
+
+ /// <summary>Nothing (no table, no text) comes after it on its page.</summary>
public bool LastOnPage { get; set; }
}
/// <summary>The document's tables as sheets: one per table, a table going on over the next page joined.</summary>
internal static Workbook Build(HDocument document)
{
- var tables = new List<Placed>();
+ // Everything on the pages in order: tables, and null for text (empty paragraphs, such as the one carrying a page
+ // break, don't count), so a table only "goes on" when nothing at all sits between it and the next page's table.
+ var items = new List<(Placed? Table, int Page)>();
var page = -1;
foreach (var section in document.Sections)
{
page++;
foreach (var block in section.Blocks)
{
- if (block is HParagraph { PageBreakBefore: true })
- page++;
+ if (block is HParagraph paragraph)
+ {
+ if (paragraph.PageBreakBefore)
+ page++;
+ if (paragraph.Inlines.OfType<HText>().Any(t => t.Text.Trim().Length > 0))
+ items.Add((null, page));
+ }
else if (block is HTable table && table.Rows.Count > 0)
- tables.Add(new Placed(table, page));
+ {
+ items.Add((new Placed(table, page), page));
+ }
}
}
- for (var i = 0; i < tables.Count; i++)
+ for (var i = 0; i < items.Count; i++)
{
- tables[i].FirstOnPage = i == 0 || tables[i - 1].Page != tables[i].Page;
- tables[i].LastOnPage = i == tables.Count - 1 || tables[i + 1].Page != tables[i].Page;
+ if (items[i].Table is not { } placed)
+ continue;
+ placed.FirstOnPage = i == 0 || items[i - 1].Page != placed.Page;
+ placed.LastOnPage = i == items.Count - 1 || items[i + 1].Page != placed.Page;
}
+ var tables = items.Where(i => i.Table is not null).Select(i => i.Table!).ToList();
var book = new Workbook();
Worksheet? sheet = null;
diff --git a/tests/Filee.Engines.Tests/PdfTablesTests.cs b/tests/Filee.Engines.Tests/PdfTablesTests.cs
index b61f9fd4..b84edf5f 100644
--- a/tests/Filee.Engines.Tests/PdfTablesTests.cs
+++ b/tests/Filee.Engines.Tests/PdfTablesTests.cs
@@ -88,6 +88,48 @@ public class PdfTablesTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal("Seoul", book.Sheets[1].Get(1, 1)!.Text);
}
+ [Fact]
+ public async Task Text_between_two_tables_keeps_them_on_their_own_sheets()
+ {
+ double[] xs = [50, 250, 545];
+ var pdf = new PdfBuilder().NewPage();
+ Table(pdf, 60, xs, [["Name", "Size"], ["a.pdf", "10"]]);
+ pdf.Paragraph("These are this week's files. The next table lists the files kept from last week.", 11, pdf.Font(), 50, 200, 495, 14);
+ pdf.NewPage();
+ Table(pdf, 60, xs, [["Kept", "Days"], ["b.pdf", "7"]]);
+ var book = await ConvertAsync(pdf.Save(Path.Combine(fx.NewFolder(), "two.pdf")));
+
+ Assert.Equal(2, book.Sheets.Count);
+ Assert.Equal("Kept", book.Sheets[1].Get(0, 0)!.Text);
+ }
+
+ [Fact]
+ public void Only_a_pdf_reaches_a_spreadsheet_through_its_tables()
+ {
+ var planner = fx.Catalog.CreatePlanner();
+ // A picture made into a PDF has no ruled tables: no image → spreadsheet route through PDF.
+ Assert.Null(planner.Plan("jpg", "xlsx"));
+ Assert.Null(planner.Plan("png", "csv"));
+ foreach (var target in new[] { "xlsx", "ods", "csv" })
+ Assert.Equal("pdf-tables", Assert.Single(planner.Plan("pdf", target)!.Steps).Converter.Id);
+ }
+
+ [Fact]
+ public async Task Csv_and_ods_are_written_from_the_same_tables()
+ {
+ var pdf = new PdfBuilder().NewPage();
+ Table(pdf, 60, [50, 250, 545], [["Name", "Size"], ["a.pdf", "1,234"]]);
+ var path = pdf.Save(Path.Combine(fx.NewFolder(), "one.pdf"));
+
+ var csv = await fx.ConvertAsync([path], new Preset { TargetFormat = "csv" });
+ var ods = await fx.ConvertAsync([path], new Preset { TargetFormat = "ods" });
+
+ Assert.Equal(JobState.Completed, csv.State);
+ Assert.Contains("a.pdf", await File.ReadAllTextAsync(csv.Outputs.Single(), TestContext.Current.CancellationToken));
+ Assert.Equal(JobState.Completed, ods.State);
+ Assert.EndsWith(".ods", ods.Outputs.Single());
+ }
+
[Fact]
public async Task A_pdf_without_tables_fails_with_a_message()
{