Repository navigation
Expand file tree
/
Copy pathPDF.java
More file actions
77 lines (43 loc) · 1.8 KB
/
Copy pathPDF.java
File metadata and controls
77 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.edu.wang.ItextReadPDF;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class PDF {
public static final String RESULT = "F:\\readFromPDF1.txt";//存放由pdf转换成txt文件的路径。
/**
* Main method.
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args)
throws DocumentException, IOException {
PrintWriter writer = new PrintWriter(new FileOutputStream(RESULT));//txt文件写入流
String string = "F:\\1201433989.PDF";//pdf文件路径
inspect(writer,string); //调用读取方法
writer.close();
System.out.println("END");
}
/**
* Inspect a PDF file and write the info to a txt file
* @param writer Writer to a text file
* @param filename Path to the PDF file
* @throws IOException
*/
public static void inspect(PrintWriter writer, String filename)
throws IOException {
PdfReader reader = new PdfReader(filename); //读取pdf所使用的输出流
int num = reader.getNumberOfPages();//获得页数
String content = ""; //存放读取出的文档内容
System.out.println(num);
for (int i = 1; i <10; i++) {
content += PdfTextExtractor.getTextFromPage(reader, i); //读取第i页的文档内容
System.out.println(content);
}
writer.write(content);//写入文件内容
writer.flush();
}
}