c# - PDF 文本搜索 C#

标签 c# pdf c#-3.0 full-text-search

<分区>

我需要阅读 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/

相关文章:

c# - 在 WCF 中使用创建自签名证书

c# - 使用电子表格设备将图像添加到 Excel 标题?

android - 如何将 Android 中的大型 pdf 文件加载到我的 webview 中

c# - 如何将 Windows 窗体保存为 pdf

c# - 扩展 IQueryable 并解析属性

c# - 如何为 C# 泛型集合获取一致的 .Count/.Length 属性?

c# - 如何在 XAML 中表达静态代码?

java - 使用 PDFBox 在 PDF 文件中绘制自动调整大小的图像

c# - 如何作为委托(delegate)传递属性?

winforms - 如何增加工具条上按钮的大小?