我试图使用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剥离器不起作用……
———————————————-编辑
@WeloSefer写道:
maybe 07001 can help you get started … I have never worked with jsoup nor pdfbox so I am no help but I sure will try pdfbox since I’ve been testing itextpdf reader for extracting texts.
OP写道:
Thanks, that is what I was looking for – it works now 🙂
this problem is solved – working code is here 07002