Java抽取Word,PDF的四种方法

很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。
1. 用jacob

其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。

jacob jar与dll文件下载: http://danadler.com/jacob/

下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子:

Java代码 复制代码
  1. import java.io.File;   
  2. import com.jacob.com.*;   
  3. import com.jacob.activeX.*;   
  4. /**  
  5.  * Title: pdf extraction  
  6.  * Description: email:chris@matrix.org.cn  
  7.  * Copyright: Matrix Copyright (c) 2003  
  8.  * Company: Matrix.org.cn  
  9.  * @author chris  
  10.  * @version 1.0,who use this example pls remain the declare  
  11.  */  
  12. public class FileExtracter{   
  13.  public static void main(String[] args) {   
  14.   ActiveXComponent component = new ActiveXComponent("Word.Application");   
  15.   String inFile = "c://test.doc";   
  16.   String tpFile = "c://temp.htm";   
  17.   String otFile = "c://temp.xml";   
  18.   boolean flag = false;   
  19.   try {   
  20.    component.setProperty("Visible"new Variant(false));   
  21.    Object wordacc = component.getProperty("document.").toDispatch();   
  22.    Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,   
  23.                                      new Object[]{inFile,new Variant(false), new Variant(true)},   
  24.                                      new int[1] ).toDispatch();   
  25.    Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);   
  26.    Variant f = new Variant(false);   
  27.    Dispatch.call(wordfile, "Close", f);   
  28.    flag = true;   
  29.   } catch (Exception e) {   
  30.    e.printStackTrace();   
  31.   } finally {   
  32.    component.invoke("Quit"new Variant[] {});   
  33.   }   
  34.  }   
  35. }  
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;
/**
 * Title: pdf extraction
 * Description: email:chris@matrix.org.cn
 * Copyright: Matrix Copyright (c) 2003
 * Company: Matrix.org.cn
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */
public class FileExtracter{
 public static void main(String[] args) {
  ActiveXComponent component = new ActiveXComponent("Word.Application");
  String inFile = "c://test.doc";
  String tpFile = "c://temp.htm";
  String otFile = "c://temp.xml";
  boolean flag = false;
  try {
   component.setProperty("Visible", new Variant(false));
   Object wordacc = component.getProperty("document.").toDispatch();
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
                                     new Object[]{inFile,new Variant(false), new Variant(true)},
                                     new int[1] ).toDispatch();
   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
   Variant f = new Variant(false);
   Dispatch.call(wordfile, "Close", f);
   flag = true;
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   component.invoke("Quit", new Variant[] {});
  }
 }
}


2. 用apache的poi来抽取word,excel。

poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:

下载经过封装后的poi包: http://jakarta.apache.org/poi/

下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:

Java代码 复制代码
  1. import java.io.*;   
  2. import  org.textmining.text.extraction.WordExtractor;   
  3. /**  
  4.  * <p>Title: word extraction</p>  
  5.  * <p>Description: email:chris@matrix.org.cn</p>  
  6.  * <p>Copyright: Matrix Copyright (c) 2003</p>  
  7.  * <p>Company: Matrix.org.cn</p>  
  8.  * @author chris  
  9.  * @version 1.0,who use this example pls remain the declare  
  10.  */  
  11.   
  12. public class PdfExtractor {   
  13.   public PdfExtractor() {   
  14.   }   
  15.   public static void main(String args[]) throws Exception   
  16.   {   
  17.   FileInputStream in = new FileInputStream ("c://a.doc");   
  18.   WordExtractor extractor = new WordExtractor();   
  19.   String str = extractor.extractText(in);   
  20.   System.out.println("the result length is"+str.length());   
  21.    System.out.println("the result is"+str);   
  22. }   
  23. }  
import java.io.*;
import  org.textmining.text.extraction.WordExtractor;
/**
 * <p>Title: word extraction</p>
 * <p>Description: email:chris@matrix.org.cn</p>
 * <p>Copyright: Matrix Copyright (c) 2003</p>
 * <p>Company: Matrix.org.cn</p>
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */

public class PdfExtractor {
  public PdfExtractor() {
  }
  public static void main(String args[]) throws Exception
  {
  FileInputStream in = new FileInputStream ("c://a.doc");
  WordExtractor extractor = new WordExtractor();
  String str = extractor.extractText(in);
  System.out.println("the result length is"+str.length());
   System.out.println("the result is"+str);
}
}


3. pdfbox-用来抽取pdf文件

  但是pdfbox对中文支持还不好,先下载pdfbox: http://www.pdfbox.org/

  下面是一个如何使用pdfbox抽取pdf文件的例子:

Java代码 复制代码
  1. import org.pdfbox.pdmodel.PDdocument.   
  2. import org.pdfbox.pdfparser.PDFParser;   
  3. import java.io.*;   
  4. import org.pdfbox.util.PDFTextStripper;   
  5. import java.util.Date;   
  6. /**  
  7.  * <p>Title: pdf extraction</p>  
  8.  * <p>Description: email:chris@matrix.org.cn</p>  
  9.  * <p>Copyright: Matrix Copyright (c) 2003</p>  
  10.  * <p>Company: Matrix.org.cn</p>  
  11.  * @author chris  
  12.  * @version 1.0,who use this example pls remain the declare  
  13.  */  
  14.   
  15. public class PdfExtracter{   
  16.   
  17. public PdfExtracter(){   
  18.   }   
  19. public String GetTextFromPdf(String filename) throws Exception   
  20.   {   
  21.   String temp=null;   
  22.   PDdocument.nbsppdfdocument.null;   
  23.   FileInputStream is=new FileInputStream(filename);   
  24.   PDFParser parser = new PDFParser( is );   
  25.   parser.parse();   
  26.   pdfdocument.nbsp= parser.getPDdocument.);   
  27.   ByteArrayOutputStream out = new ByteArrayOutputStream();   
  28.   OutputStreamWriter writer = new OutputStreamWriter( out );   
  29.   PDFTextStripper stripper = new PDFTextStripper();   
  30.   stripper.writeText(pdfdocument.getdocument.), writer );   
  31.   writer.close();   
  32.   byte[] contents = out.toByteArray();   
  33.   
  34.   String ts=new String(contents);   
  35.   System.out.println("the string length is"+contents.length+"/n");   
  36.   return ts;   
  37. }   
  38. public static void main(String args[])   
  39. {   
  40. PdfExtracter pf=new PdfExtracter();   
  41. PDdocument.nbsppdfdocument.nbsp= null;   
  42.   
  43. try{   
  44. String ts=pf.GetTextFromPdf("c://a.pdf");   
  45. System.out.println(ts);   
  46. }   
  47. catch(Exception e)   
  48.   {   
  49.   e.printStackTrace();   
  50.   }   
  51. }   
  52.   
  53. }  
import org.pdfbox.pdmodel.PDdocument.
import org.pdfbox.pdfparser.PDFParser;
import java.io.*;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
 * <p>Title: pdf extraction</p>
 * <p>Description: email:chris@matrix.org.cn</p>
 * <p>Copyright: Matrix Copyright (c) 2003</p>
 * <p>Company: Matrix.org.cn</p>
 * @author chris
 * @version 1.0,who use this example pls remain the declare
 */

public class PdfExtracter{

public PdfExtracter(){
  }
public String GetTextFromPdf(String filename) throws Exception
  {
  String temp=null;
  PDdocument.nbsppdfdocument.null;
  FileInputStream is=new FileInputStream(filename);
  PDFParser parser = new PDFParser( is );
  parser.parse();
  pdfdocument.nbsp= parser.getPDdocument.);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  OutputStreamWriter writer = new OutputStreamWriter( out );
  PDFTextStripper stripper = new PDFTextStripper();
  stripper.writeText(pdfdocument.getdocument.), writer );
  writer.close();
  byte[] contents = out.toByteArray();

  String ts=new String(contents);
  System.out.println("the string length is"+contents.length+"/n");
  return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDdocument.nbsppdfdocument.nbsp= null;

try{
String ts=pf.GetTextFromPdf("c://a.pdf");
System.out.println(ts);
}
catch(Exception e)
  {
  e.printStackTrace();
  }
}

}



4. 抽取支持中文的pdf文件-xpdf

  xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。

  下载xpdf函数包: http://www.foolabs.com/xpdf/

  同时需要下载支持中文的补丁包,按照readme放好中文的patch,就可以开始写调用本地方法的java程序了。

  下面是一个如何调用的例子:

Java代码 复制代码
  1. import java.io.*;   
  2. /**  
  3.  * <p>Title: pdf extraction</p>  
  4.  * <p>Description: email:chris@matrix.org.cn</p>  
  5.  * <p>Copyright: Matrix Copyright (c) 2003</p>  
  6.  * <p>Company: Matrix.org.cn</p>  
  7.  * @author chris  
  8.  * @version 1.0,who use this example pls remain the declare  
  9.  */  
  10.   
  11.   
  12. public class PdfWin {   
  13.   public PdfWin() {   
  14.   }   
  15.   public static void main(String args[]) throws Exception   
  16.   {   
  17.     String PATH_TO_XPDF="C://Program Files//xpdf//pdftotext.exe";   
  18.     String filename="c://a.pdf";   
  19.     String[] cmd = new String[] { PATH_TO_XPDF, "-enc""UTF-8""-q", filename, "-"};   
  20.     Process p = Runtime.getRuntime().exec(cmd);   
  21.     BufferedInputStream bis = new BufferedInputStream(p.getInputStream());   
  22.     InputStreamReader reader = new InputStreamReader(bis, "UTF-8");   
  23.     StringWriter out = new StringWriter();   
  24.     char [] buf = new char[10000];   
  25.     int len;   
  26.     while((len = reader.read(buf))>= 0) {   
  27.     //out.write(buf, 0, len);   
  28.     System.out.println("the length is"+len);   
  29.     }   
  30.     reader.close();   
  31.     String ts=new String(buf);   
  32.     System.out.println("the str is"+ts);   
  33.   }   
  34. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值