2

我将 PDFBox 用于 C# .NET 项目。并且在执行以下代码块时,我得到了“TypeInitializationException”('java.lang.Throwable' 的类型初始化程序引发了异常。):

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

异常发生在第三条语句:

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,Version = 0.30.0.0,Culture = Neutral,PublicKeyToken = 13235d27fcbfff58'或其依赖项之一。系统找不到指定的文件。”:“IKVM.Runtime,版本=0.30.0.0,文化=中性,PublicKeyToken=13235d27fcbfff58"} System.Exception {System.IO.FileNotFoundException}

好的,我通过将PDFBox的一些.dll文件复制到我的bin文件夹来解决了前面的问题。但现在我收到了这个错误:expected='/'actual='.'--1 org.pdfbox.io.PushBackInputStream@283d742

有没有使用 PDFBox 的替代方法?有没有其他可靠的库可以用来从 pdf 文件中提取文本。

4

3 回答 3

2

看起来您缺少一些 PDFBox 库。你需要:

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

阅读本主题使用 C# 从 PDF 文件中读取。您可以在本主题的评论中找到类似问题的讨论。

于 2009-11-15T21:50:40.377 回答
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;
}

视觉基础

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
于 2012-01-31T18:32:35.363 回答
-1

有类似的问题,但不是 C++,而是 VisualBasic/VisualStudio;缺少的 dll 是“commons-logging.dll”;将此dll添加到bin目录后,一切正常

于 2021-08-06T16:16:26.613 回答