使用pdfbox从PDF文件中提取文本 [英] Extracting text from PDF file using pdfbox
问题描述
我正在尝试使用pdfbox从PDF文件中提取文本,但不是将其作为命令行工具,而是在我的Java应用程序中.我正在使用jsoup下载pdf.
I am trying to extract text from PDF file using pdfbox but not as a command line tool but inside my Java app. I am downloading pdf using jsoup.
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);
此代码仅打印空行.当我这样做时:
This code prints just empty line. When I do this:
System.out.println(res.body());
它打印pdf文件以这样输出:
it prints the pdf file to output like this:
%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剥离器不起作用...
So I am sure that pdf in downloaded correctly - just this PDF stripper doesnt work...
----------------------------------------------编辑
---------------------------------------------- edit
此问题已解决-此处的工作代码为 http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
this problem is solved - working code is here http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
推荐答案
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
@WeloSefer写道:
@WeloSefer wrote:
也许这可以帮助您您可以开始使用...我从未使用过jsoup或pdfbox,所以我没有帮助,但是我肯定会尝试pdfbox,因为我一直在测试itextpdf阅读器来提取文本.
maybe this 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写道:
谢谢,这就是我一直在寻找的东西-现在可以使用了:) 此问题已解决-工作代码在这里 http: //thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
Thanks, that is what I was looking for - it works now :) this problem is solved - working code is here http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
这篇关于使用pdfbox从PDF文件中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!