【发布时间】: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 命名空间中的类。