问题
I am working on a project where Telerik's Document Processing libraries are a available to me, and I was hoping that I would be able to use it to read a PDF file and search for specific text that I can use for other processing. But while the code to do so seems straightforward, I am not actually getting expected results. This is the proof of concept I threw together:
var fs = new FileStream("..\\some.pdf", FileMode.Open);
RadFixedDocument doc = new PdfFormatProvider(fs).Import();
var pageCt = 0;
var elementCt = 0;
foreach (var page in doc.Pages) {
pageCt += 1;
Console.WriteLine($"Page {pageCt}, (Has content: {page.HasContent}, {page.Content.Count})");
foreach (var contentEl in page.Content) {
elementCt += 1;
Console.WriteLine($"Element {elementCt}");
if (contentEl is TextFragment) {
string text = (contentEl as TextFragment).Text;
Console.WriteLine(text);
// if (text.Contains("{{CustomTag}}")) {
// Console.WriteLine(text);
// } else {
// Console.Write(".");
// }
}
else {
Console.WriteLine($"Content Type: {contentEl.GetType().ToString()}");
}
}
}
I have tested this on a number of documents, but while it seems to pick out the proper number of pages, each page reports HasContent
is false
and the Content
collection is empty.
Am I not correct in thinking I should be able to step through the PDF content elements this way?
回答1:
OK. This is a pretty strange deal, but with a little help from a colleague, we managed to get this working. Turns out the difference is in how you apply the FileStream
.
So instead of
RadFixedDocument doc = new PdfFormatProvider(fs).Import();
We used
RadFixedDocument doc = new PdfFormatProvider().Import(fs);
And with everything else the same - it works.
来源:https://stackoverflow.com/questions/50164826/can-i-use-telerik-document-processing-to-read-pdf-content