选择显示字体大小

java抽取word,pdf的四种武器

很多人问到如何抽取word,excel,pdf阿。这里我总结一下抽取word,pdf的
几种方法。
   1。用jacob.
    其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。
   jacob下载:http://www.matrix.org.cn/down_view.asp?id=13
    下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.file;
import com.jacob.com.*;
import com.jacob.activex.*;

public class fileextracter{

public static void main(string[] args) {

  activexcomponent app = new activexcomponent("word.application");
  string infile = "c:\\test.doc";
string tpfile = "c:\\temp.htm";
  string otfile = "c:\\temp.xml";
  boolean flag = false;
  try {
   app.setproperty("visible", new variant(false));
   object docs = app.getproperty("documents").todispatch();
   object doc = dispatch.invoke(docs,"open", dispatch.method, new object[]{infile,new variant(false), new variant(true)}, new int[1]).todispatch();
   dispatch.invoke(doc,"saveas", dispatch.method, new object[]{tpfile,new variant(8)}, new int[1]);
   variant f = new variant(false);
   dispatch.call(doc, "close", f);
   flag = true;
  } catch (exception e) {
   e.printstacktrace();
  } finally {
   app.invoke("quit", new variant[] {});
  }

}
}
    2。用apache的poi来抽取word,excel。
    poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
    下载经过封装后的poi包:http://www.matrix.org.cn/down_view.asp?id=14
    下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
   import java.io.*;
import  org.textmining.text.extraction.wordextractor;
/**
* <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 pdfextractor {
  public pdfextractor() {
  }
  public static void main(string args[]) throws exception
  {
  fileinputstream in = new fileinputstream (&quot;c:\\a.doc&quot;);
  wordextractor extractor = new wordextractor();
  string str = extractor.extracttext(in);
  system.out.println(&quot;the result length is&quot;+str.length());
   system.out.println(&quot;the result is&quot;+str);
}
}
    
   3。pdfbox-用来抽取pdf文件
   但是pdfbox对中文支持还不好,先下载pdfbox:http://www.matrix.org.cn/down_view.asp?id=12
下面是一个如何使用pdfbox抽取pdf文件的例子:
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 pdfdocument=null;
  fileinputstream is=new fileinputstream(filename);
  pdfparser parser = new pdfparser( is );
  parser.parse();
  pdfdocument = 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(&quot;the string length is&quot;+contents.length+&quot;\n&quot;);
  return ts;
}
public static void main(string args[])
{
pdfextracter pf=new pdfextracter();
pddocument pdfdocument = null;

try{
string ts=pf.gettextfrompdf(&quot;c:\\a.pdf&quot;);
system.out.println(ts);
}
catch(exception e)
  {
  e.printstacktrace();
  }
}

}

     4.抽取支持中文的pdf文件-xpdf
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。
下载xpdf函数包:http://www.matrix.org.cn/down_view.asp?id=15
同时需要下载支持中文的补丁包:http://www.matrix.org.cn/down_view.asp?id=16
按照readme放好中文的patch,就可以开始写调用本地方法的java程序了
下面是一个如何调用的例子:
import java.io.*;
/**
* <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 pdfwin {
  public pdfwin() {
  }
  public static void main(string args[]) throws exception
  {
    string path_to_xpdf=&quot;c:\\program files\\xpdf\\pdftotext.exe&quot;;
    string filename=&quot;c:\\a.pdf&quot;;
    string[] cmd = new string[] { path_to_xpdf, &quot;-enc&quot;, &quot;utf-8&quot;, &quot;-q&quot;, filename, &quot;-&quot;};
    process p = runtime.getruntime().exec(cmd);
    bufferedinputstream bis = new bufferedinputstream(p.getinputstream());
    inputstreamreader reader = new inputstreamreader(bis, &quot;utf-8&quot;);
    stringwriter out = new stringwriter();
    char [] buf = new char[10000];
    int len;
    while((len = reader.read(buf))>= 0) {
    //out.write(buf, 0, len);
    system.out.println(&quot;the length is&quot;+len);
    }
    reader.close();
    string ts=new string(buf);
    system.out.println(&quot;the str is&quot;+ts);
  }
}

如果大家谁有更好的办法,请告诉作者:  chris@matrix.org.cn


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons