【问题标题】:Extracting text from a PDF file从 PDF 文件中提取文本
【发布时间】:2023-05-26 00:12:01
【问题描述】:

我将 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。运行时,版本=0.30.0.0,文化=中性,PublicKeyToken=13235d27fcbfff58"} System.Exception {System.IO.FileNotFoundException}

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

除了使用 PDFBox 之外,还有其他选择吗?有没有其他可靠的库可以用来从 pdf 文件中提取文本。

【问题讨论】:

  • PDFBox 是一个 Java 库,您的代码看起来像 Java。 C# 涉及哪些方面?
  • TypeInitializationException 的内部异常是什么?
  • 有点困惑;你说它的 C#,但它的 Java。在 Java 中,字符串的类型是“String”,但您使用“string”
  • 内部异常状态:“'java.lang.Throwable' 的类型初始化程序引发了异常。”
  • 这是 C# 代码,我正在调用 PDFBox 库附带的 java.io 命名空间中的类。

标签: c# java .net ikvm


【解决方案1】:

您似乎缺少一些 PDFBox 库。你需要:

  • IKVM.GNU.Classpath.dll
  • PDFBox-X.X.X.dll
  • FontBox-X.X.X-dev.dll
  • IKVM.Runtime.dll

阅读此主题Read from a PDF file using C#。您可以在本主题的cmets中找到类似问题的讨论。

【讨论】:

  • 感谢您的回复萨沙。虽然我已经解决了这个问题。我现在面临另一个:“预期='/'实际='.'--1 org.pdfbox.io.PushBackInputStream@283d742”。似乎并非所有 pdf 文件都会发生这种情况,但有一些文件会发生这种情况。
  • 您的 pdf 文件似乎有问题。格式是否正确?能发个链接吗,我试试这个情况?
  • 我在这里处于 WCF 客户端/WCF 服务场景中。因此,我通过流向 WCF 服务发送文件,然后在收到文本时尝试从中提取文本。也许这就是问题所在。
  • 我不这么认为。尝试比较发送之前和之后的文件...并查看文件格式,它看起来有问题。
【解决方案2】:

我发现 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

【讨论】:

    【解决方案3】:

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

    【讨论】: