- 浏览: 169153 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
1055229269mao:
一起学习,一起进步
servlet登陆过滤器 -
1055229269mao:
亲测好用。非常感谢了
servlet登陆过滤器 -
wx_hello:
此行:Builder builder = new BookDo ...
xml 解析例子 -
wx_hello:
我想说楼主,你那个SAX解析的不对吧。。貌似使用的dom解析出 ...
xml 解析例子 -
hxp520520:
下了源码,正研究
java ip 查询定位 QQwry.dat
很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。<!-- START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!-- END RESERVED FOR FUTURE USE INCLUDE FILES-->
其实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)}, <!-- code sample is too wide --> new int[1] ).toDispatch(); Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]); <!-- code sample is too wide --> Variant f = new Variant(false); Dispatch.call(wordfile, "Close", f); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { component.invoke("Quit", new Variant[] {}); } } } |
|
|
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对中文支持还不好,先下载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(); } } } |
|
|
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); } } |
发表评论
-
Port 80 or 443 (SSL) already in use ! Installing Apache2.2 service failed!的解决方案
2011-02-15 10:16 2897Port 80 or 443 (SSL) already in ... -
java断言 assert
2010-04-14 02:37 1451java 断言 assert 初步使用:断言开启、断言使用主要 ... -
Java调用com组件操作word使用总结
2010-03-12 16:35 2091一、准备工作 先了解一下概念,JACOB 就是 J ... -
Java操作office 2007文档
2010-03-12 16:23 1721我们希望将Office 20 ... -
java平台下通过jacob对excel,word进行打印等操作
2010-03-09 09:22 2782因为项目有个需求,要求能够在某个预定时间对特定报表进行自动打印 ... -
An internal error occurred during: "JSP Semantics Validator (JSF)". org/eclipse/
2009-11-17 11:58 2177An internal error occurred duri ... -
Comparator和Comparable比较
2009-09-22 23:40 801当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comp ... -
struts验证框架开发
2009-09-10 17:38 770一、启用Validator插件 虽然Validator框 ... -
在jsp环境中配置使用FCKEditor
2009-09-09 21:32 713FCKeditor是sourcefor ... -
配置FCKeditor(FCKeditor for java)
2009-09-09 21:28 12091.下载FCKeditor.java ... -
Java排序算法
2009-07-20 15:52 1026public class Sort { public ... -
log4j配置详
2009-07-17 14:59 671log4j详解 ... -
关于java打印功能的最简单实现的学习笔记
2009-07-16 17:03 2331<scr ... -
Java高级编程:打印--学习如何使用打印服务API
2009-07-16 16:36 1658作者:Brett Spell 翻 ... -
Tomcat 配置
2009-07-15 11:00 811Tomcat启动分析 1 - To ... -
键盘快捷键大全
2009-07-01 09:48 991、常见用法: F1 ... -
Linux必学60个命令文件处理
2009-07-01 09:47 890linux系统信息存放在文 ... -
pager-taglib使用指南【转】
2009-06-30 16:38 6882009-05-13 09:49 一简介, ... -
pager-taglib使用指南【转】
2009-06-30 16:37 6872009-05-13 09:49 一简介, ... -
Javascript实现打印网页中定义的部分内容
2009-06-26 12:12 958正常情况下的打印是使用 window.print(); 直接整 ...
相关推荐
Java抽取Word和PDF格式文件的四种武器
Java抽取Word和PDF格式文件 Java应用
本文讲解了如何利用Java语言来实现Word以及PDF的读取操作,内含原理性知识,还有源码实现。希望会对你有所帮助。
使用spring boot+Elasticsearch 7.9.1+kibana 实现对word,pdf,txt等文件的非结构化数据全文内容检索
Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这款实例会对你有所帮助。 Calendar万年历 1个目标文件 EJB 模拟银行ATM流程及操作源代码 6个目标文件,EJB来...
PDF文档解密/解除限制/提取文本图片/转Word 全能工具超好用 PDF文档解密/解除限制/提取文本图片/转Word 全能工具超好用
XBRL相比Excel、PDF、网页等财务信息格式有众多优势,原因就在于它将业务报告整体拆分为一个个元素而存在,使报告由静态变为动态,让计算机可以对报告内容进行抽取和组合。 例如,对于Word格式财务报告,尽管人们...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG(解析表达式语法)分析设施。你定义的Java源代码的语法规则,直接,没有必要专门编写和维护,外部语法文件。同时保持蒸提供全面的支持,...
java操作word,pdf的各种武器中 第一:用jacob. 其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的...