数学建模社区-数学中国

标题: [转帖]java抽取word,pdf的四种武器 [打印本页]

作者: god    时间: 2005-3-31 01:15
标题: [转帖]java抽取word,pdf的四种武器
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>/ U5 L. E+ o0 @# I7 N
<><A>1 .用jacob</A>1 ]& c; Q( G+ a* K+ B5 y- v
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P># a# r3 d1 Z( k4 u5 J# L" k5 n2 b
<>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>
  G, a; e5 j4 B  F( K, ]. c3 c6 {! Q<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>
# k6 e+ |2 e' b5 S<>. u+ ?: c; S7 W; o0 r% E' R. x
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">9 ^+ I' ]6 t; k
& c' K& _- b" R4 S
<TR>% H1 Y' D6 p3 b! I8 a
<TD><RE><CODE>6 v: P$ n5 e0 s$ A  `  i7 v
import java.io.File;( t8 x4 I! a) b3 N
import com.jacob.com.*;
5 c' t0 U+ Q, t; P2 p% bimport com.jacob.activeX.*;
2 j- `# h5 ^( w: ~/**- o* x  ]* p+ \0 M  V3 J% Z3 p& ^" \
* <>Title: pdf extraction</P>
+ X; \/ d5 g4 j- z  R1 u * <>Description: email:chris@matrix.org.cn</P>
3 c7 Y; R. z( p- M8 x * <>Copyright: Matrix Copyright (c) 2003</P>
% S: z8 P1 q4 j2 x * <>Company: Matrix.org.cn</P>
" H: q" l0 H; G6 _ * @author chris
" f4 N+ O: h3 [/ B * @version 1.0,who use this example pls remain the declare
8 }( c0 h( s/ a. X( V8 X */# N& a3 c5 t, }! @* a9 U8 N
public class FileExtracter{' N4 p3 i0 Q  r+ q" {* M
public static void main(String[] args) {3 R2 O( `% {7 {( }3 g% J/ {. y
  ActiveXComponent component = new ActiveXComponent("Word.Application");+ G1 x' ]- \: f: g7 z. f. ?" M
  String inFile = "c:\\test.doc";
% D- B3 ^0 x2 s: z" h0 v0 U String tpFile = "c:\\temp.htm";
" g/ m7 w' x1 k' s- K  String otFile = "c:\\temp.xml";& N- E" t- Q3 d; c
  boolean flag = false;( n' @  y( {+ q* M6 l1 s( G
  try {" q4 h2 ~+ a. m. ?+ d% [, d
   component.setProperty("Visible", new Variant(false));
: ?% n: K+ r9 z9 X4 Q   Object wordacc = component.getProperty("document.").toDispatch();: A9 g9 e7 j. `3 t- w
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, 4 Q2 i( z8 w- h0 y
                                     new Object[]{inFile,new Variant(false), new Variant(true)},. y! R8 R$ ^% ]  n$ J$ F
                                     new int[1] ).toDispatch();; k) v) [- r# w6 ]/ O
   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);' }" @: g9 g6 `3 m# ~" P
   Variant f = new Variant(false);
8 x% z7 a# c+ [  Y2 D' a# _2 o: H   Dispatch.call(wordfile, "Close", f);; q1 ?' n6 o" u. \! f, t
   flag = true;2 o: ?/ l5 }2 s, B9 J' v
  } catch (Exception e) {4 }0 g0 x4 \! C; ]1 d3 C3 w  L
   e.printStackTrace();
8 F. v3 a4 ]0 l1 J  } finally {
* d; F/ i0 L: `7 {" ]   component.invoke("Quit", new Variant[] {});8 ~9 r$ [2 H5 n
  }/ d, f9 b& P; r6 a4 [3 a, f
}- M2 w+ U5 N5 f1 H& z9 L; K
}
6 Q3 Y- E% |9 p! v0 p</CODE></PRE></TD></TR></TABLE></P># D% |) ^# U6 _; Z9 W' C/ ^
<><A>2. 用apache的poi来抽取word,excel。</A>
/ ?+ b  Z7 x* S0 R1 Lpoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
6 U7 `7 `  K' J, u' d! K<>下载经过封装后的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>
; I- o5 `: C5 x) ~& U<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>, B5 Y; n( N5 Z5 D4 c$ [  h
<>4 ~8 F4 s$ N# N! p& h; b: j9 t
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">! u+ W2 Z& ^% c/ L! L, W( l) z# _0 Q
; c2 Q* V* Q5 S- @$ v
<TR>0 ~' B7 o  S8 @' Q
<TD><RE><CODE>7 k/ F& ^! H4 W* [; R( D
import java.io.*;3 Z. ]2 G' o# K; ~" Y8 t/ l
import  org.textmining.text.extraction.WordExtractor;. u4 W$ a: d8 |4 l
/**
6 x: n0 r% ~; W# E% H * <>Title: word extraction</P>
9 d( l8 B( R% R/ I& s- D! O * <>Description: email:chris@matrix.org.cn</P>
3 \0 c, \- K# o6 | * <>Copyright: Matrix Copyright (c) 2003</P>
0 V0 n' O) S8 r% y1 U * <>Company: Matrix.org.cn</P>; `+ L  F2 C3 C4 r; ^' E9 g5 _
* @author chris( l6 [3 Y& r, T/ K) P9 T. i
* @version 1.0,who use this example pls remain the declare3 d8 d( N. y! Y& @& J
*/; [2 Q' F' O( z# g: e4 e7 u

- a0 s- S; _8 {4 P6 B; Opublic class PdfExtractor {4 U3 @5 F5 s8 A* z+ i+ A
  public PdfExtractor() {
: n- m, a0 Y3 F4 {  }- Z: z" y: B9 o% R) e# d
  public static void main(String args[]) throws Exception6 w0 g- ~. m% L3 j  l
  {& @: u( M2 l8 X: G% P' n; P
  FileInputStream in = new FileInputStream ("c:\\a.doc");
/ }1 V7 T. ]8 }) N% x2 {  WordExtractor extractor = new WordExtractor();# z# G( N1 Y7 z6 ?( X
  String str = extractor.extractText(in);
1 G2 j, g6 g; Y' e) Y  System.out.println("the result length is"+str.length());
/ f) k8 s& {2 w0 N' o   System.out.println("the result is"+str);
9 P& A2 n7 d5 {* C6 ]' d& Y}0 F$ V* r2 K' A. U4 Y1 g# r
}4 k: H: g# |; {! T5 W
</CODE></PRE></TD></TR></TABLE></P>9 u/ R; h. Z& A* p
<><A>3. pdfbox-用来抽取pdf文件</A>- G2 y& \) u/ t, I, L! q, `
但是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>+ g( u  o+ |2 a3 }7 a
<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>
. r' H9 b; a) B3 N8 O3 z<>
" @4 t8 \' T' d% S4 m9 D<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
3 e8 O2 @! M2 t$ e$ q. A& X4 |2 f2 k1 Q% R
<TR>
6 U- m$ Z" `8 a. }<TD><RE><CODE>
. j) Z) p6 m. J# J' Q4 Yimport org.pdfbox.pdmodel.PDdocument.6 T# {0 X  Q6 M/ i* n9 m
import org.pdfbox.pdfparser.PDFParser;( P# Z2 s/ I! F2 D
import java.io.*;
5 ^! t* }' [, i0 m' Z* K% Y( a% K( ?import org.pdfbox.util.PDFTextStripper;( I* s0 I" i* a* g- F% z
import java.util.Date;
; R/ u: f9 _; D4 @/**
$ z+ A( j4 y: h  C * <>Title: pdf extraction</P>
1 j/ y7 |) X/ P/ F * <>Description: email:chris@matrix.org.cn</P>/ K! h4 W4 q/ K' }+ n# S
* <>Copyright: Matrix Copyright (c) 2003</P>
) h- X1 R0 ^  }/ C * <>Company: Matrix.org.cn</P>1 a* g6 D- S7 U2 n
* @author chris
3 c' H1 F* W- e, O  A# r  X+ } * @version 1.0,who use this example pls remain the declare
3 |( J. B+ }: ?( y  {  {; D */
4 [  g8 T" A6 ]- o/ S
! e  E/ z: B) I& g0 h; a, qpublic class PdfExtracter{& B' `) y0 |) h) R/ c, ~! I
+ t& q2 ^) O: I2 j# I6 f) C7 J
public PdfExtracter(){' R8 `! e* O' {1 f$ V' C9 z5 ]0 M
  }
$ h' h- p8 v4 W! ^  g* z8 c" Ppublic String GetTextFromPdf(String filename) throws Exception7 W9 P- @. C& ^7 A
  {* P4 E2 P' M! \2 ~0 X1 S0 Q
  String temp=null;6 M6 e5 Q3 P) H4 q. ?
  PDdocument.nbsppdfdocument.null;
) n% G# }3 I; ]$ M9 M) z9 A  FileInputStream is=new FileInputStream(filename);7 z: W$ x/ p8 ^8 V- ^% O6 s7 E
  PDFParser parser = new PDFParser( is );
+ o- ~6 d2 i* Y8 Q  parser.parse();
( m- u8 c0 W2 O6 q5 P9 D  M  pdfdocument.nbsp= parser.getPDdocument.);
( P6 O7 K# o1 }; B4 h) i+ \  ByteArrayOutputStream out = new ByteArrayOutputStream();
& n, y8 V" U, v" Y- N6 O) @5 ~  OutputStreamWriter writer = new OutputStreamWriter( out );+ ~6 G) P( T8 \" b) t, a
  PDFTextStripper stripper = new PDFTextStripper();
5 u9 h) }, ^" R; W; |  N" j  stripper.writeText(pdfdocument.getdocument.), writer );
9 Z7 O  \( @) r  writer.close();0 m4 v2 Y0 S( K2 A
  byte[] contents = out.toByteArray();% j: B4 i0 f- m( j9 P! s

4 E7 n& p  }' D1 P) f' l  String ts=new String(contents);
$ P# L- T. P7 a8 v7 ~% E, [8 O6 ~  System.out.println("the string length is"+contents.length+"\n");
" z  Q  u# S9 `* X& O( ?& T# G- j  return ts;
! c0 P8 i% T( W) O" V' f}) v9 @4 L/ l  i7 l( x: f
public static void main(String args[])- h1 Z" [, y+ m
{: p7 H* W" X# @; _& H
PdfExtracter pf=new PdfExtracter();
' v) k9 c8 B* b" [PDdocument.nbsppdfdocument.nbsp= null;
& C8 Q% d' I( P& r1 e! b; o4 b( ?* y$ b$ {& V
try{
4 ~3 F: y. W" e2 D" nString ts=pf.GetTextFromPdf("c:\\a.pdf");
: J7 a8 u* d% j3 KSystem.out.println(ts);
2 s8 u( H0 g& ]" ]1 @}
# [& x) S+ H5 i9 s; B) K  Y1 ]catch(Exception e)
1 n- u6 {. s1 X( N! J/ g, b0 O) F1 ^% {  {
) W6 B. p; b; _) v  e.printStackTrace();
  a+ b1 c" H; T  }
& A# p; R" R; V8 q" `, b5 p  d}0 \" O. }$ m. f; L
& o" P+ Q/ K; T, F5 z3 w
}
- ~9 f5 c" q3 l' o% l& P6 T</CODE></PRE></TD></TR></TABLE></P>8 |1 a! X' y+ X, {' x
<><A>4. 抽取支持中文的pdf文件-xpdf</A>
8 `5 {  D& p* G' e# ~; W6 A3 Pxpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>2 R; `( U+ s+ y
<>下载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>9 T  h! L. m  I0 K
<>同时需要下载支持中文的补丁包: <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>" F' Z0 F; E, w& |. o( a1 J' x1 \
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>$ O& Q2 F2 m- \4 S+ s1 h
<P>下面是一个如何调用的例子: </P>. C1 o8 |! O1 }0 i) ?! i# k
<P>0 }' A' q( w4 `1 n! p
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
8 f: ~& L- ~( }0 g; I8 L. M. I7 ?! w0 ]. Y8 h" u6 F9 G2 H
<TR>* M8 ]1 c: U8 O( a! _4 c
<TD><PRE><CODE>
& H( Z, [; E  Q: G4 S8 zimport java.io.*;5 x0 |" m6 P+ m1 t: G3 `
/**2 K' \, I1 G$ {8 t
* <P>Title: pdf extraction</P>! K4 C. x6 g  q1 E
* <P>Description: email:chris@matrix.org.cn</P>
+ d2 I' x' [$ s7 j+ c; L- ` * <P>Copyright: Matrix Copyright (c) 2003</P>  L( o4 m+ w+ Y9 [1 @
* <P>Company: Matrix.org.cn</P>
$ e$ a& e2 G2 S- K6 B# w * @author chris
1 b+ g; N* z, F* @+ W2 J2 c * @version 1.0,who use this example pls remain the declare' r! E2 {- A! v1 H
*/
! d6 U. T. L  Y& T+ W! v
) z9 n/ c4 A: Y6 @( m+ E3 t4 w0 d7 B5 j" `8 R  T3 R& q
public class PdfWin {* j% Y, R/ a4 a% E" D0 }' s
  public PdfWin() {
; m4 x+ h& O( i$ Y1 s! ^  ^  }  }
$ ]- r7 D3 k' [7 [  public static void main(String args[]) throws Exception
) d& \1 u/ K0 d! C: `  {
: p% J) [; f: N* Y  V6 V    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
* Z' O! a$ T1 L. {2 I    String filename="c:\\a.pdf";
/ u1 A- z/ `, k+ K5 ^2 B    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
6 U5 X0 V) U: `4 `( S2 c4 f, A9 q1 M    Process p = Runtime.getRuntime().exec(cmd);
. w( W4 ?' L/ W    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());# _% G( p1 S. G
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
, V3 [# l9 a/ H8 `( u. ^    StringWriter out = new StringWriter();
- E, E/ |3 V( P" q9 T) t    char [] buf = new char[10000];
+ _' ~7 X5 B) \. p# m    int len;0 n# H9 @' Z- ]! n
    while((len = reader.read(buf))&gt;= 0) {/ D2 V6 @8 W  `
    //out.write(buf, 0, len);
/ t! @" h+ F" Z& d- e# J: p    System.out.println("the length is"+len);
' m: w3 |8 D$ x$ D) T    }
) Y8 m5 p; M3 _! F/ F# m4 q. P    reader.close();$ q1 D5 e( t5 v# ^+ v& o& [
    String ts=new String(buf);
5 {. k5 o% @# O; M    System.out.println("the str is"+ts);
6 v/ d( n& W' E& [- e. O6 Q/ I  }+ v# \% M, b: T$ I
}( Q, k& O( v; v
</CODE></PRE></TD></TR></TABLE></P><!-- RESOURCES--><!-- AUTHOR BIOS--><!-- Make author heading singular or plural as needed-->
作者: 39133120    时间: 2010-12-10 23:59
看不懂·················




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5