以编程方式搜索 PDF 文件中的文本并告知页码?

发布于 2024-07-16 21:53:25 字数 1539 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-07-23 21:53:25

此示例使用 Adob​​e Reader 附带的库,来自 http ://www.dotnetspider.com/resources/5040-Get-PDF-Page-Number.aspx

using Acrobat;
using AFORMAUTLib;                          
private void pdfRandD(string fPath)
{
    AcroPDDocClass objPages = new AcroPDDocClass();
    objPages.Open(fPath);
    long TotalPDFPages = objPages.GetNumPages();            
    objPages.Close();
    AcroAVDocClass avDoc = new AcroAVDocClass();
    avDoc.Open(fPath, "Title");
    IAFormApp formApp = new AFormAppClass();
    IFields myFields = (IFields)formApp.Fields;            
    string searchWord = "Search String";
    string k = "";
    StreamWriter sw = new
        StreamWriter(@"D:\KCG_FileChecker_Inputs\MAC\pdf\0230_525490_23_cha17.txt", false);
    for (int p = 0; p < TotalPDFPages; p++)
    {                
        int numWords = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageNumWords(" + p + ");"));
        k = "";
        for (int i = 0; i < numWords; i++)
        {
            string chkWord = myFields.ExecuteThisJavascript("event.value=this.getPageNthWord(" + p + "," + i + ", true);");
            k = k + " " + chkWord;
        }                
        if(k.Trim().Contains(searchWord))
        {
           int pNum = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageLabel(" + p + ",true);"));
           sw.WriteLine("The Word " + searchWord + " is exists in " + pNum);                    
        }

     }
     sw.Close();
     MessageBox.Show("Process completed");
}

This example uses the library included with Adobe Reader, and comes from http://www.dotnetspider.com/resources/5040-Get-PDF-Page-Number.aspx:

using Acrobat;
using AFORMAUTLib;                          
private void pdfRandD(string fPath)
{
    AcroPDDocClass objPages = new AcroPDDocClass();
    objPages.Open(fPath);
    long TotalPDFPages = objPages.GetNumPages();            
    objPages.Close();
    AcroAVDocClass avDoc = new AcroAVDocClass();
    avDoc.Open(fPath, "Title");
    IAFormApp formApp = new AFormAppClass();
    IFields myFields = (IFields)formApp.Fields;            
    string searchWord = "Search String";
    string k = "";
    StreamWriter sw = new
        StreamWriter(@"D:\KCG_FileChecker_Inputs\MAC\pdf\0230_525490_23_cha17.txt", false);
    for (int p = 0; p < TotalPDFPages; p++)
    {                
        int numWords = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageNumWords(" + p + ");"));
        k = "";
        for (int i = 0; i < numWords; i++)
        {
            string chkWord = myFields.ExecuteThisJavascript("event.value=this.getPageNthWord(" + p + "," + i + ", true);");
            k = k + " " + chkWord;
        }                
        if(k.Trim().Contains(searchWord))
        {
           int pNum = int.Parse(myFields.ExecuteThisJavascript("event.value=this.getPageLabel(" + p + ",true);"));
           sw.WriteLine("The Word " + searchWord + " is exists in " + pNum);                    
        }

     }
     sw.Close();
     MessageBox.Show("Process completed");
}
时常饿 2024-07-23 21:53:25

您可以使用 Docotic.Pdf 库 搜索 PDF 文件中的文本。

以下示例展示了如何在 PDF 文件中查找指定字符串以及相应的页码:

static void searchForTextStrings()
{
    string path = "";
    string[] stringsToFind = new string[] { };

    using (PdfDocument pdf = new PdfDocument(path))
    {
        for (int i = 0; i < pdf.Pages.Count; i++)
        {
            string pageText = pdf.Pages[i].GetText();
            foreach (string s in stringsToFind)
            {
                int index = pageText.IndexOf(s, 0, StringComparison.CurrentCultureIgnoreCase);
                if (index != -1)
                    Console.WriteLine("'{0}' found on page {1}", s, i);
            }
        }
    }
}

如果删除 IndexOf 方法的第三个参数,则可以进行区分大小写的搜索。

免责声明:我为 Bit Miracle 工作,该库的供应商。

You can use Docotic.Pdf library to search for text in PDF files.

Following sample shows how to find specified strings in a PDF file and corresponding page numbers:

static void searchForTextStrings()
{
    string path = "";
    string[] stringsToFind = new string[] { };

    using (PdfDocument pdf = new PdfDocument(path))
    {
        for (int i = 0; i < pdf.Pages.Count; i++)
        {
            string pageText = pdf.Pages[i].GetText();
            foreach (string s in stringsToFind)
            {
                int index = pageText.IndexOf(s, 0, StringComparison.CurrentCultureIgnoreCase);
                if (index != -1)
                    Console.WriteLine("'{0}' found on page {1}", s, i);
            }
        }
    }
}

A case-sensitive search can be conducted if you remove third argument of IndexOf method.

Disclaimer: I work for Bit Miracle, vendor of the library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文