我正在尝试将pdf文件的提取文本写入文本文档,但是目前,虽然在eclipse的控制台中输出了6页,但只有第一页正在文本文档中写入。
我该如何解决?
我感谢任何帮助。
try {
PdfReader reader = new PdfReader("D:\\hl_sv\\L09MF.pdf");
int pagenumber = reader.getNumberOfPages();
for (int i = 1; i <= pagenumber; i++) {
System.out.println("==================PAGE NUMBER " + i
+ "===================");
String line = PdfTextExtractor.getTextFromPage(reader, i);
PrintWriter out = new PrintWriter("D:\\hl_sv\\L09MF.txt");
for (String s : line.split("\n")) {
out.println(s);
}
out.close();
System.out.println(line);
}
答案 0 :(得分:4)
尝试在主PrintWriter
循环之外移动for
的实例化和关闭:
try {
PdfReader reader = new PdfReader("D:\\hl_sv\\L09MF.pdf");
int pagenumber = reader.getNumberOfPages();
PrintWriter out = new PrintWriter("D:\\hl_sv\\L09MF.txt");
for (int i = 1; i <= pagenumber; i++) {
System.out.println("==================PAGE NUMBER " + i
+ "===================");
String line = PdfTextExtractor.getTextFromPage(reader, i);
for (String s : line.split("\n")) {
out.println(s);
}
System.out.println(line);
}
out.close();
我不确定是否会这样做,但它可能会有所帮助,因为PrintWriter
会一直打开和关闭每一页。
答案 1 :(得分:0)
尝试将输出变量用于循环,也许有帮助
try {
PdfReader reader = new PdfReader("D:\\hl_sv\\L09MF.pdf");
PrintWriter out = new PrintWriter("D:\\hl_sv\\L09MF.txt");
int pagenumber = reader.getNumberOfPages();
for (int i = 1; i <= pagenumber; i++) {
System.out.println("==================PAGE NUMBER " + i
+ "===================");
String line = PdfTextExtractor.getTextFromPage(reader, i);
for (String s : line.split("\n")) {
out.println(s);
}
System.out.println(line);
}
out.close();