QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4202|回复: 1
打印 上一主题 下一主题

[转帖]java抽取word,pdf的四种武器

[复制链接]
字体大小: 正常 放大
god        

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:15 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>- j+ |/ i5 x+ n/ \3 P3 ?* g) q
<><A>1 .用jacob</A>
( v/ d/ G4 a. A$ J* {; D其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>* Y7 P" i$ V: b: V' C% |$ M
<>jacob jar与dll文件下载: <a href="http://www.matrix.org.cn/down_view.asp?id=13" target="_blank" >http://www.matrix.org.cn/down_view.asp?id=13</A> </P>
; S* s+ h4 S- m( L' |<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>
+ w% b$ p' W9 ^% D; b4 D<>
% M, `: I1 L0 l8 @8 Z- }% s" I$ N<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
, [1 N; r% v, ~& i" ^' V0 J* g/ |
<TR>
6 ]- W5 `6 F& `  A<TD><RE><CODE>0 _5 b3 b& C; u9 n1 y
import java.io.File;
, e8 G* g' m1 C. `7 B' q5 zimport com.jacob.com.*;
* a0 K- @* V8 i$ ]. gimport com.jacob.activeX.*;
: F/ k% Y" ], ~: O, f$ z/**
9 @  C( h, [+ [0 @( l' ?, ] * <>Title: pdf extraction</P>
: g9 N+ t! x% e( | * <>Description: email:chris@matrix.org.cn</P>
: F3 O  p4 E, L, ?5 H$ F * <>Copyright: Matrix Copyright (c) 2003</P>
4 y" G1 c% G6 Z. T& E * <>Company: Matrix.org.cn</P>
9 ^) f9 p4 K. N' j7 ?1 G3 g' z$ ^ * @author chris
6 h) B: a; I' e" ]7 b- k" U' w * @version 1.0,who use this example pls remain the declare
0 |: q/ o/ X) g9 I* l1 Y */) [. M2 w+ F2 v- r8 h- m% o
public class FileExtracter{* K5 j/ i  n! y* J8 U* f, e5 J, h
public static void main(String[] args) {
! K' b3 s  s+ B  ActiveXComponent component = new ActiveXComponent("Word.Application");$ {! p% E8 @: O4 B% Z1 I% y
  String inFile = "c:\\test.doc";& o4 D+ D* k$ ]5 ~9 {/ }
String tpFile = "c:\\temp.htm";
! y0 [. U9 [: L& D  String otFile = "c:\\temp.xml";9 [4 J3 t; Z% E
  boolean flag = false;
2 o2 a* ]5 i( k8 ~( r$ i3 K  try {7 C5 D7 J# M& J2 {
   component.setProperty("Visible", new Variant(false));
. c! u7 H7 T: q' R. i   Object wordacc = component.getProperty("document.").toDispatch();
+ X% K2 M( w3 N, G   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
3 ^1 w6 b2 N5 I! V: F# V                                     new Object[]{inFile,new Variant(false), new Variant(true)},3 J$ K( U$ h' K
                                     new int[1] ).toDispatch();
5 D/ T# _- |( L0 T+ M' B5 B* l7 U% j; o   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
6 W: N6 ^, L8 g+ N   Variant f = new Variant(false);, m) ]$ R6 F! k- T, h# |1 U2 E0 D
   Dispatch.call(wordfile, "Close", f);
& v7 G! _6 ]2 s: @& L: b   flag = true;- P4 G8 x: c6 c1 l" A
  } catch (Exception e) {
! m. j1 W: q6 S8 p  |7 S) y/ e   e.printStackTrace();' P3 v$ y5 _! I. y" I/ J& @! m
  } finally {
4 X, _  I6 ?, r- ~   component.invoke("Quit", new Variant[] {});
$ ~7 j( |) G8 i5 W  }9 P& p  g0 X; g4 I% F+ P/ E
}) {1 U% ]+ U/ @; S
}( m% Z  ^4 ?) P6 o# s
</CODE></PRE></TD></TR></TABLE></P>
' G4 I  }% N6 K& @# t  v<><A>2. 用apache的poi来抽取word,excel。</A>
+ E$ Q9 K; n- [, V& m8 zpoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>. q2 V3 n' j' W# @+ ^, T6 I$ \7 R( N
<>下载经过封装后的poi包: <a href="http://www.matrix.org.cn/down_view.asp?id=14" target="_blank" >http://www.matrix.org.cn/down_view.asp?id=14</A> </P>1 e5 K# W+ X4 y$ y$ }
<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>% e) c- K# `0 D- S& G6 w" w
<>5 k; A' C; P# ?7 A8 Z
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%"># H1 @- n! P" g

, I3 i! Y, A( w  g* ]* p; t<TR>" Z! b, C8 V0 P. E% O; p- R6 a# e$ i
<TD><RE><CODE>; n$ W5 p1 q; d* \( n
import java.io.*;: H5 F2 |$ H6 Z% ?( k' x3 X# Q
import  org.textmining.text.extraction.WordExtractor;  V" l( b+ J& W
/**8 P5 Z7 {; Y, C; o
* <>Title: word extraction</P>, @; I# @; [- ]; J
* <>Description: email:chris@matrix.org.cn</P>
$ ^" X& Z( T* p) Z9 F * <>Copyright: Matrix Copyright (c) 2003</P>6 c3 q" G6 _+ E" W$ A
* <>Company: Matrix.org.cn</P>: n" O9 T, Z* ?3 V- {5 @
* @author chris
3 T' J1 E6 L, k, T, N# S' a * @version 1.0,who use this example pls remain the declare% Z& |1 l4 f5 a5 h
*/9 H# w" f: ^. k. S! t* a3 N+ M

& M6 H/ E9 G* Z5 @public class PdfExtractor {- O1 t- }) y2 `7 w
  public PdfExtractor() {! \7 \  @4 D, V( ?  T8 p$ r7 W
  }
$ n$ [/ W' [& M  C4 u  public static void main(String args[]) throws Exception
# ]6 P- Z6 @8 w' e/ g; w, W  {
( f8 A) ?1 O0 e$ v& ?$ R1 `9 m7 D7 M; ?  FileInputStream in = new FileInputStream ("c:\\a.doc");8 C5 l2 C. l5 e
  WordExtractor extractor = new WordExtractor();
; h2 M, Z5 B7 S% }) Y8 G# c, r  String str = extractor.extractText(in);# z* P# h& y: [$ o
  System.out.println("the result length is"+str.length());
8 t8 n; y) O& J2 z4 f   System.out.println("the result is"+str);
6 V5 T; }0 d5 j$ B3 y}
( T( s: ~) W5 u' _6 H0 B}
* h) j, G8 l# t0 Z/ v</CODE></PRE></TD></TR></TABLE></P>  x( Q8 z7 q$ B8 n* K2 a
<><A>3. pdfbox-用来抽取pdf文件</A>
4 v) V& I& t: M; c% h$ \& m但是pdfbox对中文支持还不好,先下载pdfbox: <a href="http://www.matrix.org.cn/down_view.asp?id=12" target="_blank" >http://www.matrix.org.cn/down_view.asp?id=12</A> </P>
) _% x/ e" x/ |7 H. Y2 L$ w; y<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>' ^* A' x( \3 P
<>' ]! ^4 w' M1 `# s& O2 Q% \- t
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
3 e5 F+ j/ [# q8 c
( R0 V6 [& E/ A+ Z3 |( x2 \+ ]<TR>( h: y/ Y$ i4 W+ F6 ?6 r
<TD><RE><CODE>1 T1 o9 d0 k) d; X" R, ~
import org.pdfbox.pdmodel.PDdocument.+ P0 \9 X' v4 S( y# c+ ^
import org.pdfbox.pdfparser.PDFParser;9 I# Z. e) H( u  r( C6 H8 O
import java.io.*;' u% S+ R5 ^: \
import org.pdfbox.util.PDFTextStripper;0 u: d( F* W% d; w0 g1 m
import java.util.Date;
3 Y9 g, x5 A) L* f, D; E; t/**
3 H& S5 ?" l1 \. ~- m/ r * <>Title: pdf extraction</P>
2 _! a( A) h6 _  _- _- w, z * <>Description: email:chris@matrix.org.cn</P>  Y6 R  ^5 c4 t
* <>Copyright: Matrix Copyright (c) 2003</P>& r( [; l8 U' T9 _
* <>Company: Matrix.org.cn</P>) s; U; b2 V) m+ D
* @author chris/ D+ U1 r$ N  n! e  X
* @version 1.0,who use this example pls remain the declare4 X  _4 Q8 e* ~' R1 @8 z3 b
*/
; G7 O' r1 ?6 L% w, e  S& ?- J- k2 }% @' W3 V2 s5 w; g
public class PdfExtracter{2 [1 o' ?$ o8 s4 H- t! N
6 p/ E' m7 y; o" D* d/ W% C0 N9 x5 u
public PdfExtracter(){
. U6 x. o8 s! [+ i' ~  }
9 u" @/ \3 E' v. ~# q; Qpublic String GetTextFromPdf(String filename) throws Exception
8 Q3 C3 Z# L: p  i  {* Y& h4 _0 k' J+ q4 z' w( _$ C
  String temp=null;$ V- B+ q+ f+ o* N) a; C' a7 R
  PDdocument.nbsppdfdocument.null;+ R6 U- S1 v# W) \9 I  L
  FileInputStream is=new FileInputStream(filename);! q3 v# |1 ]2 F  W) u$ ~/ T
  PDFParser parser = new PDFParser( is );" I* g9 K3 t1 o, C
  parser.parse();
% E- N! |% K1 s! N' m  pdfdocument.nbsp= parser.getPDdocument.);
4 T9 l! W3 ?. P% x0 J% K5 D$ A  ByteArrayOutputStream out = new ByteArrayOutputStream();
" ~  A/ ~, a) S  OutputStreamWriter writer = new OutputStreamWriter( out );
2 J6 a* a2 `* |  M7 v  PDFTextStripper stripper = new PDFTextStripper();
& F( B4 B9 O" Q8 V9 z  stripper.writeText(pdfdocument.getdocument.), writer );
% q$ S. E& @( L0 [$ s  writer.close();4 o8 g% l5 P( L# ^, k5 C/ [
  byte[] contents = out.toByteArray();
7 f4 d' `( z& ~1 h& u  A6 [
* s; V5 o* Z+ @: c4 ?  String ts=new String(contents);  h. ~5 j# w& H
  System.out.println("the string length is"+contents.length+"\n");
' T* t% f! e4 U7 ]6 P) P* m, y$ I+ h  return ts;
' D+ y* ]# P8 A7 n% p4 P}4 |! U- k$ u7 `$ y( o, a) m7 _
public static void main(String args[])  v: _: j$ D- ~! N
{
  F% d' Q0 m$ O+ K! ZPdfExtracter pf=new PdfExtracter();
; g. v* _: k2 h0 U6 S) RPDdocument.nbsppdfdocument.nbsp= null;9 }' T" H# `7 k8 x5 b
5 e5 a' B# U/ h; a3 S2 @
try{
/ o$ b) f( B8 `2 ~String ts=pf.GetTextFromPdf("c:\\a.pdf");
& g4 s5 ?' O* o7 TSystem.out.println(ts);
3 w4 f9 W; z+ h; Y4 B: X0 T" P: ]% l}
4 J' M# x" y' A) m3 Dcatch(Exception e)
& ?; H# J  D5 {, y+ o  {$ |- y% C/ G8 |
  e.printStackTrace();% n0 B/ l: S6 n) t  k1 s
  }
) n" O% Z' }7 `1 u}' V2 s" x$ w% m+ N4 O/ _% x

9 X; [8 x5 Z% \3 n4 }' H& o}" |  R' D0 @% q6 y# F
</CODE></PRE></TD></TR></TABLE></P>
% H, [# ]+ f( z<><A>4. 抽取支持中文的pdf文件-xpdf</A>
2 {: k: f- i) T$ N- J  ]; u1 Q9 f4 M& Mxpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
  Y1 W) W- \' |! p0 p<>下载xpdf函数包: <a href="http://www.matrix.org.cn/down_view.asp?id=15" target="_blank" >http://www.matrix.org.cn/down_view.asp?id=15</A> </P>8 M$ I4 _5 Z/ N8 ^' M
<>同时需要下载支持中文的补丁包: <a href="http://www.matrix.org.cn/down_view.asp?id=16" target="_blank" >http://www.matrix.org.cn/down_view.asp?id=16</A> </P>1 {5 z9 \% q- r9 y* X( K$ W
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>0 N  t% n) J; C5 o( d
<P>下面是一个如何调用的例子: </P>. s2 ~! e. x9 [- d: y
<P>; g/ O  Q) W* ?7 u
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
; k7 P! v) t8 G
; S' o( i; ?; ^# n' P8 X<TR>
6 t1 T! N: W/ z3 \6 \<TD><PRE><CODE>4 t5 O( R# q7 t
import java.io.*;3 A0 X3 |- n3 @: g, \* w
/**, T# }4 `$ `# f+ I7 d
* <P>Title: pdf extraction</P>5 s8 W+ {0 Q" y9 [4 U* o
* <P>Description: email:chris@matrix.org.cn</P>
! j" e6 A6 f2 T2 X2 l) I * <P>Copyright: Matrix Copyright (c) 2003</P>8 m1 J4 w1 P4 J1 W& e  w+ d
* <P>Company: Matrix.org.cn</P>
2 m/ H' O! M+ u" d8 v * @author chris( O( ~# n, D: L) s' d
* @version 1.0,who use this example pls remain the declare" ?- {4 Q( U0 t5 m1 h, H3 O
*/+ ~! L4 ^5 R  a& V* x% C3 r- c

9 y; c. u1 u0 Y3 d% H4 v* g
- n1 x9 \4 a" c, t+ }6 ppublic class PdfWin {* P. N$ d& D: L+ f2 w, T
  public PdfWin() {
- h3 i, q6 i% p" G  a. R2 o4 ^+ q  }& G4 b$ H' m! r" ]
  public static void main(String args[]) throws Exception
6 D- i$ T- o* N1 Y3 _  {2 G4 n" p* U5 c, y! O+ U  V: O$ e
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
6 k# k; R% x- `    String filename="c:\\a.pdf";' C9 Q7 g0 v; O6 o! E( w; M( V/ b. P
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};, E3 @/ l* }2 l; T( K, F
    Process p = Runtime.getRuntime().exec(cmd);: j  }; a8 m3 s& ?: D; o
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());- D' k8 X0 C, C7 `1 T
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");3 t: l' O' H1 o6 V% G
    StringWriter out = new StringWriter();  q8 i8 h$ {2 t6 L4 n
    char [] buf = new char[10000];
7 p; p: y( N2 b    int len;
# W) D; e1 l/ v, n; I8 p6 l    while((len = reader.read(buf))&gt;= 0) {
; D9 k3 |" N! h, ^4 }6 k* Y5 E    //out.write(buf, 0, len);: y) k6 H5 Q8 D0 P6 H8 B$ _; o
    System.out.println("the length is"+len);
+ |! |6 C/ s% }- u  [! K) y. Z    }1 _! B2 P( [2 r
    reader.close();
$ {# r, O: [  F3 C8 ~4 N    String ts=new String(buf);' M# @2 t: k, d# s5 s6 x
    System.out.println("the str is"+ts);
4 r! I1 i' @9 @$ J  }( y* o! M) M' x3 A. R* |" U
}6 y) f2 v4 a$ q  v! Q5 o1 S
</CODE></PRE></TD></TR></TABLE></P><!-- RESOURCES--><!-- AUTHOR BIOS--><!-- Make author heading singular or plural as needed-->
zan
转播转播0 分享淘帖0 分享分享1 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
39133120 实名认证       

10

主题

4

听众

1045

积分

  • TA的每日心情
    开心
    2014-9-6 09:56
  • 签到天数: 209 天

    [LV.7]常住居民III

    自我介绍
    希望和大家交流学习!

    群组小草的客厅

    群组数学专业考研加油站

    群组数学建摸协会

    群组数学建模培训课堂2

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-4-20 22:21 , Processed in 0.432205 second(s), 55 queries .

    回顶部