2009-11-15 173 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