Can I use Telerik Document Processing to read PDF content?

本小妞迷上赌 提交于 2019-12-13 00:28:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!