4

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());

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

So I am sure that pdf in downloaded correctly - just this PDF stripper doesnt work...

---------------------------------------------- edit

this problem is solved - working code is here http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/

divanov
  • 6,173
  • 3
  • 32
  • 51
user606521
  • 14,486
  • 30
  • 113
  • 204
  • 1
    maybe [this](http://thottingal.in/blog/2009/06/24/pdfbox-extract-text-from-pdf/) 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. – WeloSefer Jan 16 '13 at 09:19
  • 4
    Great. If you can answer your own question for later use, it will be great .. – WeloSefer Jan 16 '13 at 09:35
  • 3
    Provide the answer as an actual answer, and mark the question as answered, so the question will appear to a have a correct answer to other users coming from an unanswered questions search page. – Richard Krajunus Feb 20 '14 at 23:03
  • Can't add much more than a ditto. An unanswered question is like a water spring that was never discovered. – James P. Jul 11 '14 at 23:44
  • itext have a new licence you can not use it if it is not an open source project... or buy a licence... but you can use PDFBOX (and you can index file to with PDFBOX lucene) – cyril Oct 15 '14 at 08:20

1 Answers1

2

(Question answered in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )

@WeloSefer wrote:

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.

The OP wrote:

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/

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129