2009-02-20 19 views
16

特定の文字列があるかどうかを確認するには、pdfファイルを検索する必要があります。問題の文字列は確かにテキストとしてエンコードされています(つまり、画像などではありません)。私はそれがプレーンテキストであるかのようにファイルを検索しようとしましたが、これはうまくいきません。C言語でPDF文書をプログラムで検索する方法

これは可能ですか?私のためにpdfファイルからすべてのテキストを抽出/デコードする.net2.0のライブラリがありますか?

答えて

1

大部分の場合、PDFの内容をメモ帳で開くことで直接検索することはできません。少数の場合でも(PDFの作成方法によっては) PDFが内部的にテキストを処理する方法のために、個々の単語を検索することしかできません。

私の会社には、PDFファイルからテキストを抽出できる商用ソリューションがあります。私はいくつかのサンプルコード、as shown on this pageを含んでおり、特定の文字列のPDFファイルからテキストを検索する方法を示しています。

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ファイル内のテキストを検索するためにDocotic.Pdf libraryを使用することができます。

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

ライブラリすることもできextract formatted and plain text文書全体または任意のドキュメントのページから:ここで

は、サンプルコードです。

免責事項:私はBit Miracle、ライブラリのベンダーのために働いています。

関連する問題