2009-02-20 6 views
16

Ho bisogno di cercare un file pdf per vedere se è presente una determinata stringa. La stringa in questione è sicuramente codificata come testo (cioè non è un'immagine o altro). Ho provato a cercare il file come se fosse un testo semplice, ma questo non funziona.Come cercare un documento PDF a livello di programmazione in C#

È possibile farlo? Ci sono delle librerie là fuori per .net2.0 che estraggono/decodificano tutto il testo da file PDF per me?

risposta

1

Nella maggior parte dei casi, non è possibile cercare il contenuto di un PDF direttamente aprendolo nel blocco note e, anche in alcuni casi (a seconda di come è stato creato il PDF), cerca sempre e solo parole singole a causa del modo in cui il PDF gestisce il testo internamente.

La mia azienda ha una soluzione commerciale che consente di estrarre il testo da un file PDF. Di seguito ho incluso alcuni esempi di codice, as shown on this page, che dimostrano come cercare nel testo da un file PDF per una stringa particolare.

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

È possibile utilizzare Docotic.Pdf library per cercare il testo nei file PDF.

Ecco un esempio di codice:

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

La biblioteca può anche extract formatted and plain text dal l'intero documento o qualsiasi pagina del documento.

Disclaimer: Lavoro per Bit Miracle, venditore della biblioteca.