PDF文本搜索C# [英] PDF Text search C#

查看:215
本文介绍了PDF文本搜索C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须要求阅读PDF文件,然后搜索文本。
我应该显示在页面文本存在和出现的次数。
我可以阅读PDF格式的文本,但我需要知道的页码。

I have requirement to read a pdf file and search for a text. I should display in which page that text exist and the number of occurances. I can read the pdf to text but i need to know the page number.

感谢

推荐答案

您可以使用 Docotic.Pdf 这个(我的奇迹位工作)。

You can use Docotic.Pdf for this (I work for Bit Miracle).

下面是如何搜索的PDF文本的例子:

Here is a sample for how to search text in 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 方法。

You may want to remove third argument for IndexOf method if you want to perform case-sensitive search.

这篇关于PDF文本搜索C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆