2009-11-15 171 views
1

我正在使用PDFBox作爲C#.NET項目。從PDF文件中提取文本

FileStream stream = new FileStream(@"C:\1.pdf",FileMode.Open); 

    //retrieve the pdf bytes from the stream. 
    byte[] pdfbytes=new byte[65000]; 

    stream.Read(pdfbytes, 0, 65000); 

//get the pdf file bytes. 
allbytes = pdfbytes; 

//create a stream from the file bytes. 
java.io.InputStream ins = new java.io.ByteArrayInputStream(allbytes); 
string txt; 

//load the doc 
PDDocument doc = PDDocument.load(ins); 
PDFTextStripper stripper = new PDFTextStripper(); 

//retrieve the pdf doc's text 
txt = stripper.getText(doc); 
doc.close(); 

發生在3日聲明的除外:

,我得到一個「TypeInitializationException」執行下面的代碼塊時(對於「java.lang.Throwable的」的類型初始值引發異常。)
PDDocument doc = PDDocument.load(ins); 

我該怎麼做才能解決這個問題?

這是堆棧跟蹤:

  at java.lang.Throwable.__<map>(Exception , Boolean) 
    at org.pdfbox.pdfparser.PDFParser.parse() 
    at org.pdfbox.pdmodel.PDDocument.load(InputStream input, RandomAccess scratchFile) 
    at org.pdfbox.pdmodel.PDDocument.load(InputStream input) 
    at At.At.ExtractTextFromPDF(InputStream fileStream) in 
C:\Users\Administrator\Documents\Visual Studio 2008\Projects\AtProject\Att\At.cs:line 61 

設置InnerException的內部異常:

  • 的InnerException {「無法加載文件或程序集「IKVM.Runtime,版本= 0.30.0.0,文化=中性,PublicKeyToken = 13235d27fcbfff58'或其依賴項之一。系統找不到指定的文件。「:」IKVM.Runtime,Version = 0.30.0.0,Culture = neutral,PublicKeyToken = 13235d27fcbfff58「} System.Exception {System.IO .FileNotFoundException}

好的,我通過將PDFBox的某些.dll文件複製到我的bin文件夾中解決了上一個問題。但現在我得到這個錯誤:expected ='/'actual ='。' - 1 [email protected]

是否有任何替代使用PDFBox?有沒有其他可靠的庫,我可以用它來從pdf文件中提取文本。

+2

PDFBox的是一個Java庫,你的代碼看起來像Java。 C#涉及哪裏? – dtb 2009-11-15 19:03:11

+0

「TypeInitializationException」的內部異常是什麼? – dtb 2009-11-15 19:04:22

+2

有點困惑;你說它的C#,但它的Java。 而在Java中,字符串類型爲「String」,但您使用「string」 – alternative 2009-11-15 19:06:16

回答

2

它看起來像你缺少一些PDFBox庫。您需要:

  • IKVM.GNU.Classpath.dll
  • PDFBox的-XXXdll
  • FontBox-XXX-dev.dll
  • IKVM.Runtime.dll

閱讀本主題Read from a PDF file using C#。您可以在本主題的評論中找到類似問題的討論。

+0

感謝您的迴應Sasha。 雖然我已經解決了這個問題。 我現在正面臨另一個:「expected ='/'actual ='。' - 1 [email protected]」。似乎這不會發生所有的PDF文件,但與一些。 – Attilah 2009-11-15 21:54:28

+0

它看起來像你的PDF文件有問題。它是否合適?你可以發佈鏈接到它,我會嘗試測試這種情況? – Sasha 2009-11-16 07:37:24

+0

我在這裏的WCF客戶端/ WCF服務場景。所以,我通過流式發送文件到WCF服務,然後嘗試從文本中收到文本。也許這就是問題所在。 – Attilah 2009-11-17 14:27:10

1

我發現DLL文件的版本是罪魁禍首。 轉到http://www.netlikon.de/docs/PDFBox-0.7.2/bin/?C=M;O=A和下載以下文件:

  • IKVM.Runtime.dll
  • IKVM.GNU.Classpath.dll
  • PDFBox的-0.7.2.dll

然後將它們複製插入到Visual Studio項目的根目錄中。右鍵單擊項目並添加引用,找到所有3並添加它們。

最後這裏是我以前的PDF解析成文本代碼

C#

private static string TransformPdfToText(string SourceFile) 
{ 
string content = ""; 
PDDocument doc = new PDDocument(); 
PDFTextStripper stripper = new PDFTextStripper(); 
doc.close(); 
doc = PDDocument.load(SourceFile); 

try 
{ 
content = stripper.getText(doc); 
doc.close(); 
} 
catch (Exception ex) 
{ 
Console.WriteLine(ex.Message); 
} 
finally 
{ 
doc.close(); 
} 
return content; 
} 

的Visual Basic

Private Function parseUsingPDFBox(ByVal filename As String) As String 
    LogFile(" Attempting to parse file: " & filename) 
    Dim doc As PDDocument = New PDDocument() 
    Dim stripper As PDFTextStripper = New PDFTextStripper() 
    doc.close() 
    doc = PDDocument.load(filename) 

    Dim content As String = "empty" 
    Try 
     content = stripper.getText(doc) 
     doc.close() 
    Catch ex As Exception 
     LogFile(" Error parsing file: " & filename & vbcrlf & ex.Message) 
    Finally 
     doc.close() 
    End Try 
    Return content 
End Function