【发布时间】:2017-04-24 15:02:27
【问题描述】:
我有一个包含很多 pdf 的文件夹,我需要将它们全部转换为 txt 并将这些文本文件保存在另一个文件夹中。我想为此使用 java。
我有这段代码来解析一个 pdf,但它一次只能处理一个,我需要处理一个包含数千个 pdf 的文件夹。
PDFTextStripper pdfStripper = null;
PDDocument pdDoc = null;
COSDocument cosDoc = null;
File file = new File("C:/my.pdf");
try {
PDFParser parser = new PDFParser(new FileInputStream(file));
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(20);
String parsedText = pdfStripper.getText(pdDoc);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
有什么想法吗?
【问题讨论】:
-
把上面的代码放在一个循环中,迭代文件。
-
尝试使用文件夹名称和
listFiles()方法而不是一个文件名