QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4227|回复: 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>$ n. ^8 Y. W2 K3 q
<><A>1 .用jacob</A>
' B9 ], E* s- T3 L" l9 X0 m其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>
" W; V# _0 e. A  u4 [, w" H9 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>! t7 V) c' `  J" ^( c; E- Z# e
<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>) n: ?1 u4 E/ C# K! `* [' N% n
<>
2 [4 s  u) w( H# C) R: `: _7 k1 S<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">+ N! I. v6 r0 x7 j1 t( D& |
7 K- A; g; K1 x, v+ a( t
<TR>+ A5 H) I5 Z: @+ q
<TD><RE><CODE>$ U. N' ^# @# K/ ?3 `
import java.io.File;" y' n3 k, I' C2 A3 P! d% x" L
import com.jacob.com.*;  L+ O  W  {2 k3 Q# k( K
import com.jacob.activeX.*;" Y4 A% `9 `7 Z2 e. J4 M6 _
/**, ?) i( Q7 s* ?3 u( _; ]/ v0 G
* <>Title: pdf extraction</P>
! Q7 V. z7 Y3 a# ?7 f* z$ k * <>Description: email:chris@matrix.org.cn</P>
4 @  U0 v% M; f$ z" K4 B! g * <>Copyright: Matrix Copyright (c) 2003</P>) @" K" \( j# x; g- Q/ P% l
* <>Company: Matrix.org.cn</P>
! D- C( p" ^7 D% n% V8 z- \ * @author chris
. }" ]& X/ ~' \6 h2 e * @version 1.0,who use this example pls remain the declare) O1 I4 c& i- e
*/
# |8 a; D6 q2 k' Gpublic class FileExtracter{1 b4 U3 A3 M( B% Z6 q# ~
public static void main(String[] args) {! D8 ?2 ~4 b% B- s0 g* Q
  ActiveXComponent component = new ActiveXComponent("Word.Application");2 o( X, y% d1 E, L2 J5 g1 B
  String inFile = "c:\\test.doc";& Q, @, o. b5 @& E
String tpFile = "c:\\temp.htm";6 K) E: r- u5 Q9 ?# o7 y
  String otFile = "c:\\temp.xml";+ T! B9 t. t6 }! F$ V* \8 B1 x
  boolean flag = false;
% Q& h; G) q  d+ k3 S1 n+ q  try {" t* @' y% g2 i; k1 ^
   component.setProperty("Visible", new Variant(false));
; K8 k3 N8 X3 q  S- e" X   Object wordacc = component.getProperty("document.").toDispatch();: S. M3 e  R8 g3 w7 B+ n
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, ! B. _) J' h$ H! \8 ]! Z
                                     new Object[]{inFile,new Variant(false), new Variant(true)},0 d  L- o; ?& Y0 ~$ o  G% ]- Q. M
                                     new int[1] ).toDispatch();: N* M3 U, ]* R. t$ C
   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);, r% t& s; t* f# ~  p- D* ?
   Variant f = new Variant(false);
7 |% r' {0 }* K1 C( H4 q% t   Dispatch.call(wordfile, "Close", f);
: l4 ~/ Z1 `: A   flag = true;; @/ k" R# s$ n2 G
  } catch (Exception e) {" p4 ]% C0 }+ B. X! T0 A. {5 z
   e.printStackTrace();, [$ m$ J9 c2 W7 ~! L+ a6 _# [
  } finally {
- {' S3 w7 @1 J   component.invoke("Quit", new Variant[] {});
8 q4 N. ~3 g! I# s/ L  }
4 L' p8 B2 |3 h: `' F }
1 X& i& |3 S: i) u}" c0 B$ O0 o/ V: N9 X
</CODE></PRE></TD></TR></TABLE></P>  G; l; I' G1 n' k- l
<><A>2. 用apache的poi来抽取word,excel。</A>: K* d# I- m* E5 }% \; R
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
" R3 i% A) G" 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>
' h! q; G- _2 P<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>
7 b  ^. L5 y/ ^# y8 q( F2 T# ~7 s# n; y<>( e6 r: @* m. l# U& L* \
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">, R- L9 l3 u' X* J' _

1 O! q5 \3 l/ x: q, N<TR>9 |/ j7 L; A( n# p' T3 e
<TD><RE><CODE>8 x* C1 F# L0 Q& R' T
import java.io.*;
0 u) N5 {8 `" o% x; L" Oimport  org.textmining.text.extraction.WordExtractor;
" i2 c+ x6 @: ]# e/**
; v# V+ G; F0 C7 v * <>Title: word extraction</P>
5 a4 ]8 X0 h: u* v# T" _- @ * <>Description: email:chris@matrix.org.cn</P>1 N" ?  d  t2 A2 `4 C) b
* <>Copyright: Matrix Copyright (c) 2003</P>, D$ x3 P- N" v. ]' n! M" W
* <>Company: Matrix.org.cn</P>
- |: n/ _3 {! \% n" [9 q) t * @author chris
0 m+ N& ~) d9 \- E * @version 1.0,who use this example pls remain the declare0 x, T2 D7 Y4 ?3 B5 ]0 j" e! g
*/
* q% e- d3 C, g6 _
9 ]0 n% m6 |8 Ypublic class PdfExtractor {
8 _& v2 x8 s+ {; F4 w( D! u  public PdfExtractor() {& `. G8 c  w' y5 N9 }  G
  }
8 ~+ ?  q  c  a  public static void main(String args[]) throws Exception/ h2 C) I/ A" E, h
  {
! R' w/ L5 D( V) T; ~7 T  FileInputStream in = new FileInputStream ("c:\\a.doc");$ B2 D; Q. g0 y# w" h
  WordExtractor extractor = new WordExtractor();& R: A5 q; G: n1 K, f
  String str = extractor.extractText(in);5 `$ A1 j7 }6 f' V( e, t. p5 _
  System.out.println("the result length is"+str.length());" d# P- w6 Q- q0 q# e; R# O( Y4 T/ g
   System.out.println("the result is"+str);8 g6 q/ Q6 j+ u# h
}
7 J' p& _9 V& w* h; G; @}
; `4 a: o: A* `( @2 R+ r8 T</CODE></PRE></TD></TR></TABLE></P>7 l# X) Y1 g- f) x
<><A>3. pdfbox-用来抽取pdf文件</A>( Z! V  b+ J' o; t" K5 L( b
但是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>
( h8 f$ n; q7 E# p6 c% i+ J<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>
2 n% L( I1 I! G<>/ l# k8 I) ]& G7 B) h7 X
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
# `& @% t) w0 k% }/ V. z! D: r  k& H5 \
<TR>
/ f1 d, d7 n2 r* K5 Z3 U<TD><RE><CODE>4 L- {  [9 R' F
import org.pdfbox.pdmodel.PDdocument.
: S1 @; L$ d; B' z+ ~0 B' Uimport org.pdfbox.pdfparser.PDFParser;
' [# H3 T, p; M- b( @4 R. P0 [$ u8 Bimport java.io.*;+ M7 g6 e5 Y1 h/ J) p: I% w+ X, s; q
import org.pdfbox.util.PDFTextStripper;* u1 W0 L- d  k; K8 l
import java.util.Date;
# W+ o! q- Z/ R/**7 F# @* L* ~- x4 k
* <>Title: pdf extraction</P>; \- Y; y7 V- v5 i1 C9 y; D
* <>Description: email:chris@matrix.org.cn</P>* b- O6 h# _# V# e  p3 M: o% y! J
* <>Copyright: Matrix Copyright (c) 2003</P>* V( I$ f* A' E8 I  F
* <>Company: Matrix.org.cn</P>" i6 U  G% Z/ B+ ]  i+ \
* @author chris
! G. H! z: [$ E0 ~4 u, l9 { * @version 1.0,who use this example pls remain the declare
# }% `  h4 D, v' M" C# s& X */
2 p$ I7 m. |1 F1 ^# G/ ?5 @8 R; ^+ w5 l2 A9 S% \: `: U
public class PdfExtracter{
8 ^4 x$ d- x- u1 O8 e* ]/ p' t' i/ ]
public PdfExtracter(){
' w/ t* H- o# }- y7 q, E( v0 ]  }( I7 w8 Y; n+ k0 }* f
public String GetTextFromPdf(String filename) throws Exception
  T8 q& }' X  ]6 [0 [7 |- x8 K  {
! U! S/ t7 T: d2 C1 r  String temp=null;
  v6 ~" @- o8 m' x+ T" c  PDdocument.nbsppdfdocument.null;( p' a. h" B2 b" ?, j% J
  FileInputStream is=new FileInputStream(filename);+ z. j0 o/ f2 N3 K
  PDFParser parser = new PDFParser( is );9 t' b9 y  a* y+ _4 w' F
  parser.parse();
  E9 i: U, l6 ^- Y' S  pdfdocument.nbsp= parser.getPDdocument.);
) ^$ E: [- |+ }" D6 a7 F& R0 I0 a  ByteArrayOutputStream out = new ByteArrayOutputStream();
3 m* d( s# ]- Z# P, k. X$ R) S  OutputStreamWriter writer = new OutputStreamWriter( out );
2 T# `: T3 g9 O- n1 y( r  PDFTextStripper stripper = new PDFTextStripper();
( ?4 ]9 V" ^4 u3 x9 P; A! C  stripper.writeText(pdfdocument.getdocument.), writer );# s$ q2 [4 E4 I9 D3 s2 t  p1 I& l
  writer.close();
2 Z! B' [0 O4 [+ A6 a+ x9 n% E  byte[] contents = out.toByteArray();
4 h) Q- w  _0 j: }+ E$ y, ]- V' P& L' W" H  N" W0 A5 I: ^
  String ts=new String(contents);$ I. Q4 u, d% p1 n( x
  System.out.println("the string length is"+contents.length+"\n");
' q; V" s+ ~5 ~  return ts;
2 r+ e+ y5 Z/ |; ^}
8 I, r  t3 P# Cpublic static void main(String args[])
* y! _+ {" P9 E" V: {7 \5 ~5 O$ m, K3 }8 J{
3 K% i  t6 y. IPdfExtracter pf=new PdfExtracter();
3 ]3 w5 I, E3 K) jPDdocument.nbsppdfdocument.nbsp= null;
% M& C7 W5 C/ t6 K# v6 F5 B
4 o& [3 Y3 U' Z+ z* o0 Ytry{" T# x; |2 M+ e+ ~9 C
String ts=pf.GetTextFromPdf("c:\\a.pdf");/ z8 A. J) F- Z8 ^( }, Z
System.out.println(ts);
2 @. J1 g( G* z5 T}
- S- T! P! y5 i  R0 pcatch(Exception e)
7 ]3 B* e9 Z9 o( n6 M. V  s& Z- k9 U  {
$ ~6 I- G: o1 |8 @- V: X  e.printStackTrace();  x* l, T$ g! T4 |* z3 u0 l
  }
1 t9 o+ v7 @  F}
4 [6 t" T; D+ a
' M! r1 @5 P: l# R' w# ]}; E0 J, K$ U2 E$ X9 M
</CODE></PRE></TD></TR></TABLE></P>
" B- Z5 @8 H& F0 k# W& f<><A>4. 抽取支持中文的pdf文件-xpdf</A>! X2 s1 {# s0 q4 a6 d8 q
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
$ n' C4 H% r2 w* A<>下载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>: u6 f8 P" Q( y4 W( l% _4 _
<>同时需要下载支持中文的补丁包: <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>
& u" j4 T2 x# h( y5 a. F2 E3 H<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>
: P$ k5 K6 u0 t9 E. |# q<P>下面是一个如何调用的例子: </P>1 u( E+ F5 Y  O0 D) U/ @1 c
<P>
3 ~2 h, f6 t* t1 B$ r<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
+ z; a; T- k2 G. k7 b+ i' e- b" {8 m8 r4 u1 M0 o
<TR>
* R# |; F: g  ~! n, O5 x<TD><PRE><CODE>" q6 o1 H# f, O! n
import java.io.*;
9 l0 K) f. J- @4 y/**
7 N' S! c# b2 o( B, _# g  M * <P>Title: pdf extraction</P>7 B+ g, l2 {! `7 X
* <P>Description: email:chris@matrix.org.cn</P>
- p& D( @0 l, t( q, [; w/ w1 @ * <P>Copyright: Matrix Copyright (c) 2003</P>5 E' Y/ n3 m: V+ G8 Q
* <P>Company: Matrix.org.cn</P>
) H2 J' m: E* c0 n- k4 w * @author chris& e' x6 w" k; |  y8 A
* @version 1.0,who use this example pls remain the declare
) @9 I& P( i$ ]' y; j( J7 D, T* a */
% c9 i/ I& Y1 n) Y: f2 e4 K+ b) y% U; i. C
' E6 D8 u* i9 l1 P5 P: U
public class PdfWin {8 ^" h! B; ^$ i1 ~& [6 Y
  public PdfWin() {9 z. K0 y) A9 i6 U9 h/ I. c
  }1 S4 w/ i, T8 O0 `% |
  public static void main(String args[]) throws Exception
$ Q4 |6 U4 k4 v; C  {
; N8 v7 H- @7 D! }$ Q    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";" B9 ^( j/ }- H5 C8 q+ ?+ N1 a
    String filename="c:\\a.pdf";4 H! [4 I( N* Y! b# \( @' P
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
7 R8 k9 x7 U* T3 b& l$ N    Process p = Runtime.getRuntime().exec(cmd);
% e$ P* R( l, H- f1 |9 Z+ u5 ^4 U    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());. T. t3 l& c4 _: a" k- ?9 N3 @0 f: @
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");* Y, u# S6 i5 o0 R. ?
    StringWriter out = new StringWriter();
6 U/ u" s& m0 m8 m: n( d8 C/ o/ B    char [] buf = new char[10000];
# \9 R. h" N3 x* _    int len;
8 O9 o! W) e5 K' [6 O3 Y    while((len = reader.read(buf))&gt;= 0) {# j" w2 Y+ F7 {
    //out.write(buf, 0, len);
/ c8 |7 l, h0 e    System.out.println("the length is"+len);
* G- A$ W, _/ C    }
9 \5 R+ c) r% v4 x( x8 L! b! {    reader.close();: N; Q6 `% m+ f3 K: E. S# D, k
    String ts=new String(buf);7 G/ y; _+ ]4 T" m
    System.out.println("the str is"+ts);" Q, l$ L- D" h3 v+ Q" n, j9 z' c
  }; t9 m! K) Q5 M$ W
}
6 S) c2 U) \, }- U7 j! b9 l</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-6-10 21:01 , Processed in 0.422319 second(s), 55 queries .

    回顶部