Werkzeugkasten: PDF-Text aus Anhaengen und Dateien (#692)
- DocumentText reads PDF with PdfPig 0.1.16 (managed, Apache-2.0): page by page in reading order, at most 300 pages; a PDF without a text layer is named as probably scanned; broken and protected files are named. - Mail read_attachment and FileRW read/read_many take PDF like Word/Excel. - nuget.config maps exactly "PdfPig" - "UglyToad.PdfPig" is a package of a different account and stays out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cd1d4501e4
commit
347b35fc44
@@ -23,6 +23,11 @@
|
||||
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="10.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8" />
|
||||
|
||||
<!-- #692: PDF text, managed code only, Apache-2.0. Das Paket heisst "PdfPig" (Eigentuemer
|
||||
EliotJones/BobLd, github.com/UglyToad/PdfPig) - NICHT "UglyToad.PdfPig": unter diesem
|
||||
Namen liegt eine fremde Vorabversion eines anderen Kontos. -->
|
||||
<PackageReference Include="PdfPig" Version="0.1.16" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -22,8 +22,8 @@ public sealed record DocumentExtraction(bool Ok, string Text, string Kind, bool
|
||||
/// <para><b>Bounded.</b> Office files are zip archives, and a zip can unpack to anything. Every
|
||||
/// part is read up to <see cref="MaxPartBytes"/>, the whole up to the caller's character limit.</para>
|
||||
///
|
||||
/// <para>PDF is not read here yet: a reliable PDF text extractor is a library decision, not a
|
||||
/// hundred lines of code — fonts with their own encodings come out as garbage otherwise.</para>
|
||||
/// <para><b>PDF</b> is read with PdfPig (managed code only): the text of each page in reading
|
||||
/// order. A scanned PDF has no text layer and is named as such — reading it would need OCR.</para>
|
||||
/// </summary>
|
||||
public static class DocumentText
|
||||
{
|
||||
@@ -40,7 +40,10 @@ public static class DocumentText
|
||||
|
||||
/// <summary>Whether the file is a document whose text needs extracting (not plain text).</summary>
|
||||
public static bool IsOfficeDocument(string fileName)
|
||||
=> Path.GetExtension(fileName).ToLowerInvariant() is ".docx" or ".xlsx";
|
||||
=> Path.GetExtension(fileName).ToLowerInvariant() is ".docx" or ".xlsx" or ".pdf";
|
||||
|
||||
/// <summary>Pages read from one PDF — an invoice has a few, a catalogue is not read whole.</summary>
|
||||
public const int MaxPdfPages = 300;
|
||||
|
||||
public static DocumentExtraction Extract(byte[] content, string fileName, int maxChars)
|
||||
{
|
||||
@@ -51,7 +54,7 @@ public static class DocumentText
|
||||
{
|
||||
".docx" => Bounded(Docx(content), "Word document", maxChars),
|
||||
".xlsx" => Bounded(Xlsx(content), "spreadsheet", maxChars),
|
||||
".pdf" => DocumentExtraction.Fail("PDF", "PDF text cannot be read in this version yet."),
|
||||
".pdf" => Pdf(content, maxChars),
|
||||
_ when PlainText.Contains(ext) => Bounded(Decode(content), "text", maxChars),
|
||||
_ => DocumentExtraction.Fail(ext.TrimStart('.'), $"'{ext}' files cannot be read as text.")
|
||||
};
|
||||
@@ -62,6 +65,44 @@ public static class DocumentText
|
||||
}
|
||||
}
|
||||
|
||||
// ─── PDF ───
|
||||
|
||||
private static DocumentExtraction Pdf(byte[] content, int maxChars)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var document = UglyToad.PdfPig.PdfDocument.Open(content, new UglyToad.PdfPig.ParsingOptions { UseLenientParsing = true });
|
||||
var sb = new StringBuilder();
|
||||
var pages = document.NumberOfPages;
|
||||
var read = 0;
|
||||
foreach (var page in document.GetPages())
|
||||
{
|
||||
if (read >= MaxPdfPages || sb.Length > maxChars)
|
||||
break;
|
||||
read++;
|
||||
var text = UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor.ContentOrderTextExtractor.GetText(page).Trim();
|
||||
if (pages > 1)
|
||||
sb.AppendLine($"## Page {page.Number}");
|
||||
sb.AppendLine(text).AppendLine();
|
||||
}
|
||||
|
||||
var all = sb.ToString().Trim();
|
||||
if (all.Replace("## Page", "").All(c => char.IsWhiteSpace(c) || char.IsDigit(c)))
|
||||
return DocumentExtraction.Fail("PDF", "The PDF has no text layer — it is probably scanned, and reading it would need OCR.");
|
||||
if (read < pages)
|
||||
all += $"\n\n(Read {read} of {pages} pages.)";
|
||||
var bounded = Bounded(all, "PDF", maxChars);
|
||||
return bounded with { Truncated = bounded.Truncated || read < pages };
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
// PdfPig throws its own exceptions for broken and encrypted files.
|
||||
return DocumentExtraction.Fail("PDF", ex.Message.Contains("password", StringComparison.OrdinalIgnoreCase) || ex.Message.Contains("encrypt", StringComparison.OrdinalIgnoreCase)
|
||||
? "The PDF is protected by a password."
|
||||
: $"The PDF could not be read: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static DocumentExtraction Bounded(string text, string kind, int maxChars)
|
||||
{
|
||||
var limit = Math.Max(1, maxChars);
|
||||
|
||||
Reference in New Issue
Block a user