java 使用pdfbox从PDF文件中提取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14354427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting text from PDF file using pdfbox
提问by user606521
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.
我正在尝试使用 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);
This code prints just empty line. When I do this:
此代码仅打印空行。当我这样做时:
System.out.println(res.body());
it prints the pdf file to output like this:
它打印 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
So I am sure that pdf in downloaded correctly - just this PDF stripper doesnt work...
所以我确信 pdf 下载正确 - 只是这个 PDF 剥离器不起作用......
---------------------------------------------- edit
- - - - - - - - - - - - - - - - - - - - - - - 编辑
this problem is solved - working code is here http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
这个问题解决了 - 工作代码在这里http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/
回答by u4370109
(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat))
(在评论中回答的问题。请参阅没有答案的问题,但问题已在评论中解决(或在聊天中扩展))
@WeloSefer wrote:
@WeloSefer 写道:
maybe thiscan 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.
也许这可以帮助您入门...我从未使用过 jsoup 或 pdfbox,所以我没有帮助,但我肯定会尝试使用 pdfbox,因为我一直在测试 itextpdf 阅读器以提取文本。
The OP wrote:
OP写道:
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/
谢谢,这就是我一直在寻找的 - 现在可以工作了 :) 这个问题已经解决了 - 工作代码在这里http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/