2009-02-20 45 views
16

Belirli bir dizenin bulunup bulunmadığını görmek için bir pdf dosyası aramam gerekiyor. Söz konusu dizi metin olarak kesinlikle kodlanmıştır (yani, bir görüntü veya herhangi bir şey değildir). Dosyayı düz metinmiş gibi aramayı denedim, ancak bu işe yaramıyor.Bir PDF belgesini program aracılığıyla arama nasıl C#

Bunu yapmak mümkün mü? Benim için pdf dosyası dışında tüm metni ayıklayacak/çözecek .net2.0 için herhangi bir librarys var mı?

cevap

1

Vakaların çoğunda, PDF içeriğini doğrudan not defterinde açarak ve hatta vakaların azınlığında bile (PDF'nin nasıl oluşturulduğuna bağlı olarak) aramak mümkün olmaz. PDF'in metinleri dahili olarak işleme biçiminden dolayı tek tek sözcükler arayabilir.

Şirketim, bir PDF dosyasından metin ayıklamanızı sağlayacak ticari bir çözüme sahiptir. Aşağıda, belirli bir dize için bir PDF dosyasındaki metni nasıl arayacağınızı gösteren as shown on this page için bazı örnek kodlar ekledim.

using System; 
using System.IO; 
using QuickPDFDLL0718; 

namespace QPLConsoleApp 
{ 
    public class QPL 
    { 
     public static void Main() 
     { 
      // This example uses the DLL edition of Quick PDF Library 
      // Create an instance of the class and give it the path to the DLL 
      PDFLibrary QP = new PDFLibrary("QuickPDFDLL0718.dll"); 

      // Check if the DLL was loaded successfully 
      if (QP.LibraryLoaded()) 
      { 
       // Insert license key here/Check the license key 
       if (QP.UnlockKey("...") == 1) 
       { 
        QP.LoadFromFile(@"C:\Program Files\Quick PDF Library\DLL\GettingStarted.pdf"); 

        int iPageCount = QP.PageCount(); 
        int PageNumber = 1; 
        int MatchesFound = 0; 

        while (PageNumber <= iPageCount) 
        { 
         QP.SelectPage(PageNumber); 
         string PageText = QP.GetPageText(3); 

         using (StreamWriter TempFile = new StreamWriter(QP.GetTempPath() + "temp" + PageNumber + ".txt")) 
         { 
          TempFile.Write(PageText); 
         } 

         string[] lines = File.ReadAllLines(QP.GetTempPath() + "temp" + PageNumber + ".txt"); 
         string[][] grid = new string[lines.Length][]; 

         for (int i = 0; i < lines.Length; i++) 
         { 
          grid[i] = lines[i].Split(','); 
         } 

         foreach (string[] line in grid) 
         { 
          string FindMatch = line[11]; 

          // Update this string to the word that you're searching for. 
          // It can be one or more words (i.e. "sunday" or "last sunday". 

          if (FindMatch.Contains("characters")) 
          { 
           Console.WriteLine("Success! Word match found on page: " + PageNumber); 
           MatchesFound++; 
          } 
         } 
         PageNumber++; 
        } 

        if (MatchesFound == 0) 
        { 
         Console.WriteLine("Sorry! No matches found."); 
        } 
        else 
        { 
         Console.WriteLine(); 
         Console.WriteLine("Total: " + MatchesFound + " matches found!"); 
        } 
        Console.ReadLine(); 
       } 
      } 
     } 
    } 
} 
2

PDF dosyalarında metin aramak için Docotic.Pdf library kullanabilirsiniz. İşte

bir örnek kod:

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

kütüphane bütün belge veya herhangi bir belge sayfasından Ayrıca extract formatted and plain text.

Yasal Uyarı: Kütüphanenin sağlayıcısı Bit Miracle için çalışıyorum. ITextSharp için

İlgili konular