PDF文字搜索C#

我要求阅读pdf文件并搜索文本。 我应该在哪个页面显示文本存在和出现次数。 我可以阅读pdf文本,但我需要知道页码。

谢谢

你可以使用Docotic.Pdf (我为Bit Miracle工作)。

以下是如何在PDF中搜索文本的示例:

PdfDocument doc = new PdfDocument("file.pdf"); string textToSearch = "some text"; for (int i = 0; i < doc.Pages.Count; i++) { string pageText = doc.Pages[i].GetText(); int count = 0; int lastStartIndex = pageText.IndexOf(textToSearch, 0, StringComparison.CurrentCultureIgnoreCase); while (lastStartIndex != -1) { count++; lastStartIndex = pageText.IndexOf(textToSearch, lastStartIndex + 1, StringComparison.CurrentCultureIgnoreCase); } if (count != 0) Console.WriteLine("Page {0}: '{1}' found {2} times", i, textToSearch, count); } 

如果要执行区分大小写的搜索,可能需要删除IndexOf方法的第三个参数。

你检查过itextsharp了吗? http://itextsharp.sourceforge.net/

编辑:详细说明,在TOC中,我看到了一节:15.3.3:使用PdfReaderContentParser和PdfTextExtractor提取文本

在PdfReaderContentParser: http ://api.itextpdf.com/com/itextpdf/text/pdf/parser/PdfReaderContentParser.html下,有一个选项可以处理每页的pdf内容。

所以它似乎是一个圆形的方式,但你可以遍历每个页面,搜索你想要的单词的内容,然后返回你找到它的页面。