tags:

views:

2790

answers:

4

I am getting problem to read pdf files using iText in java. I can read only one page but when I go to second page it gives exception.I want to read all the pages of any pdf file.

PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);

I am using these lines and at second line gives exception.

A: 

when you say one page, do you mean the first page? you might be indexing the pages incorrectly? Without any more info it could be anything.

Mark Redman
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf")); parser.getTextFromPage(3);I am using these lines. These gives exception
Sunil
What exception is it that it gives?
Steve Claridge
java.io.IOException: C:/Text.pdf not found as file or resource. at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source) at com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(Unknown Source) at com.lowagie.text.pdf.PRTokeniser.<init>(Unknown Source) at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source) at com.lowagie.text.pdf.PdfReader.<init>(Unknown Source) at test1.ReadPdf.read(ReadPdf.java:23) at test1.ReadPdf.main(ReadPdf.java:55)
Sunil
this is Method public void read(){ try{ PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf")); System.out.println(parser.getTextFromPage(3)); }catch(Exception ex) { ex.printStackTrace(); }}
Sunil
+1  A: 

Your filename is wrong. Should be c:\ and not c:/

Steve Claridge
NO sir file name is not wrong.This is correct.
Sunil
Sunil - why are you saying that, when you have posted an exception that says **C:/Text.pdf not found as file or resource**?
Andrzej Doyle
Note that you'll need to escape the backslash in Java as `new PdfReader("C:\\Text.pdf")`, to avoid errors with invalid character literals.
Andrzej Doyle
Thanks boss This is my fault.
Sunil
Sorry but this is incorrect. Java will happily convert / to \ on Windows.
mlk
+2  A: 
  1. Try changing the file location. Sometimes OS does not allow file to be read from some system drives by other applications. Put somewhere in D: etc. I face this problem in Vista when reading files from desktop.

  2. I in fact ran the same two lines of code on one of my PDF and it did print the text. Also make sure you have sufficient pages in the PDF. (3 pages or more) or try with parser.getTextFromPage(1) etc. to get content from other pages.

Kushal Paudyal
Thanks boss This is my fault. Thanks once again.
Sunil
A: 

Are you re-constructing the parser and reader for each operation? You can do that, but it's not very efficient (there is a lot of overhead with creating a new PdfReader).

Kevin Day