Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Change PDF text extraction logics #10420

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 84 additions & 84 deletions test/docfx.Snapshot.Tests/SamplesTest.Seed/api/toc.pdf.verified.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

160 changes: 80 additions & 80 deletions test/docfx.Snapshot.Tests/SamplesTest.Seed/md/toc.pdf.verified.json

Large diffs are not rendered by default.

254 changes: 127 additions & 127 deletions test/docfx.Snapshot.Tests/SamplesTest.Seed/pdf/toc.pdf.verified.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

44 changes: 22 additions & 22 deletions test/docfx.Snapshot.Tests/SamplesTest.Seed/toc.pdf.verified.json

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion test/docfx.Snapshot.Tests/SamplesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using UglyToad.PdfPig;
using UglyToad.PdfPig.Actions;
using UglyToad.PdfPig.Annotations;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
using UglyToad.PdfPig.Outline;

namespace Docfx.Tests;
Expand Down Expand Up @@ -80,7 +82,7 @@ void PdfToJson(string path)
{
p.Number,
p.NumberOfImages,
p.Text,
Text = ExtractText(p),
Links = p.ExperimentalAccess.GetAnnotations().Select(ToLink).ToArray(),
}).ToArray(),
Bookmarks = document.TryGetBookmarks(out var bookmarks) ? ToBookmarks(bookmarks.Roots) : null,
Expand Down Expand Up @@ -207,4 +209,24 @@ private void ScrubFile(string path, StringBuilder builder)
}));
}
}

private string ExtractText(Page page)
{
// Gets PDF text content
var text = ContentOrderTextExtractor.GetText(page, new ContentOrderTextExtractor.Options { ReplaceWhitespaceWithSpace = true });

// string.Normalize is not works when using `Globalization Invariant Mode`.
StringBuilder sb = new(text);

// Normalize known ligature chars. (Note: `string.Normalize` is not works when using `Globalization Invariant Mode`)
sb.Replace("ff", "ff");
sb.Replace("ffi", "ffi");
sb.Replace("fl", "fl");
sb.Replace("fi", "fi");

// Normalize newline char.
sb.Replace("\r\n", "\n");

return sb.ToString();
}
}