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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
PDF to Word: TOC leaders, heading split, fixed line spacing (#897)
Marouane-elfadlicb3bc542a9b2
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.Layout.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.Layout.csindex 5e9162b6..b7a9e6de 100644--- a/src/Filee.Engines/Pdf/PdfDocumentReader.Layout.cs+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.Layout.cs@@ -16,6 +16,33 @@ internal sealed partial class PdfDocumentReader"^([•◦▪▫●○■□‣⁃∙·\\-–—*]|\\(?\\d{1,3}[.)]|\\(?[a-zA-Z][.)]|[ivxIVX]{1,4}[.)]|[①-⑳]|[가-하][.)])$",RegexOptions.CultureInvariant | RegexOptions.Compiled);+ /// <summary>+ /// A word that holds the dot leader of a table-of-contents entry: "Title.........12" is one word for PdfPig. The+ /// title before the dots and the page number after them are optional.+ /// </summary>+ private static readonly Regex LeaderWord = new(+ "^(?<title>.*?)(?<dots>[.…·.]{8,})(?<page>\\d{1,4})?$",+ RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.Singleline);++ private static readonly Regex PageDigits = new("^\\d{1,4}$", RegexOptions.CultureInvariant | RegexOptions.Compiled);++ /// <summary>+ /// Index of the word of <paramref name="words"/> that is the dot leader of a table-of-contents entry (the line+ /// ends with a page number, in the same word or in the word after the dots), or -1.+ /// </summary>+ private static int FindLeader(List<TextWord> words)+ {+ for (var i = 0; i < words.Count; i++)+ {+ var match = LeaderWord.Match(words[i].Text);+ if (!match.Success)+ continue;+ if (match.Groups["page"].Success || (i == words.Count - 2 && PageDigits.IsMatch(words[^1].Text)))+ return i;+ }+ return -1;+ }+// ───────────────────────── Lines ─────────────────────────/// <summary>@@ -147,6 +174,7 @@ internal sealed partial class PdfDocumentReaderprivate static List<Item> Paragraphs(List<TextLine> lines){var items = new List<Item>();+ var pagePitch = DominantPitch(lines);foreach (var line in lines){Item? best = null;@@ -156,13 +184,21 @@ internal sealed partial class PdfDocumentReaderif (line.Tabs.Count > 0)break; // a tabbed row starts its own paragraphvar last = item.Lines[^1];+ // Table-of-contents entries (title, dot leader, page number) are one paragraph each.+ if (line.LeaderIndex >= 0 || item.Lines[0].LeaderIndex >= 0)+ continue;// A tabbed row continues only with text under its last part (the wrapped text of "1. Text …").var head = item.Lines[0];if (head.Tabs.Count > 0 && Math.Abs(line.Left - head.Words[head.Tabs[^1]].Left) > 0.5 * line.Size)continue;var distance = last.Baseline - line.Baseline;var size = Math.Max(last.Size, line.Size);- if (distance <= 0 || distance > 1.9 * size || Math.Abs(last.Size - line.Size) > 0.2 * size)+ // A fixed line spacing (23 pt for 12 pt text is 1.92 em) is no paragraph gap: the page's usual pitch stays inside.+ var spaced = pagePitch > 0 && Math.Abs(distance - pagePitch) <= 0.08 * pagePitch;+ // Sizes must match closely: a 14 pt heading under 12 pt text is a paragraph of its own. The size of a line is+ // the one most of its characters have, so a single larger word (12 pt in 10.5 pt text) does not split the paragraph.+ var mainSize = Math.Max(last.MainSize, line.MainSize);+ if (distance <= 0 || (distance > 1.9 * size && !spaced) || Math.Abs(last.MainSize - line.MainSize) > Math.Max(1.0, 0.08 * mainSize))continue;if (item.Lines.Count >= 2 && distance > 1.3 * Pitch(item))continue; // more space than between the lines so far: a new paragraph@@ -187,6 +223,41 @@ internal sealed partial class PdfDocumentReaderreturn items;}+ /// <summary>+ /// The line pitch (baseline to baseline, in points) most lines of the page have to the line right below them+ /// (same size, overlapping horizontally, between 1 and 2.6 em); 0 when fewer than three pairs agree.+ /// </summary>+ private static double DominantPitch(List<TextLine> lines)+ {+ var pitches = new List<double>();+ foreach (var line in lines)+ {+ var nearest = double.MaxValue;+ foreach (var other in lines)+ {+ var distance = line.Baseline - other.Baseline;+ if (distance < 0.8 * line.Size || distance >= nearest || Math.Abs(line.Size - other.Size) > 0.5)+ continue;+ var overlap = Math.Min(line.Right, other.Right) - Math.Max(line.Left, other.Left);+ if (overlap < 0.3 * Math.Min(line.Right - line.Left, other.Right - other.Left))+ continue;+ nearest = distance;+ }+ if (nearest >= line.Size && nearest <= 2.6 * line.Size)+ pitches.Add(Math.Round(nearest * 2) / 2);+ }++ var bestPitch = 0.0;+ var bestCount = 0;+ foreach (var candidate in pitches.Distinct())+ {+ var count = pitches.Count(p => Math.Abs(p - candidate) <= 0.5);+ if (count > bestCount || (count == bestCount && candidate < bestPitch))+ (bestPitch, bestCount) = (candidate, count);+ }+ return bestCount >= 3 ? bestPitch : 0;+ }+private static bool StartsParagraph(Item item, TextLine line){var last = item.Lines[^1];diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.csindex 4cf94aed..71615c85 100644--- a/src/Filee.Engines/Pdf/PdfDocumentReader.cs+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.cs@@ -150,12 +150,27 @@ internal sealed partial class PdfDocumentReader/// <summary>Indexes of the words a tab leads to (the start of each part after the first of a tabbed row).</summary>public List<int> Tabs { get; } = [];+ private int? _leaderIndex;++ /// <summary>Index of the word holding the dot leader of a table-of-contents entry ("Title.....12"), -1 for other lines.</summary>+ public int LeaderIndex => _leaderIndex ??= FindLeader(Words);+public double Left => Words[0].Left;public double Right => Words[^1].Right;public double Top => Words.Max(w => w.Top);public double Bottom => Words.Min(w => w.Bottom);public double Baseline => Words[0].Baseline;public double Size => Words.Max(w => w.Size);++ private double? _mainSize;++ /// <summary>+ /// Size of most of the line's characters (rounded to half points), like <see cref="Item.Size"/>: one larger word+ /// (an emphasised word or a name) does not change it, a line that is larger throughout does.+ /// </summary>+ public double MainSize => _mainSize ??= Words.GroupBy(w => Math.Round(w.Size * 2) / 2)+ .OrderByDescending(g => g.Sum(w => w.Text.Length)).ThenByDescending(g => g.Key).Select(g => g.Key).First();+public string Text => string.Join(' ', Words.Select(w => w.Text));private bool? _rightToLeft;@@ -703,7 +718,8 @@ internal sealed partial class PdfDocumentReader}private static bool IsHeadingCandidate(Item item, double bodySize) =>- !item.IsPicture && item.Lines.Count <= 3 && item.Text.Length <= 200 && item.Size >= bodySize * 1.15 && item.Size >= bodySize + 1;+ !item.IsPicture && item.Lines.Count <= 3 && item.Text.Length <= 200 && item.Size >= bodySize * 1.15 && item.Size >= bodySize + 1+ && !item.Lines.Any(l => l.LeaderIndex >= 0); // a table-of-contents entry is no heading/// <summary>Margins shared by the text pages: the smallest distances from the paper edges to any text or picture.</summary>private static (double Left, double Right, double Top, double Bottom) Margins(List<PageContent> pages)@@ -754,19 +770,24 @@ internal sealed partial class PdfDocumentReader{var size = item.Size;var paragraph = new HParagraph();- if (headingLevels.TryGetValue(Math.Round(size), out var level) && item.Lines.Count <= 3 && item.Text.Length <= 200)+ var rightToLeft = item.RightToLeft;+ // A table-of-contents entry: title, dot leader and page number on one line. It becomes a left-aligned paragraph+ // (indented like the PDF's level) with a right tab with dot leader at the right edge of the text.+ var entry = !rightToLeft && item.Lines.Count == 1 && item.Lines[0].LeaderIndex >= 0;+ if (!entry && headingLevels.TryGetValue(Math.Round(size), out var level) && item.Lines.Count <= 3 && item.Text.Length <= 200)paragraph.HeadingLevel = level;// Alignment and indents relative to the column (or text area) the paragraph is in. A right-to-left paragraph// starts at the right: its alignment and indents are measured from that side (HParaFormat.RightToLeft).- var rightToLeft = item.RightToLeft;var containerWidth = Math.Max(1, item.ContainerRight - item.ContainerLeft);var lefts = item.Lines.Select(l => l.Left - item.ContainerLeft).ToList();var rights = item.Lines.Select(l => item.ContainerRight - l.Right).ToList();var (starts, ends) = rightToLeft ? (rights, lefts) : (lefts, rights);var tolerance = Math.Max(3, containerWidth * 0.03);HAlign align;- if (item.Lines.All(l => Math.Abs((l.Left - item.ContainerLeft) - (item.ContainerRight - l.Right)) < tolerance) && lefts.Min() > containerWidth * 0.04)+ if (entry)+ align = HAlign.Left;+ else if (item.Lines.All(l => Math.Abs((l.Left - item.ContainerLeft) - (item.ContainerRight - l.Right)) < tolerance) && lefts.Min() > containerWidth * 0.04)align = HAlign.Center;else if (ends.All(r => r < 3) && starts.Min() > containerWidth * 0.15)align = HAlign.Right;@@ -789,6 +810,11 @@ internal sealed partial class PdfDocumentReader// A tabbed row: a left tab stop where each part starts in the PDF.var tabs = rightToLeft ? [] : item.Lines.SelectMany(l => l.Tabs.Select(t => Hwp(l.Words[t].Left - item.ContainerLeft))).Distinct().Order().Select(position => new HTabStop(position, HTabKind.Left, "NONE")).ToList();+ if (entry)+ {+ tabs.Add(new HTabStop(Hwp(item.Right - item.ContainerLeft), HTabKind.Right, "DOT"));+ tabs = [.. tabs.OrderBy(t => t.Position)];+ }var pitch = item.Lines.Count >= 2 ? Pitch(item) : size * lineRatio;paragraph.Format = new HParaFormat(@@ -835,10 +861,27 @@ internal sealed partial class PdfDocumentReadervar line = item.Lines[l];// Words in reading order: a tabbed row keeps its parts where they are.var words = line.Tabs.Count > 0 ? line.Words : InReadingOrder(line.Words, rightToLeft);+ // The dot leader of a table-of-contents entry becomes a tab (the paragraph has a right tab with dot leader).+ var leaderIndex = !rightToLeft && ReferenceEquals(words, line.Words) ? line.LeaderIndex : -1;for (var w = 0; w < words.Count; w++){var word = words[w];var text = word.Text;+ string? leaderPage = null;+ if (w == leaderIndex)+ {+ var leader = LeaderWord.Match(text);+ text = leader.Groups["title"].Value.TrimEnd();+ leaderPage = leader.Groups["page"].Value;+ }+ if (text.Length == 0)+ {+ // No title in this word: the tab, then the page number if it is in this word too.+ pieces.Add(("\t", word.Format, null));+ if (!string.IsNullOrEmpty(leaderPage))+ pieces.Add((leaderPage, word.Format, null));+ continue;+ }if (pieces.Count > 0){var (previousText, previousFormat, previousLink) = pieces[^1];@@ -850,7 +893,7 @@ internal sealed partial class PdfDocumentReader{// Not between two Chinese or Japanese words: an OCR layer (Tesseract's) puts a space after// every word it found, also inside a sentence of Han characters (#691). Korean keeps them.- if (!(IsCjk(previousText[^1]) && IsCjk(text[0])))+ if (previousText != "\t" && !(IsCjk(previousText[^1]) && IsCjk(text[0])))pieces[^1] = (previousText + " ", previousFormat, previousLink);}else if (previousText.EndsWith('-') && previousText.Length > 1 && char.IsLetter(previousText[^2]) && text.Length > 0 && char.IsLower(text[0]))@@ -867,6 +910,12 @@ internal sealed partial class PdfDocumentReader}}pieces.Add((text, word.Format, word.Link));+ if (leaderPage is not null)+ {+ pieces.Add(("\t", word.Format, null));+ if (leaderPage.Length > 0)+ pieces.Add((leaderPage, word.Format, null));+ }}}diff --git a/tests/Filee.Engines.Tests/PdfReaderTests.cs b/tests/Filee.Engines.Tests/PdfReaderTests.csindex 5c908225..350ad3f4 100644--- a/tests/Filee.Engines.Tests/PdfReaderTests.cs+++ b/tests/Filee.Engines.Tests/PdfReaderTests.cs@@ -191,6 +191,123 @@ public class PdfReaderTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Contains("Ste. 200E\r\nWest Linn", text);}+ [Fact]+ public async Task Table_of_contents_entries_are_paragraphs_with_a_right_tab_with_dot_leader()+ {+ var pdf = new PdfBuilder().NewPage();+ var regular = pdf.Font();+ var bold = pdf.Font(bold: true);+ pdf.Line("Contents", 18, bold, 54, 80);+ string[] titles = ["1.1 Purpose", "1.2 Scope", "2.1 Design"];+ for (var i = 0; i < titles.Length; i++)+ pdf.Line(titles[i] + new string('.', 40) + (i + 1), 12, regular, 54, 130 + i * 20);+ var path = pdf.Save(Path.Combine(fx.NewFolder(), "contents.pdf"));++ var docx = await ConvertAsync(path, "docx");++ ValidPackage(docx);+ var paragraphs = Paragraphs(ReadBack(docx).Sections.SelectMany(s => s.Blocks)).Where(p => TextOf(p).Length > 0).ToList();+ // One paragraph per entry: the dots are a tab leader, not text, and the entries are no headings.+ var entries = paragraphs.Where(p => Shown(p).Contains('\t')).ToList();+ Assert.Equal(["1.1 Purpose\t1", "1.2 Scope\t2", "2.1 Design\t3"], entries.Select(Shown));+ Assert.All(entries, p =>+ {+ Assert.Equal(0, p.HeadingLevel);+ var tab = Assert.Single(p.Format.Tabs!);+ Assert.Equal(HTabKind.Right, tab.Kind);+ Assert.Equal("DOT", tab.Leader);+ });+ Assert.Equal(1, paragraphs.Single(p => TextOf(p) == "Contents").HeadingLevel);+ }++ [Fact]+ public async Task A_larger_line_under_a_paragraph_starts_its_own_paragraph()+ {+ var pdf = new PdfBuilder().NewPage();+ var regular = pdf.Font();+ var bold = pdf.Font(bold: true);+ var y = pdf.Paragraph(First, 12, regular, 54, 100, 495, 14.4);+ // 14 pt right below 12 pt text: close enough for a line of the paragraph, but a different size.+ pdf.Line("2.1.3 Design", 14, bold, 54, y + 2);+ var path = pdf.Save(Path.Combine(fx.NewFolder(), "heading.pdf"));++ var docx = await ConvertAsync(path, "docx");++ var paragraphs = Paragraphs(ReadBack(docx).Sections.SelectMany(s => s.Blocks)).Where(p => TextOf(p).Length > 0).ToList();+ Assert.Equal([First, "2.1.3 Design"], paragraphs.Select(p => TextOf(p).Trim()));+ }++ [Fact]+ public async Task One_larger_word_in_a_line_does_not_split_the_paragraph()+ {+ // 10.5 pt text (五号) with one 12 pt word (小四) on its second line: an emphasised word or a name, common in Chinese Word documents.+ const double Body = 10.5, Large = 12, X = 54, Max = 495;+ var pdf = new PdfBuilder().NewPage();+ var regular = pdf.Font();+ var text = $"{First} {Second}";++ // Wrap by hand (PdfBuilder.Paragraph draws a whole paragraph in one size).+ var lines = new List<string>();+ var current = "";+ foreach (var word in text.Split(' '))+ {+ var candidate = current.Length == 0 ? word : current + " " + word;+ if (current.Length > 0 && pdf.Width(candidate, Body, regular) > Max)+ {+ lines.Add(current);+ current = word;+ }+ else+ {+ current = candidate;+ }+ }+ lines.Add(current);+ Assert.True(lines.Count >= 3, "the paragraph needs at least three lines");++ for (var i = 0; i < lines.Count; i++)+ {+ var y = 100 + i * 14.0;+ if (i != 1)+ {+ pdf.Line(lines[i], Body, regular, X, y);+ continue;+ }+ // The middle word of the second line is 12 pt; the rest of the line stays 10.5 pt.+ var words = lines[i].Split(' ');+ var at = words.Length / 2;+ var before = string.Join(' ', words[..at]);+ var large = " " + words[at];+ var after = " " + string.Join(' ', words[(at + 1)..]);+ var x = X;+ pdf.Line(before, Body, regular, x, y);+ x += pdf.Width(before, Body, regular);+ pdf.Line(large, Large, regular, x, y);+ x += pdf.Width(large, Large, regular);+ pdf.Line(after, Body, regular, x, y);+ }+ var path = pdf.Save(Path.Combine(fx.NewFolder(), "larger-word.pdf"));++ var docx = await ConvertAsync(path, "docx");++ var paragraphs = Paragraphs(ReadBack(docx).Sections.SelectMany(s => s.Blocks)).Where(p => TextOf(p).Length > 0).ToList();+ Assert.Equal([text], paragraphs.Select(p => TextOf(p).Trim()));+ }++ [Fact]+ public async Task Fixed_line_spacing_wider_than_1_9_em_stays_one_paragraph()+ {+ var pdf = new PdfBuilder().NewPage();+ var text = $"{First} {Second} {First}";+ pdf.Paragraph(text, 12, pdf.Font(), 54, 100, 495, 23); // 23 pt for 12 pt text, as a fixed line spacing of 1.92 em+ var path = pdf.Save(Path.Combine(fx.NewFolder(), "spaced.pdf"));++ var docx = await ConvertAsync(path, "docx");++ var paragraphs = Paragraphs(ReadBack(docx).Sections.SelectMany(s => s.Blocks)).Where(p => TextOf(p).Length > 0).ToList();+ Assert.Equal([text], paragraphs.Select(p => TextOf(p).Trim()));+ }+[Fact]public async Task Scanned_pages_become_page_pictures(){