Sim*_*mon 3 java pdf text pdfbox
我有一个 pdf 文件的字节数组,想要从文件中获取文本。我的下面的代码可以工作,但我需要先创建一个实际的文件。你知道更好的方法吗,这样我就不必先创建这个文件了?
try {
File temp = File.createTempFile("temp-pdf", ".tmp");
OutputStream out = new FileOutputStream(temp);
out.write(Base64.decodeBase64(testObject.getPdfAsDoc().getContent()));
out.close();
PDDocument document = PDDocument.load(temp);
PDFTextStripper pdfStripper = new PDFTextStripper();
String text = pdfStripper.getText(document);
log.info(text);
} catch(IOException e){
}
Run Code Online (Sandbox Code Playgroud)
答案取决于您使用的 PDFBox 版本。
每当你有一个byte[](你似乎从 获得一个Base64.decodeBase64),你可以直接加载它:
byte[] documentBytes = Base64.decodeBase64(testObject.getPdfAsDoc().getContent());
PDDocument document = PDDocument.load(documentBytes);
Run Code Online (Sandbox Code Playgroud)
每当你有一个 时byte[],你可以通过 a 加载它ByteArrayInputStream:
byte[] documentBytes = Base64.decodeBase64(testObject.getPdfAsDoc().getContent());
InputStream documentStream = new ByteArrayInputStream(documentBytes);
PDDocument document = PDDocument.load(documentStream);
Run Code Online (Sandbox Code Playgroud)
顺便说一句:使用 PDFBox 1.8.x 时,您应该使用loadNonSeq重载,而不是使用重载,load因为load它不会按照指定的方式加载 PDF,因此可能会被欺骗而读取错误的内容。不过,如果 PDF 损坏,您仍然可以尝试load作为后备。
| 归档时间: |
|
| 查看次数: |
6982 次 |
| 最近记录: |