我正在尝试使用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/