2009-02-20 3 views
16

내가 특정 문자열이 존재하는지 확인하기 위해 pdf 파일을 검색 할 필요가있다. 문제의 문자열은 텍스트로 인코딩됩니다 (예 : 이미지 나 다른 것이 아닙니다). 파일을 일반 텍스트처럼 검색하려고 시도했지만 작동하지 않습니다.프로그래밍 C#에서 PDF 문서를 검색하는 방법

가능합니까? .net2.0을위한 라이브러리가 있습니까? 저에게 pdf 파일의 모든 텍스트를 추출/디코딩합니까?

답변

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

Docotic.Pdf library을 사용하면 PDF 파일의 텍스트를 검색 할 수 있습니다. 여기

은 샘플 코드입니다 :

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.

면책 조항 : 나는 비트의 기적, 라이브러리의 공급 업체에 대한 작동합니다.

관련 문제