温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

// 读取PDF的内容
 public String getPdfContent(String filePath) {
  
  // 设置pdftotext所在的路径
  String excute = "E:\\JAR\\xpdf\\xpdf-3.02pl4-win32\\pdftotext.exe";
  //构造命令行里面的命令
  String[] cmd = new String[] { excute, "-enc", "UTF-8", "-q", filePath,"-" };
  Process p = null;
  try {
   // 调用本地命令,类似于在cmd里面敲上述命令
   p = Runtime.getRuntime().exec(cmd);
  } catch (IOException e) {
   e.printStackTrace();
  }
  //封装成字符流
  BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
  InputStreamReader reader = null;

  try {
   reader = new InputStreamReader(bis, "UTF-8");
  } catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
  }

  StringBuffer sb = new StringBuffer();
  BufferedReader br = new BufferedReader(reader);
  String line;
  
  try {
   line = br.readLine();
   sb = new StringBuffer();
   while (line != null) {
    sb.append(line);
    sb.append(" ");
    line = br.readLine();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  return sb.toString();
 }

posted on 2010-05-31 09:10 雪山飞鹄 阅读(3321) 评论(5)  编辑  收藏 所属分类: javase

Feedback

# re: pdf转换器 2010-05-31 10:20 沙漠绿洲
有问题。
1. 把exception给catch起来,会导致空置,下面就是nullpoint exception了
2. 如果PDF的内容是一张图片,这里会有很大的问题  回复  更多评论
  

# re: pdf转换器 2010-05-31 11:07 雪山飞鹄
@沙漠绿洲
这只摘录了我项目里的一部分代码,做备忘用的,你需要将参数什么的配置齐全,那个filePath是pdf文档的路径,含pdf文档名(.pdf)
如果pdf里面有图片的话你需要用字节流读取,而不是字符流啊,我这块是将pdf中的文字转成文本啊,图片肯定是不行的,你见过txt文档里面存放图片么?  回复  更多评论
  

# re: 使用xpdf将pdf文件转换为文本 2010-06-06 23:11 追梦少年
不错啊!!!  回复  更多评论
  

# re: 使用xpdf将pdf文件转换为文本 2010-07-01 23:05 隔叶黄莺-2
pdfbox 在这方面应该要好点,iText抽取pdf中的文本比较笨拙。Lucene 就是用的 pdfbox.  回复  更多评论
  

# re: 使用xpdf将pdf文件转换为文本 2010-07-02 08:47 雪山飞鹄
@隔叶黄莺-2
pdfbox?完了抽时间研究下  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: