2009-04-02 35 views

Trả lời

2

Ví dụ này sử dụng thư viện bao gồm Adobe Reader, và xuất phát từ 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"); 
} 
+1

Cảm ơn bạn đã nhập mã! Tuy nhiên, mẫu này cần cài đặt Adobe Professional. Vì lý do bản quyền, tôi muốn có một thành phần để làm điều này. – splattne

2

Bạn có thể sử dụng Docotic.Pdf library để tìm kiếm văn bản trong các tập tin PDF.

Tiếp theo mẫu cho thấy làm thế nào để tìm chuỗi xác định trong một tập tin PDF và số trang tương ứng:

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); 
      } 
     } 
    } 
} 

Một tìm kiếm case-sensitive có thể được thực hiện nếu bạn loại bỏ số thứ ba của phương pháp IndexOf.

Tuyên bố từ chối trách nhiệm: Tôi làm việc cho Bit Miracle, nhà cung cấp thư viện.

Các vấn đề liên quan