xpdf读取pdf文件并根据pdf内容修改文件名称
2014-05-15 22:26
435 查看
package com.sunlei; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import javax.crypto.spec.IvParameterSpec; public class Rename { /** * @param args * @throws IOException */ public static void main(String[] args) { // TODO Auto-generated method stub // System.out.println("hello\n"); File file = new File("D:\\pdf");//pdf文件夹 String xpdfPath = "D:\\TDDOWNLOAD\\xpdfbin-win-3.03\\bin32\\pdfinfo.exe "; //pdfinfo.exe文件夹,注意这个exe和后面的pdf文件名有空格,所以这里有空格 File[] fileListFiles = file.listFiles();// 取出文件夹下所有的文件 for (int i = 0; i < fileListFiles.length; i++) { String cmd = xpdfPath + fileListFiles[i].getAbsolutePath(); try { Process process = Runtime.getRuntime().exec(cmd); BufferedReader br = new BufferedReader(new InputStreamReader( process.getInputStream()));//获得exe执行程序返回结果 String firstLine = br.readLine();//只需要读取第一行就行,只要标题 // System.out.println(firstLine); // System.out.println(firstLine.indexOf('D')); //下面substring的时候为什么是16,是通过这个实验出来的 if (firstLine != null) { String subTitle = firstLine.substring(16); if (!subTitle.equals("")) { subTitle = subTitle.replace(':', ' ');// 去掉文件名不合规范的 subTitle = subTitle.replace('*', ' '); subTitle = subTitle.replace('/', ' '); subTitle = subTitle.replace('?', ' '); String title = subTitle + ".pdf";//加上后缀名 File newFile; if (title != "untitled.pdf" && title != ".pdf") { newFile = new File("D:/pdf/" + title); // System.out.println(title); if (fileListFiles[i].renameTo(newFile)) {//修改文件名 System.out.println(fileListFiles[i].getName() + "修改成功"); } else { System.out.println(fileListFiles[i].getName() + "修改失败"); } } br.close();//别忘了关闭流 process.destroy(); }else { System.out.println(fileListFiles[i].getName() + "因为文中没有文件title而修改失败"); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
1:准备工作
上网查资料,C++读取pdf库,java读取pdf库,最后找到了xpdf库,还好,下载地址【下载xpdf地址】,我下载的是windows版的,然后按照步骤实验了一下
实验,打开压缩包,读了读readme,然后进入bin32文件夹,里面好多exe可执行文件,好吧,开始搞起
拷贝一个pdf文件进去,然后cmd命令行进入bin32文件夹,
[html] view plaincopy
pdftotext.exe 5026a001.pdf
然后果然生成了一个5026a001.txt,打开一看,哇,完美转换,看来不需要配置什么东西就能执行。
开始写程序,java代码的,开始的想法是通过pdftotext转成txt,然后解析txt文档,后来一看还有一个可执行程序pdfinfo.exe,感觉这个是读取pdf文档信息的程序。
命令行
[html] view plaincopy
pdfinfo.exe 5026a001.pdf
然后在屏幕上完美输出,说明他的标题直接可以拿到,那太好了,verygood
相关文章推荐
- xpdf读取pdf文件并根据pdf内容修改文件名称
- 使用xpdf读取PDF文件内容
- java读取pdf文件内容
- PHP 读取和修改大文件的某行内容的代码
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- C#生成PDF文档,读取TXT文件内容
- PHP读取PDF内容配合Xpdf的使用
- java读取pdf文件内容
- java读取pdf文件内容
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- 利用pdfbox读取pdf文件内容和图片
- java读取pdf文件内容
- java读取pdf文件内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 读取pdf文件内容
- java读取pdf文件内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- 读取扫描版的Pdf文件 如何修改扫描版的pdf文件
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例