<分区>
我需要阅读 pdf 文件并搜索文本。 我应该显示文本存在于哪个页面以及出现的次数。 我可以阅读 pdf 到文本,但我需要知道页码。
谢谢
<分区>
我需要阅读 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
方法的第三个参数。
关于c# - PDF 文本搜索 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893898/