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:
Richard
2026-10-01 18:45:03 +02:00
co-authored by Claude Opus 5.5
parent cd1d4501e4
commit 347b35fc44
6 changed files with 91 additions and 7 deletions
@@ -109,10 +109,40 @@ public sealed class DocumentTextTests
}
[Fact]
public void Pdf_and_unknown_files_are_named_not_guessed()
public void Broken_and_unknown_files_are_named_not_guessed()
{
DocumentText.Extract([1, 2, 3], "invoice.pdf", 100).Problem!.ShouldContain("PDF");
DocumentText.Extract([1, 2, 3], "image.png", 100).Ok.ShouldBeFalse();
DocumentText.Extract([1, 2, 3], "broken.docx", 100).Ok.ShouldBeFalse();
}
private static byte[] Pdf(params string?[] pages)
{
var builder = new UglyToad.PdfPig.Writer.PdfDocumentBuilder();
var font = builder.AddStandard14Font(UglyToad.PdfPig.Fonts.Standard14Fonts.Standard14Font.Helvetica);
foreach (var text in pages)
{
var page = builder.AddPage(UglyToad.PdfPig.Content.PageSize.A4);
if (text is not null)
page.AddText(text, 12, new UglyToad.PdfPig.Core.PdfPoint(40, 700), font);
}
return builder.Build();
}
[Fact]
public void A_pdf_comes_out_page_by_page()
{
var text = DocumentText.Extract(Pdf("Invoice 4711", "Total 150.00 EUR"), "invoice.pdf", 10_000);
text.Ok.ShouldBeTrue(text.Problem);
text.Kind.ShouldBe("PDF");
text.Text.ShouldContain("## Page 1");
text.Text.ShouldContain("Invoice 4711");
text.Text.ShouldContain("## Page 2");
text.Text.ShouldContain("Total 150.00 EUR");
}
[Fact]
public void A_pdf_without_text_is_named_as_probably_scanned()
=> DocumentText.Extract(Pdf((string?)null), "scan.pdf", 10_000).Problem!.ShouldContain("no text layer");
}