4

我正在尝试使用 pdfbox 从 PDF 文件中提取文本,但不是作为命令行工具,而是在我的 Java 应用程序中。我正在使用 jsoup 下载 pdf。

res = Jsoup
.connect(host+action)
.ignoreContentType(true)
.data(data)
.cookies(cookies)
.method(Method.POST)
.timeout(20*1000)
.execute();

// prepare document
InputStream is = new ByteArrayInputStream(res.bodyAsBytes()); 
PDDocument pdf = new PDDocument();
pdf.load(is,true);

// extract text
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(pdf);

// print extracted text
System.out.println(text);

此代码仅打印空行。当我这样做时:

System.out.println(res.body());

它打印pdf文件以输出如下:

%PDF-1.4
%����
6 0 obj
<<
/Filter /FlateDecode
/Length 1869
>>
stream
x��X�n��

...

<<
/Size 28
/Info 27 0 R
/Root 26 0 R
>>
startxref
20632
%%EOF

所以我确信正确下载的pdf - 只是这个PDF剥离器不起作用......

- - - - - - - - - - - - - - - - - - - - - - - 编辑

这个问题解决了 - 工作代码在这里http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/

4

1 回答 1

2

(评论中回答的问题。请参阅没有答案的问题,但问题已在评论中解决(或在聊天中扩展)

@WeloSefer 写道:

也许可以帮助您入门...我从未使用过 jsoup 或 pdfbox,所以我没有任何帮助,但我肯定会尝试 pdfbox,因为我一直在测试 itextpdf 阅读器以提取文本。

OP写道:

谢谢,这就是我一直在寻找的 - 它现在可以工作了:) 这个问题已经解决了 - 工作代码在这里http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/

于 2015-01-26T14:21:57.230 回答