我在使用iText阅读PDF文件时遇到问题。我只能阅读一个页面,但是当我转到第二页时,它会给出异常。我想阅读任何pdf文件的所有页面。使用iText阅读pdf
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);
我正在使用这些行,并在第二行给出异常。
我在使用iText阅读PDF文件时遇到问题。我只能阅读一个页面,但是当我转到第二页时,它会给出异常。我想阅读任何pdf文件的所有页面。使用iText阅读pdf
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
parser.getTextFromPage(3);
我正在使用这些行,并在第二行给出异常。
尝试更改文件位置。有时OS不允许其他应用程序从某些系统驱动器读取文件。放在D的某处:等我在桌面上读取文件时遇到Vista中的这个问题。
我实际上在我的一个PDF上运行了相同的两行代码,它确实打印了文本。另外请确保您在PDF中有足够的页面。 (3页或更多)或尝试用parser.getTextFromPage(1)等从其他页面获取内容。
谢谢老板这是我的错。再次感谢。 – Sunil 2009-10-28 15:42:06
当你说一个页面,你的意思是第一页?您可能会错误地将页面编入索引?没有更多的信息,它可能是任何东西。
PdfTextExtractor parser = new PdfTextExtractor(new PdfReader(“C:/Text.pdf”)); parser.getTextFromPage(3); 我正在使用这些行。这些给出了例外 – Sunil 2009-10-28 14:23:33
它给出了什么例外? – 2009-10-28 14:28:20
java.io.IOException:未找到C:/Text.pdf作为文件或资源。 at com.lowagie.text.pdf.RandomAccessFileOrArray。
您是否正在为每个操作重新构建解析器和阅读器?你可以这样做,但效率不高(创建一个新的PdfReader有很多开销)。
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
/**
* This class is used to read an existing
* pdf file using iText jar.
* @author javawithease
*/
public class PDFReadExample {
public static void main(String args[]){
try {
//Create PdfReader instance.
PdfReader pdfReader = new PdfReader("D:\\testFile.pdf");
//Get the number of pages in pdf.
int pages = pdfReader.getNumberOfPages();
//Iterate the pdf through pages.
for(int i=1; i<=pages; i++) {
//Extract the page content using PdfTextExtractor.
String pageContent =
PdfTextExtractor.getTextFromPage(pdfReader, i);
//Print the page content on console.
System.out.println("Content on Page "
+ i + ": " + pageContent);
}
//Close the PdfReader.
pdfReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
请列出你得到的例外,因为没有这个没有任何信息可以继续下去。 – 2009-10-28 14:16:35