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.

Rebuild ruled tables in PDF → DOCX / HWPX

KnifeLemon7a3adf11ae2c
diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs
index 9c792470..f908fdce 100644
--- a/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs
+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxConverter.cs
@@ -5,7 +5,8 @@
// * TXT needs no reader: one paragraph per line. Markdown is parsed with Markdig (MarkdownReader).
// * XLSX / XLSM / XLTX and CSV are read directly (Office/Sheets); every sheet becomes a table (SheetDocument).
// * PPTX / PPTM / POTX / PPSX are read directly (Pptx/PptxReader): one page per slide.
-// * PDF is read with PdfPig (Pdf/PdfDocumentReader): text in reading order, headings, pictures, scans as images.
+// * PDF is read with PdfPig (Pdf/PdfDocumentReader): text in reading order, headings, ruled tables, pictures, scans
+// as images.
// * HTML (HtmlReader, AngleSharp), EPUB, MOBI / AZW3, FB2, HTMLZ and TXTZ (Ebooks) and HWPX (HwpxReader) are read
// directly.
// * ODT, RTF, reStructuredText and LaTeX are parsed by Pandoc (optional engine) into its JSON AST.
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs
new file mode 100644
index 00000000..cc33d4db
--- /dev/null
+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs
@@ -0,0 +1,487 @@
+// Ruled tables for PdfDocumentReader: tables drawn with lines become real tables (PDF → DOCX, HWPX, ...).
+//
+// The page's vector paths give the rules: stroked straight lines and rectangle edges, and thin filled rectangles
+// (thick rules are often drawn that way). Rules on one line are joined, rules that touch each other form a grid,
+// and the grid's lines are the column and row edges. A unit of the grid without a rule between it and its neighbour
+// is merged with it (a spanned cell). Words go into the cell under their centre; each cell's text is read like the
+// page (lines, paragraphs, alignment within the cell), and cell borders, shading and row heights come from the PDF.
+//
+// Conservative on purpose: a grid needs 2 rows and 2 columns, merged cells must be rectangles, text must sit in the
+// cells (a rule through a word means a chart or a strike-through, not a table) and at least two cells hold text.
+// Anything else stays text in reading order as before. Tables without rules (aligned by white space only) are not
+// detected; a table running over several pages becomes one table per page.
+
+using Filee.Engines.Hwp.Hwpx;
+using UglyToad.PdfPig.Core;
+using UglyToad.PdfPig.Graphics;
+
+namespace Filee.Engines.Pdf;
+
+internal sealed partial class PdfDocumentReader
+{
+ /// <summary>Thickest line (points) still taken for a rule; thicker filled rectangles are shading.</summary>
+ private const double MaxRuleThickness = 4;
+
+ /// <summary>Shortest rule (points): shorter strokes are glyph parts, ticks and bullets.</summary>
+ private const double MinRuleLength = 4;
+
+ /// <summary>Rules this close (points) across their direction lie on one line.</summary>
+ private const double SameLine = 1.5;
+
+ /// <summary>Gap (points) along a line that still joins two rules, and the reach of a rule to one it touches.</summary>
+ private const double RuleGap = 2.5;
+
+ /// <summary>Grid lines this close (points) are one edge (double rules, a frame drawn twice).</summary>
+ private const double EdgeTolerance = 3;
+
+ /// <summary>Pages with more line segments than this are drawings (maps, charts), not forms.</summary>
+ private const int MaxSegments = 5000;
+
+ /// <summary>A straight rule: horizontal (Position is y, Start..End is x) or vertical (Position is x, Start..End is y).</summary>
+ private sealed record Rule(bool Horizontal, double Position, double Start, double End, double Thickness, string Color, bool Dashed)
+ {
+ public double Length => End - Start;
+ }
+
+ /// <summary>A cell of a ruled table: its place in the grid, its box (points) and the words in it.</summary>
+ private sealed class RuledCell(int row, int column, int rowSpan, int columnSpan, PdfRectangle bounds)
+ {
+ public int Row { get; } = row;
+ public int Column { get; } = column;
+ public int RowSpan { get; } = rowSpan;
+ public int ColumnSpan { get; } = columnSpan;
+ public PdfRectangle Bounds { get; } = bounds;
+ public List<TextWord> Words { get; } = [];
+ public HBorders Borders { get; set; } = HBorders.Empty;
+ public string? Fill { get; set; }
+ }
+
+ /// <summary>A table found from the rules of a page.</summary>
+ private sealed class RuledTable(PdfRectangle bounds, double[] columns, double[] rows, List<RuledCell> cells)
+ {
+ public PdfRectangle Bounds { get; } = bounds;
+
+ /// <summary>Column edges left to right (one more than columns).</summary>
+ public double[] Columns { get; } = columns;
+
+ /// <summary>Row edges top to bottom (one more than rows).</summary>
+ public double[] Rows { get; } = rows;
+
+ public List<RuledCell> Cells { get; } = cells;
+
+ /// <summary>Distance from a cell's left edge to its text in left-aligned cells (the PDF's cell padding).</summary>
+ public double Padding { get; set; }
+
+ public string Text => string.Join(' ', Cells.SelectMany(c => c.Words).Select(w => w.Text));
+ }
+
+ // ───────────────────────── Rules ─────────────────────────
+
+ /// <summary>
+ /// The rules of a page (stroked lines and edges, thin filled rectangles) and the larger filled rectangles that
+ /// may be cell shading, shifted like the words. Invisible (white, clipping) paths are left out.
+ /// </summary>
+ private static (List<Rule> Rules, List<(PdfRectangle Bounds, string Color)> Shades) PageRules(IEnumerable<PdfPath> paths, PdfRectangle crop)
+ {
+ var rules = new List<Rule>();
+ var shades = new List<(PdfRectangle, string)>();
+ var segments = 0;
+ foreach (var path in paths)
+ {
+ if (path.IsClipping || (!path.IsFilled && !path.IsStroked))
+ continue;
+ var stroke = path.IsStroked ? Color(path.StrokeColor) ?? "#000000" : null;
+ var fill = path.IsFilled ? Color(path.FillColor) ?? "#000000" : null;
+ var dashed = path.LineDashPattern is { Array.Count: > 0 };
+ foreach (var subpath in path)
+ {
+ var lines = new List<(PdfPoint From, PdfPoint To)>();
+ var curved = false;
+ PdfPoint? start = null, current = null;
+ foreach (var command in subpath.Commands)
+ {
+ switch (command)
+ {
+ case PdfSubpath.Move move:
+ start = current = move.Location;
+ break;
+ case PdfSubpath.Line line:
+ lines.Add((line.From, line.To));
+ start ??= line.From;
+ current = line.To;
+ break;
+ case PdfSubpath.BezierCurve curve:
+ curved = true;
+ current = curve.EndPoint;
+ break;
+ case PdfSubpath.Close when start is { } first && current is { } last:
+ lines.Add((last, first));
+ current = first;
+ break;
+ }
+ }
+ segments += lines.Count;
+ if (segments > MaxSegments)
+ return ([], []);
+ var shifted = lines.Select(l => (From: new PdfPoint(l.From.X - crop.Left, l.From.Y - crop.Bottom), To: new PdfPoint(l.To.X - crop.Left, l.To.Y - crop.Bottom))).ToList();
+
+ if (stroke is not null && !IsWhite(stroke))
+ {
+ var thickness = Math.Max(0.25, path.LineWidth);
+ foreach (var (from, to) in shifted)
+ {
+ if (Math.Abs(from.Y - to.Y) <= 0.5 && Math.Abs(from.X - to.X) >= MinRuleLength)
+ rules.Add(new Rule(true, (from.Y + to.Y) / 2, Math.Min(from.X, to.X), Math.Max(from.X, to.X), thickness, stroke, dashed));
+ else if (Math.Abs(from.X - to.X) <= 0.5 && Math.Abs(from.Y - to.Y) >= MinRuleLength)
+ rules.Add(new Rule(false, (from.X + to.X) / 2, Math.Min(from.Y, to.Y), Math.Max(from.Y, to.Y), thickness, stroke, dashed));
+ }
+ }
+
+ // A filled shape of straight, upright edges: a thin one is a rule, a larger one may shade a cell.
+ if (fill is null || curved || shifted.Count < 3 || IsWhite(fill)
+ || !shifted.All(l => Math.Abs(l.From.X - l.To.X) <= 0.5 || Math.Abs(l.From.Y - l.To.Y) <= 0.5))
+ continue;
+ var left = shifted.Min(l => Math.Min(l.From.X, l.To.X));
+ var right = shifted.Max(l => Math.Max(l.From.X, l.To.X));
+ var bottom = shifted.Min(l => Math.Min(l.From.Y, l.To.Y));
+ var top = shifted.Max(l => Math.Max(l.From.Y, l.To.Y));
+ var (width, height) = (right - left, top - bottom);
+ if (height <= MaxRuleThickness && width >= MinRuleLength && width > 2 * height)
+ rules.Add(new Rule(true, (top + bottom) / 2, left, right, Math.Max(0.25, height), fill, false));
+ else if (width <= MaxRuleThickness && height >= MinRuleLength && height > 2 * width)
+ rules.Add(new Rule(false, (left + right) / 2, bottom, top, Math.Max(0.25, width), fill, false));
+ else if (width > MaxRuleThickness && height > MaxRuleThickness)
+ shades.Add((new PdfRectangle(left, bottom, right, top), fill));
+ }
+ }
+ return (JoinRules(rules), shades);
+ }
+
+ private static bool IsWhite(string color) =>
+ Convert.ToInt32(color[1..3], 16) >= 242 && Convert.ToInt32(color[3..5], 16) >= 242 && Convert.ToInt32(color[5..7], 16) >= 242;
+
+ /// <summary>Rules on one line that overlap or nearly touch become one rule (cells drawn one by one share edges).</summary>
+ private static List<Rule> JoinRules(List<Rule> rules)
+ {
+ var result = new List<Rule>();
+ foreach (var direction in rules.GroupBy(r => r.Horizontal))
+ {
+ var sorted = direction.OrderBy(r => r.Position).ToList();
+ for (var i = 0; i < sorted.Count;)
+ {
+ var j = i + 1;
+ while (j < sorted.Count && sorted[j].Position - sorted[j - 1].Position <= SameLine)
+ j++;
+ Rule? current = null;
+ foreach (var rule in sorted[i..j].OrderBy(r => r.Start))
+ {
+ if (current is not null && rule.Start <= current.End + RuleGap)
+ {
+ current = current with
+ {
+ End = Math.Max(current.End, rule.End),
+ Thickness = Math.Max(current.Thickness, rule.Thickness),
+ // The longer piece decides where the line is and how it looks.
+ Position = rule.Length > current.Length ? rule.Position : current.Position,
+ Color = rule.Length > current.Length ? rule.Color : current.Color,
+ Dashed = current.Dashed && rule.Dashed,
+ };
+ }
+ else
+ {
+ if (current is not null)
+ result.Add(current);
+ current = rule;
+ }
+ }
+ result.Add(current!);
+ i = j;
+ }
+ }
+ return result;
+ }
+
+ // ───────────────────────── Grids ─────────────────────────
+
+ /// <summary>
+ /// Ruled tables of a page; the words in them are taken out of <paramref name="words"/>. Grids are tried largest
+ /// first, and a grid inside a table already found (a table in a cell) stays part of that cell.
+ /// </summary>
+ private static List<RuledTable> RuledTables(IEnumerable<PdfPath> paths, PdfRectangle crop, List<TextWord> words)
+ {
+ var (rules, shades) = PageRules(paths, crop);
+ var tables = new List<RuledTable>();
+ foreach (var group in Grids(rules).OrderByDescending(g => Area(Extent(g))))
+ {
+ var extent = Extent(group);
+ if (tables.Any(t => Overlaps(t.Bounds, extent)))
+ continue;
+ if (Table(group, words, shades) is not { } table)
+ continue;
+ tables.Add(table);
+ var taken = table.Cells.SelectMany(c => c.Words).ToHashSet();
+ words.RemoveAll(taken.Contains);
+ }
+ return tables;
+ }
+
+ /// <summary>Groups of rules that touch each other (a horizontal and a vertical rule crossing or meeting).</summary>
+ private static IEnumerable<List<Rule>> Grids(List<Rule> rules)
+ {
+ var parent = Enumerable.Range(0, rules.Count).ToArray();
+ int Find(int i) => parent[i] == i ? i : parent[i] = Find(parent[i]);
+ for (var a = 0; a < rules.Count; a++)
+ {
+ if (!rules[a].Horizontal)
+ continue;
+ for (var b = 0; b < rules.Count; b++)
+ {
+ if (!rules[b].Horizontal && Touch(rules[a], rules[b]))
+ parent[Find(a)] = Find(b);
+ }
+ }
+ return Enumerable.Range(0, rules.Count).GroupBy(Find).Select(g => g.Select(i => rules[i]).ToList())
+ .Where(g => g.Any(r => r.Horizontal) && g.Any(r => !r.Horizontal) && g.Count >= 4);
+ }
+
+ private static bool Touch(Rule horizontal, Rule vertical)
+ {
+ var reach = RuleGap + Math.Max(horizontal.Thickness, vertical.Thickness) / 2;
+ return vertical.Position >= horizontal.Start - reach && vertical.Position <= horizontal.End + reach
+ && horizontal.Position >= vertical.Start - reach && horizontal.Position <= vertical.End + reach;
+ }
+
+ /// <summary>
+ /// The box of a grid: its outer vertical and horizontal rules, or further out where most rules of the other
+ /// direction reach (a table without side or bottom rules).
+ /// </summary>
+ private static PdfRectangle Extent(List<Rule> group)
+ {
+ var horizontal = group.Where(r => r.Horizontal).ToList();
+ var vertical = group.Where(r => !r.Horizontal).ToList();
+ var left = vertical.Min(r => r.Position);
+ var right = vertical.Max(r => r.Position);
+ var bottom = horizontal.Min(r => r.Position);
+ var top = horizontal.Max(r => r.Position);
+ left = Math.Min(left, Median(horizontal.Select(r => r.Start)));
+ right = Math.Max(right, Median(horizontal.Select(r => r.End)));
+ bottom = Math.Min(bottom, Median(vertical.Select(r => r.Start)));
+ top = Math.Max(top, Median(vertical.Select(r => r.End)));
+ return new PdfRectangle(left, bottom, right, top);
+ }
+
+ private static double Area(PdfRectangle box) => box.Width * box.Height;
+
+ private static bool Overlaps(PdfRectangle a, PdfRectangle b) =>
+ Math.Min(a.Right, b.Right) - Math.Max(a.Left, b.Left) > EdgeTolerance && Math.Min(a.Top, b.Top) - Math.Max(a.Bottom, b.Bottom) > EdgeTolerance;
+
+ /// <summary>Positions closer than <see cref="EdgeTolerance"/> merged into one (their mean), sorted.</summary>
+ private static List<double> Edges(IEnumerable<double> positions)
+ {
+ var result = new List<List<double>>();
+ foreach (var position in positions.Order())
+ {
+ if (result.Count > 0 && position - result[^1][^1] <= EdgeTolerance)
+ result[^1].Add(position);
+ else
+ result.Add([position]);
+ }
+ return result.Select(g => g.Average()).ToList();
+ }
+
+ /// <summary>
+ /// The rule covering most of the segment from <paramref name="from"/> to <paramref name="to"/> on the line at
+ /// <paramref name="position"/>, or null when rules cover less than half of it.
+ /// </summary>
+ private static Rule? Covering(List<Rule> rules, double position, double from, double to)
+ {
+ var length = to - from;
+ if (length <= 0)
+ return null;
+ var covered = 0.0;
+ Rule? best = null;
+ var bestOverlap = 0.0;
+ foreach (var rule in rules)
+ {
+ if (Math.Abs(rule.Position - position) > EdgeTolerance + rule.Thickness / 2)
+ continue;
+ var overlap = Math.Min(to, rule.End) - Math.Max(from, rule.Start);
+ if (overlap <= 0)
+ continue;
+ covered += overlap;
+ if (overlap > bestOverlap)
+ (best, bestOverlap) = (rule, overlap);
+ }
+ return covered >= 0.5 * length ? best : null;
+ }
+
+ /// <summary>The table a grid of rules draws, with the words in its cells; null when it does not look like one.</summary>
+ private static RuledTable? Table(List<Rule> group, List<TextWord> words, List<(PdfRectangle Bounds, string Color)> shades)
+ {
+ var extent = Extent(group);
+ var horizontal = group.Where(r => r.Horizontal).ToList();
+ var vertical = group.Where(r => !r.Horizontal).ToList();
+ var xs = Edges(vertical.Select(r => r.Position).Where(x => x > extent.Left && x < extent.Right).Append(extent.Left).Append(extent.Right));
+ var ys = Edges(horizontal.Select(r => r.Position).Where(y => y > extent.Bottom && y < extent.Top).Append(extent.Bottom).Append(extent.Top));
+ ys.Reverse(); // top to bottom
+
+ // Grid lines that separate no two units anywhere (a rule too short to bound a cell) are dropped.
+ bool VerticalEdge(int c, int r) => Covering(vertical, xs[c], ys[r + 1], ys[r]) is not null;
+ bool HorizontalEdge(int r, int c) => Covering(horizontal, ys[r], xs[c], xs[c + 1]) is not null;
+ while (true)
+ {
+ var column = Enumerable.Range(1, Math.Max(0, xs.Count - 2)).FirstOrDefault(c => !Enumerable.Range(0, ys.Count - 1).Any(r => VerticalEdge(c, r)));
+ if (column > 0)
+ {
+ xs.RemoveAt(column);
+ continue;
+ }
+ var row = Enumerable.Range(1, Math.Max(0, ys.Count - 2)).FirstOrDefault(r => !Enumerable.Range(0, xs.Count - 1).Any(c => HorizontalEdge(r, c)));
+ if (row == 0)
+ break;
+ ys.RemoveAt(row);
+ }
+ var (rows, columns) = (ys.Count - 1, xs.Count - 1);
+ if (rows < 2 || columns < 2 || rows * columns > 2500)
+ return null;
+
+ // Units of the grid joined where no rule separates them; each group must be a rectangle (a cell).
+ var owner = new int[rows, columns];
+ var parent = Enumerable.Range(0, rows * columns).ToArray();
+ int Find(int i) => parent[i] == i ? i : parent[i] = Find(parent[i]);
+ for (var r = 0; r < rows; r++)
+ {
+ for (var c = 0; c < columns; c++)
+ {
+ if (c + 1 < columns && !VerticalEdge(c + 1, r))
+ parent[Find(r * columns + c)] = Find(r * columns + c + 1);
+ if (r + 1 < rows && !HorizontalEdge(r + 1, c))
+ parent[Find(r * columns + c)] = Find((r + 1) * columns + c);
+ }
+ }
+ var cells = new List<RuledCell>();
+ foreach (var unit in Enumerable.Range(0, rows * columns).GroupBy(Find))
+ {
+ var (top, bottom) = (unit.Min(u => u / columns), unit.Max(u => u / columns));
+ var (left, right) = (unit.Min(u => u % columns), unit.Max(u => u % columns));
+ if ((bottom - top + 1) * (right - left + 1) != unit.Count())
+ return null; // an L-shaped region: not a table cell
+ var cell = new RuledCell(top, left, bottom - top + 1, right - left + 1, new PdfRectangle(xs[left], ys[bottom + 1], xs[right + 1], ys[top]));
+ foreach (var u in unit)
+ owner[u / columns, u % columns] = cells.Count;
+ cells.Add(cell);
+ }
+ if (cells.Count < 4 && (cells.Select(c => c.Row).Distinct().Count() < 2 || cells.Select(c => c.Column).Distinct().Count() < 2))
+ return null;
+
+ // Words by their centre; a rule through the middle of a word means this is no table around text.
+ var bounds = new PdfRectangle(xs[0], ys[^1], xs[^1], ys[0]);
+ foreach (var word in words)
+ {
+ var (x, y) = ((word.Left + word.Right) / 2, (word.Top + word.Bottom) / 2);
+ if (x <= bounds.Left || x >= bounds.Right || y <= bounds.Bottom || y >= bounds.Top)
+ continue;
+ var band = (word.Top - word.Bottom) * 0.3;
+ if (vertical.Any(v => v.Position > word.Left + 1 && v.Position < word.Right - 1 && v.Start < y && v.End > y)
+ || horizontal.Any(h => h.Position > word.Bottom + band && h.Position < word.Top - band && h.Start < word.Right - 1 && h.End > word.Left + 1))
+ return null;
+ var column = Math.Clamp(xs.FindLastIndex(e => e <= x), 0, columns - 1);
+ var row = Math.Clamp(ys.FindLastIndex(e => e >= y), 0, rows - 1);
+ cells[owner[row, column]].Words.Add(word);
+ }
+ if (cells.Count(c => c.Words.Count > 0) < 2)
+ return null;
+
+ foreach (var cell in cells)
+ {
+ var box = cell.Bounds;
+ cell.Borders = new HBorders(
+ Border(Covering(vertical, box.Left, box.Bottom, box.Top)),
+ Border(Covering(vertical, box.Right, box.Bottom, box.Top)),
+ Border(Covering(horizontal, box.Top, box.Left, box.Right)),
+ Border(Covering(horizontal, box.Bottom, box.Left, box.Right)));
+ // Shading: a filled rectangle inside the table that covers most of the cell.
+ cell.Fill = shades.LastOrDefault(s => s.Bounds.Left >= bounds.Left - EdgeTolerance && s.Bounds.Right <= bounds.Right + EdgeTolerance
+ && s.Bounds.Bottom >= bounds.Bottom - EdgeTolerance && s.Bounds.Top <= bounds.Top + EdgeTolerance
+ && Intersection(s.Bounds, box) >= 0.8 * Area(box)).Color;
+ }
+
+ var table = new RuledTable(bounds, [.. xs], [.. ys], cells);
+ var paddings = cells.Where(c => c.Words.Count > 0).Select(c => c.Words.Min(w => w.Left) - c.Bounds.Left).ToList();
+ table.Padding = Math.Clamp(paddings.Min(), 1, 8);
+ return table;
+ }
+
+ private static HBorder Border(Rule? rule) => rule is null
+ ? HBorder.None
+ : new HBorder(rule.Dashed ? HBorderStyle.Dash : HBorderStyle.Solid, Math.Max(0.1, rule.Thickness * 25.4 / 72), rule.Color);
+
+ private static double Intersection(PdfRectangle a, PdfRectangle b) =>
+ Math.Max(0, Math.Min(a.Right, b.Right) - Math.Max(a.Left, b.Left)) * Math.Max(0, Math.Min(a.Top, b.Top) - Math.Max(a.Bottom, b.Bottom));
+
+ // ───────────────────────── Building ─────────────────────────
+
+ /// <summary>
+ /// The table of a ruled table item: column widths and row heights from the PDF, each cell's text read like a page
+ /// (paragraphs, alignment within the cell, fonts), borders and shading per cell.
+ /// </summary>
+ private static HTable TableBlock(Item item, double lineRatio)
+ {
+ var grid = item.Table!;
+ var padding = grid.Padding;
+ var table = new HTable
+ {
+ ColumnCount = grid.Columns.Length - 1,
+ ColumnWidths = [.. grid.Columns.Zip(grid.Columns.Skip(1), (a, b) => Hwp(b - a))],
+ Borders = HBorders.Empty,
+ CellMargin = new HInsets(Hwp(padding), Hwp(padding), Hwp(1), Hwp(1)),
+ };
+ var containerWidth = item.ContainerRight - item.ContainerLeft;
+ var center = (grid.Bounds.Left + grid.Bounds.Right) / 2;
+ if (Math.Abs(center - (item.ContainerLeft + item.ContainerRight) / 2) < Math.Max(6, containerWidth * 0.05) && grid.Bounds.Width < containerWidth * 0.9)
+ table.Align = HAlign.Center;
+ else if (grid.Bounds.Left - item.ContainerLeft > 2)
+ table.Indent = Hwp(grid.Bounds.Left - item.ContainerLeft);
+
+ for (var r = 0; r < grid.Rows.Length - 1; r++)
+ table.Rows.Add(new HRow { Height = Hwp(grid.Rows[r] - grid.Rows[r + 1]) });
+ var noHeadings = new Dictionary<double, int>();
+ foreach (var cell in grid.Cells.OrderBy(c => c.Row).ThenBy(c => c.Column))
+ {
+ var hcell = new HCell
+ {
+ Column = cell.Column,
+ RowSpan = cell.RowSpan,
+ ColSpan = cell.ColumnSpan,
+ Borders = cell.Borders,
+ Fill = cell.Fill,
+ };
+ if (cell.Words.Count > 0)
+ {
+ var items = TextItems(cell.Words).OrderByDescending(i => i.Top).ThenBy(i => i.Left).ToList();
+ Item? previous = null;
+ foreach (var paragraph in items)
+ {
+ (paragraph.ContainerLeft, paragraph.ContainerRight) = (cell.Bounds.Left + padding, cell.Bounds.Right - padding);
+ hcell.Blocks.Add(TextParagraph(paragraph, previous, noHeadings, lineRatio));
+ previous = paragraph;
+ }
+ hcell.VerticalAlign = VerticalAlign(cell, items);
+ }
+ table.Rows[cell.Row].Cells.Add(hcell);
+ }
+ return table;
+ }
+
+ /// <summary>Text about as far from the top as from the bottom of a taller cell is centred; text at the bottom stays there.</summary>
+ private static HVerticalAlign VerticalAlign(RuledCell cell, List<Item> items)
+ {
+ var above = cell.Bounds.Top - items.Max(i => i.Top);
+ var below = items.Min(i => i.Bottom) - cell.Bounds.Bottom;
+ if (above < 0 || below < 0)
+ return HVerticalAlign.Top;
+ if (Math.Abs(above - below) <= Math.Max(2, 0.25 * (above + below)) && above + below > 4)
+ return HVerticalAlign.Center;
+ return below < 0.4 * above && above > items[0].Size * 1.5 ? HVerticalAlign.Bottom : HVerticalAlign.Top;
+ }
+}
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
index f25d868c..97b43b83 100644
--- a/src/Filee.Engines/Pdf/PdfDocumentReader.cs
+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
@@ -10,8 +10,9 @@
// positions, and a line that ends early although the next line's first word would have fitted (an address) keeps its
// line break.
//
-// Not converted: tables (their cells come out as paragraphs in reading order), vector drawings on text pages,
-// annotations other than links, form fields.
+// Tables drawn with lines become tables (PdfDocumentReader.Tables). Not converted: tables without rules (their cells
+// come out as paragraphs in reading order), other vector drawings on text pages, annotations other than links, form
+// fields.
using System.Text;
using System.Text.RegularExpressions;
@@ -113,25 +114,29 @@ internal sealed partial class PdfDocumentReader
public string Text => string.Join(' ', Words.Select(w => w.Text));
}
- /// <summary>A paragraph or a picture on a page, the unit of reading order.</summary>
+ /// <summary>A paragraph, a picture or a ruled table on a page, the unit of reading order.</summary>
private sealed class Item
{
public List<TextLine> Lines { get; } = [];
public string? PicturePath { get; init; }
public PdfRectangle? PictureBounds { get; init; }
+ public RuledTable? Table { get; init; }
- public double Left => PictureBounds?.Left ?? Lines.Min(l => l.Left);
- public double Right => PictureBounds?.Right ?? Lines.Max(l => l.Right);
- public double Top => PictureBounds?.Top ?? Lines[0].Top;
- public double Bottom => PictureBounds?.Bottom ?? Lines[^1].Bottom;
+ private PdfRectangle? Box => PictureBounds ?? Table?.Bounds;
+
+ public double Left => Box?.Left ?? Lines.Min(l => l.Left);
+ public double Right => Box?.Right ?? Lines.Max(l => l.Right);
+ public double Top => Box?.Top ?? Lines[0].Top;
+ public double Bottom => Box?.Bottom ?? Lines[^1].Bottom;
public double Width => Right - Left;
public bool IsPicture => PicturePath is not null;
+ public bool IsTable => Table is not null;
/// <summary>Size of most of the text (by characters).</summary>
public double Size => Lines.SelectMany(l => l.Words).GroupBy(w => Math.Round(w.Size * 2) / 2)
.OrderByDescending(g => g.Sum(w => w.Text.Length)).Select(g => g.Key).FirstOrDefault(10);
- public string Text => string.Join(' ', Lines.Select(l => l.Text));
+ public string Text => Table?.Text ?? string.Join(' ', Lines.Select(l => l.Text));
/// <summary>Left and right edges of the column (or the text area) the item sits in, set by the reading order.</summary>
public double ContainerLeft { get; set; }
@@ -258,14 +263,23 @@ internal sealed partial class PdfDocumentReader
return content;
}
+ // Plain text (TXT, page texts for slides) keeps table cells as paragraphs in reading order.
+ var tables = _textOnly ? [] : RuledTables(page.Paths, crop, words);
+ content.Items = words.Count > 0 ? TextItems(words) : [];
+ content.Items.AddRange(tables.Select(t => new Item { Table = t }));
+ if (!_textOnly)
+ content.Items.AddRange(Pictures(page, content, crop));
+ return content;
+ }
+
+ /// <summary>Words → lines → paragraphs, with tabbed rows joined.</summary>
+ private static List<Item> TextItems(List<TextWord> words)
+ {
var lines = Lines(words);
var items = Paragraphs(lines);
if (JoinTabbedRows(lines, items) is { } joined)
items = Paragraphs(joined);
- content.Items = items;
- if (!_textOnly)
- content.Items.AddRange(Pictures(page, content, crop));
- return content;
+ return items;
}
private static PdfRectangle Shift(PdfRectangle box, PdfRectangle crop) =>
@@ -485,7 +499,7 @@ internal sealed partial class PdfDocumentReader
private HDocument Build(List<PageContent> pages, string? title)
{
var document = new HDocument { Title = title };
- var items = pages.SelectMany(p => p.Items.Where(i => !i.IsPicture)).ToList();
+ var items = pages.SelectMany(p => p.Items.Where(i => !i.IsPicture && !i.IsTable)).ToList();
var bodySize = BodySize(items);
var headingLevels = HeadingLevels(items, bodySize);
var (left, right, top, bottom) = Margins(pages);
@@ -529,13 +543,18 @@ internal sealed partial class PdfDocumentReader
Item? previous = null;
foreach (var item in page.Items)
{
- blocks.Add(item.IsPicture ? PictureParagraph(item, previous) : TextParagraph(item, previous, headingLevels, lineRatio));
+ blocks.Add(item.IsPicture ? PictureParagraph(item, previous)
+ : item.IsTable ? TableBlock(item, lineRatio)
+ : TextParagraph(item, previous, headingLevels, lineRatio));
previous = item;
}
if (blocks.Count == first)
blocks.Add(new HParagraph());
}
+ // A table cannot start a page in every format: a tiny empty paragraph before it carries the page break.
+ if (!newSection && blocks[first] is HTable)
+ blocks.Insert(first, new HParagraph { MarkFormat = new HCharFormat(Size: 100) });
if (!newSection && blocks[first] is HParagraph startOfPage)
startOfPage.PageBreakBefore = true;
}
@@ -674,9 +693,9 @@ internal sealed partial class PdfDocumentReader
if (previous is null || previous.Bottom <= item.Top - 1 || previous.Right <= item.Left || previous.Left >= item.Right)
return 0;
// Edges of the line boxes: a text line reaches about 0.25 em below its baseline and 0.95 em above it.
- var above = previous.IsPicture ? previous.Bottom : previous.Lines[^1].Baseline - 0.25 * previous.Size;
- var top = item.IsPicture ? item.Top : item.Lines[0].Baseline + 0.95 * item.Size;
- var normal = item.IsPicture || previous.IsPicture ? 0 : (lineRatio - 1.2) * item.Size;
+ var above = previous.Lines.Count == 0 ? previous.Bottom : previous.Lines[^1].Baseline - 0.25 * previous.Size;
+ var top = item.Lines.Count == 0 ? item.Top : item.Lines[0].Baseline + 0.95 * item.Size;
+ var normal = item.Lines.Count == 0 || previous.Lines.Count == 0 ? 0 : (lineRatio - 1.2) * item.Size;
var extra = above - top - normal;
return extra > 1 ? Hwp(Math.Min(extra, 72)) : 0;
}
diff --git a/tests/Filee.Engines.Tests/PdfBuilder.cs b/tests/Filee.Engines.Tests/PdfBuilder.cs
index 5089014b..40672876 100644
--- a/tests/Filee.Engines.Tests/PdfBuilder.cs
+++ b/tests/Filee.Engines.Tests/PdfBuilder.cs
@@ -1,6 +1,7 @@
// Builds small PDF files for the PDF reader tests with PdfPig's writer: text in the standard 14 fonts (every PDF
// reader has them, so no font file is needed and the tests run on machines without Windows fonts), wrapped
-// paragraphs, colours, pictures, links and several pages. PDFsharp adds password protection to a finished file.
+// paragraphs, colours, pictures, links, lines and filled bars (ruled tables) and several pages. PDFsharp adds password
+// protection to a finished file.
using ImageMagick;
using PdfSharp.Pdf.IO;
@@ -101,6 +102,24 @@ internal sealed class PdfBuilder
return this;
}
+ /// <summary>A stroked straight line from (x1, y1) to (x2, y2).</summary>
+ public PdfBuilder Rule(double x1, double y1, double x2, double y2, double width = 0.5)
+ {
+ Page.DrawLine(new PdfPoint(x1, _height - y1), new PdfPoint(x2, _height - y2), width);
+ return this;
+ }
+
+ /// <summary>A filled rectangle (a thick rule drawn as a bar, or shading) with its top-left corner at (x, y).</summary>
+ public PdfBuilder Bar(double x, double y, double width, double height, (byte R, byte G, byte B)? color = null)
+ {
+ var (r, g, b) = color ?? ((byte)0, (byte)0, (byte)0);
+ Page.SetTextAndFillColor(r, g, b);
+ Page.SetStrokeColor(r, g, b);
+ Page.DrawRectangle(new PdfPoint(x, _height - y - height), width, height, 0.1, fill: true);
+ Page.ResetColor();
+ return this;
+ }
+
/// <summary>A web link over the rectangle.</summary>
public PdfBuilder Link(string url, double x, double y, double width, double height)
{
diff --git a/tests/Filee.Engines.Tests/PdfTableTests.cs b/tests/Filee.Engines.Tests/PdfTableTests.cs
new file mode 100644
index 00000000..8930152a
--- /dev/null
+++ b/tests/Filee.Engines.Tests/PdfTableTests.cs
@@ -0,0 +1,195 @@
+// Ruled tables in PDF → DOCX / HWPX: grids drawn with lines (or thin filled bars) become real tables with the PDF's
+// column widths, merged cells, borders and shading, in reading order between the paragraphs around them. Boxes,
+// underlines and empty grids stay what they were. The PDFs are drawn here with the standard 14 fonts.
+
+using Filee.Core.Conversion;
+using Filee.Core.Presets;
+using Filee.Engines.Hwp.Hwpx;
+using static Filee.Engines.Tests.DocxAssert;
+
+namespace Filee.Engines.Tests;
+
+public class PdfTableTests(EngineFixture fx) : IClassFixture<EngineFixture>
+{
+ private async Task<string> ConvertAsync(string input, string target)
+ {
+ var job = await fx.ConvertAsync([input], new Preset { TargetFormat = target });
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ return job.Outputs.Single();
+ }
+
+ /// <summary>Text in a cell: 4 pt from its left edge, on a baseline 14 pt below the row's top.</summary>
+ private static void Cell(PdfBuilder pdf, string text, double left, double top, bool bold = false) =>
+ pdf.Line(text, 10, pdf.Font(bold), left + 4, top + 14);
+
+ /// <summary>
+ /// A paragraph, a 3 × 3 table with every rule drawn as a stroked line (a bold header row), and a paragraph after
+ /// it. Columns are 150, 150 and 195 pt wide, rows 20 pt high.
+ /// </summary>
+ private static string SimpleTable(string folder)
+ {
+ var pdf = new PdfBuilder().NewPage();
+ var body = pdf.Font();
+ pdf.Paragraph("The files converted this week are listed in the table below, with their format and size.", 11, body, 50, 80, 495, 14);
+ double[] xs = [50, 200, 350, 545];
+ double[] ys = [110, 130, 150, 170];
+ foreach (var y in ys)
+ pdf.Rule(xs[0], y, xs[^1], y);
+ foreach (var x in xs)
+ pdf.Rule(x, ys[0], x, ys[^1]);
+ string[][] cells = [["Name", "Format", "Size"], ["Report", "PDF", "120 KB"], ["Photo", "JPEG", "2.4 MB"]];
+ for (var r = 0; r < 3; r++)
+ {
+ for (var c = 0; c < 3; c++)
+ Cell(pdf, cells[r][c], xs[c], ys[r], bold: r == 0);
+ }
+ pdf.Paragraph("Everything was converted on this computer; nothing was uploaded anywhere.", 11, body, 50, 200, 495, 14);
+ return pdf.Save(Path.Combine(folder, "table.pdf"));
+ }
+
+ private static List<HBlock> Blocks(HDocument document) =>
+ [.. document.Sections.SelectMany(s => s.Blocks).Where(b => b is HTable || (b is HParagraph p && TextOf(p).Trim().Length > 0))];
+
+ private static string CellText(HCell cell) => string.Join("\n", Paragraphs(cell.Blocks).Select(TextOf).Select(t => t.Trim()).Where(t => t.Length > 0));
+
+ private static string[][] Texts(HTable table) => [.. table.Rows.Select(r => r.Cells.Select(CellText).ToArray())];
+
+ [Fact]
+ public async Task Ruled_table_becomes_a_word_table_between_its_paragraphs()
+ {
+ var docx = await ConvertAsync(SimpleTable(fx.NewFolder()), "docx");
+
+ ValidPackage(docx);
+ var blocks = Blocks(ReadBack(docx));
+ Assert.Equal(3, blocks.Count);
+ Assert.StartsWith("The files converted", TextOf(Assert.IsType<HParagraph>(blocks[0])).Trim());
+ Assert.StartsWith("Everything was converted", TextOf(Assert.IsType<HParagraph>(blocks[2])).Trim());
+
+ var table = Assert.IsType<HTable>(blocks[1]);
+ Assert.Equal([["Name", "Format", "Size"], ["Report", "PDF", "120 KB"], ["Photo", "JPEG", "2.4 MB"]], Texts(table));
+ // Column widths from the rules (points × 100), rows at least as high as in the PDF.
+ Assert.Equal([15000, 15000, 19500], table.ColumnWidths!.Select(w => (int)Math.Round(w / 100.0) * 100));
+ Assert.All(table.Rows, r => Assert.InRange(r.Height ?? 0, 1900, 2100));
+ // Every cell has its four borders; the header keeps its bold font.
+ Assert.All(table.Rows.SelectMany(r => r.Cells), c =>
+ {
+ var borders = c.Borders ?? table.Borders ?? HBorders.Grid;
+ Assert.All(new[] { borders.Left, borders.Right, borders.Top, borders.Bottom }, b => Assert.Equal(HBorderStyle.Solid, b.Style));
+ });
+ var header = Paragraphs(table.Rows[0].Cells[0].Blocks).Single().Inlines.OfType<HText>().First();
+ Assert.Equal((true, "Arial", 1000), (header.Format.Bold, header.Format.Font, header.Format.Size));
+
+ if (await ConvertibleByLibreOfficeAsync(fx, docx) is { } pages)
+ Assert.Equal(1, pages);
+ }
+
+ [Fact]
+ public async Task Ruled_table_reaches_hwpx_and_text_keeps_the_cells()
+ {
+ var path = SimpleTable(fx.NewFolder());
+
+ var hwpx = await ConvertAsync(path, "hwpx");
+ HwpxAssert.ValidPackage(hwpx);
+ var model = HwpxReader.Read(hwpx, Path.Combine(Path.GetDirectoryName(hwpx)!, "readback-media"));
+ var table = Assert.Single(model.Sections.SelectMany(s => s.Blocks).OfType<HTable>());
+ Assert.Equal([["Name", "Format", "Size"], ["Report", "PDF", "120 KB"], ["Photo", "JPEG", "2.4 MB"]], Texts(table));
+ HwpxAssert.RenderWithRhwp(fx, hwpx, "pdf-table");
+
+ // PDF → TXT has no tables: the cells stay text in reading order.
+ var text = await File.ReadAllTextAsync(await ConvertAsync(path, "txt"), TestContext.Current.CancellationToken);
+ Assert.True(text.IndexOf("Name", StringComparison.Ordinal) < text.IndexOf("2.4 MB", StringComparison.Ordinal), text);
+ }
+
+ [Fact]
+ public async Task Missing_inner_rules_become_merged_cells()
+ {
+ var dir = fx.NewFolder();
+ var pdf = new PdfBuilder().NewPage();
+ double[] xs = [50, 180, 310, 440];
+ double[] ys = [100, 120, 140, 160];
+ // Outer frame, the header's bottom rule, and the inner rules with the gaps that merge cells:
+ // "Region and city" spans the first two columns, "North" the last two rows.
+ pdf.Rule(50, 100, 440, 100).Rule(50, 160, 440, 160).Rule(50, 100, 50, 160).Rule(440, 100, 440, 160);
+ pdf.Rule(50, 120, 440, 120);
+ pdf.Rule(180, 140, 440, 140);
+ pdf.Rule(180, 120, 180, 160);
+ pdf.Rule(310, 100, 310, 160);
+ Cell(pdf, "Region and city", xs[0], ys[0], bold: true);
+ Cell(pdf, "Total", xs[2], ys[0], bold: true);
+ Cell(pdf, "North", xs[0], ys[1]);
+ Cell(pdf, "Seoul", xs[1], ys[1]);
+ Cell(pdf, "10", xs[2], ys[1]);
+ Cell(pdf, "Busan", xs[1], ys[2]);
+ Cell(pdf, "20", xs[2], ys[2]);
+ var docx = await ConvertAsync(pdf.Save(Path.Combine(dir, "merged.pdf")), "docx");
+
+ ValidPackage(docx);
+ var table = Assert.Single(ReadBack(docx).Sections.SelectMany(s => s.Blocks).OfType<HTable>());
+ Assert.Equal(3, table.ColumnCount);
+ Assert.Equal([["Region and city", "Total"], ["North", "Seoul", "10"], ["Busan", "20"]], Texts(table));
+ Assert.Equal([(2, 1), (1, 1)], table.Rows[0].Cells.Select(c => (c.ColSpan, c.RowSpan)));
+ Assert.Equal([(1, 2), (1, 1), (1, 1)], table.Rows[1].Cells.Select(c => (c.ColSpan, c.RowSpan)));
+ Assert.Equal([1, 2], table.Rows[2].Cells.Select(c => c.Column));
+ }
+
+ [Fact]
+ public async Task Rules_drawn_as_bars_keep_their_weight_and_shading()
+ {
+ var dir = fx.NewFolder();
+ var pdf = new PdfBuilder().NewPage();
+ pdf.Line("Results", 14, pdf.Font(bold: true), 60, 80);
+ // A shaded header row, a 2 pt frame and 0.5 pt inner rules, all filled rectangles.
+ pdf.Bar(60, 100, 400, 22, (217, 217, 217));
+ pdf.Bar(60, 99, 400, 2).Bar(60, 165, 400, 2).Bar(59, 100, 2, 66).Bar(459, 100, 2, 66);
+ pdf.Bar(60, 121.75, 400, 0.5).Bar(60, 143.75, 400, 0.5);
+ pdf.Bar(199.75, 100, 0.5, 66).Bar(329.75, 100, 0.5, 66);
+ string[][] cells = [["Test", "Before", "After"], ["Speed", "12 s", "4 s"], ["Memory", "800 MB", "300 MB"]];
+ double[] xs = [60, 200, 330];
+ for (var r = 0; r < 3; r++)
+ {
+ for (var c = 0; c < 3; c++)
+ Cell(pdf, cells[r][c], xs[c], 100 + 22 * r, bold: r == 0);
+ }
+ var docx = await ConvertAsync(pdf.Save(Path.Combine(dir, "bars.pdf")), "docx");
+
+ ValidPackage(docx);
+ var table = Assert.Single(ReadBack(docx).Sections.SelectMany(s => s.Blocks).OfType<HTable>());
+ Assert.Equal(cells, Texts(table));
+ var corner = table.Rows[0].Cells[0];
+ var inner = table.Rows[1].Cells[1];
+ Assert.True(corner.Borders!.Value.Top.WidthMm > 2 * inner.Borders!.Value.Top.WidthMm, $"{corner.Borders.Value.Top.WidthMm} vs {inner.Borders.Value.Top.WidthMm}");
+ Assert.InRange(corner.Borders.Value.Left.WidthMm, 0.5, 1);
+ Assert.Equal("#D9D9D9", corner.Fill);
+ Assert.Null(inner.Fill);
+ }
+
+ [Fact]
+ public async Task Boxes_underlines_and_empty_grids_stay_text()
+ {
+ var dir = fx.NewFolder();
+ var pdf = new PdfBuilder().NewPage();
+ var body = pdf.Font();
+ // A note in a box with a rule under its title.
+ pdf.Rule(50, 70, 545, 70).Rule(50, 140, 545, 140).Rule(50, 70, 50, 140).Rule(545, 70, 545, 140);
+ pdf.Line("Note", 11, pdf.Font(bold: true), 60, 88);
+ pdf.Rule(50, 95, 545, 95);
+ pdf.Paragraph("A box drawn around a note is no table, even with a rule under its title line.", 11, body, 60, 112, 470, 14);
+ // Underlined words and a separator.
+ pdf.Line("Important words", 11, body, 50, 170);
+ pdf.Rule(50, 172, 50 + pdf.Width("Important words", 11, body), 172);
+ pdf.Rule(50, 190, 545, 190, 1);
+ // An empty chart grid with its labels outside.
+ foreach (var y in new double[] { 220, 260, 300 })
+ pdf.Rule(100, y, 400, y);
+ foreach (var x in new double[] { 100, 250, 400 })
+ pdf.Rule(x, 220, x, 300);
+ pdf.Line("Chart axis label", 9, body, 200, 315);
+ var docx = await ConvertAsync(pdf.Save(Path.Combine(dir, "boxes.pdf")), "docx");
+
+ ValidPackage(docx);
+ var blocks = ReadBack(docx).Sections.SelectMany(s => s.Blocks).ToList();
+ Assert.Empty(blocks.OfType<HTable>());
+ var texts = blocks.OfType<HParagraph>().Select(p => TextOf(p).Trim()).Where(t => t.Length > 0).ToList();
+ Assert.Equal(["Note", "A box drawn around a note is no table, even with a rule under its title line.", "Important words", "Chart axis label"], texts);
+ }
+}