转自:https://www.ibm.com/developerworks/cn/java/l-java-tips/     感谢作者发布的文章

用 jacob

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

jacob jar 与 dll 文件下载: 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.*;
/**
* 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[] {});
}
}
}

用 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: 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);
}
}

  

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 . 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();
}
}
}

抽取支持中文的 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="C:\\Program Files\\xpdf\\pdftotext.exe";
String filename="c:\\a.pdf";
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
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("the length is"+len);
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
}

最新文章

  1. 【javaweb学习】解析XML
  2. REST WCF Service中的WebMessageBodyStyle
  3. Spring基础—— 在 Spring Config 中使用外部属性文件
  4. c#调用Aspose.Word组件操作word 插入文字/图片/表格 书签替换套打
  5. Getopt::Long 模块的简单使用
  6. Gradle 编译时选择不同的 google-services.json
  7. HDU 1720 A+B Coming
  8. R语言︱SNA-社会关系网络 R语言实现专题(基础篇)(一)
  9. 20162330 实验三 《敏捷开发与XP实践》 实验报告
  10. 前端笔记之ES678&amp;Webpack&amp;Babel(中)对象|字符串|数组的扩展&amp;函数新特性&amp;类
  11. [ gczdac ] HDU1000
  12. ALTER添加列后,立即UPDATE该列会报错
  13. 官网下载旧版本jdk,老版本jdk,jdk1.7,jdk1.8
  14. oracle入坑日记&lt;五&gt;数据表
  15. 《软件测试自动化之道》读书笔记 之 基于Windows的UI测试
  16. sql server得到某个数据库的所有表和所有字段
  17. microcks 微服务mocks 工具&amp;&amp;运行时
  18. [UE4]获得特定类型的所有Actor:Get All Actors Of Class、Get All Actors with Interface、Get All Actors with Tag
  19. 安卓逆向(一)--Smali基础
  20. PHP 调用ffmpeg

热门文章

  1. GPS坐标转大地坐标
  2. SQL语句复习【专题一】
  3. deep_learning_Function_Sklearn_Mode
  4. zabbix 邮件报警(五)
  5. Ubuntu 18.04 手动升级内核
  6. html标签被div嵌套页面字体变大的解决办法
  7. windows安装PostgreSQL
  8. MySQL中主键的选择与磁盘性能
  9. JS 实现复制一个或多个内容到剪贴板
  10. k8s 1.16.3 yaml声明变化