数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:15
标题: [转帖]java抽取word,pdf的四种武器
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>1 h$ j4 W5 K2 N5 l1 l9 j
<><A>1 .用jacob</A>
9 G# [2 e9 K1 v0 m2 s" U其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>
) ?* u( h8 h7 ]/ [<>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>' B+ H6 h  k" j/ w& m
<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>5 Y* _1 m- j! d/ F& z* i3 k* a
<>
0 ?6 y5 C+ ^0 y* F; W  l<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
# }2 {  u6 K) V
" a2 R  K1 U3 R; Z- r' Y<TR>  J0 k. h3 r% V7 D3 r
<TD><RE><CODE>- b8 g4 b4 w/ U
import java.io.File;4 T# ^9 s4 b0 U* I5 Y% C
import com.jacob.com.*;
+ A, V$ T. v8 ~1 Z8 Jimport com.jacob.activeX.*;  e0 Z" q9 r6 E
/**! X% J8 P, e6 a6 l
* <>Title: pdf extraction</P>) ?$ ?; }" l" J% m& X! q2 P# c$ O
* <>Description: email:chris@matrix.org.cn</P>" E% Z( n# v/ c- r  F
* <>Copyright: Matrix Copyright (c) 2003</P>% n3 ~( E' y9 Y  I
* <>Company: Matrix.org.cn</P>" z4 O% n( f8 _1 e  ^
* @author chris$ }5 u& |$ N% t
* @version 1.0,who use this example pls remain the declare
! M0 h' T( D+ m: o# t6 q0 ~ */
3 e9 n5 L/ y+ q0 p4 k+ Apublic class FileExtracter{
' d4 g( P% G6 F3 @2 ~% j5 x public static void main(String[] args) {
  B0 \: i$ b8 d8 {  ActiveXComponent component = new ActiveXComponent("Word.Application");
- ?; |, j* i1 K- u4 R. |7 x  String inFile = "c:\\test.doc";" N) r& Z& ?3 l& ~5 ~; B  I
String tpFile = "c:\\temp.htm";5 M0 u0 A' |- e9 P. N* {9 J) v
  String otFile = "c:\\temp.xml";/ z* R1 }  I" Y$ }. n4 J6 v
  boolean flag = false;; O. ~% {4 I3 L& \
  try {, m  k/ [# Y' U1 `& z
   component.setProperty("Visible", new Variant(false));8 Z- y4 X8 v, x0 h7 C( `* z7 k, u8 l
   Object wordacc = component.getProperty("document.").toDispatch();5 z5 B6 A' `, x
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, % G% [% T6 J" \8 p
                                     new Object[]{inFile,new Variant(false), new Variant(true)},# n4 j7 @5 q% B9 d2 B& k7 f
                                     new int[1] ).toDispatch();% T/ f0 t3 L% h/ p7 G" c+ p+ W
   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
- w0 ~7 y0 G+ R- c& B' Q   Variant f = new Variant(false);% H/ B- w$ O; C0 V% y" n0 J' |& A0 V1 N
   Dispatch.call(wordfile, "Close", f);# ?0 g$ q8 n7 J" q
   flag = true;. b$ Z; N9 s9 J9 D4 y( h
  } catch (Exception e) {. i6 a0 O# o+ l4 G0 ]
   e.printStackTrace();$ f7 C% u) k- Z/ [" _8 X, S% t
  } finally {( X; D9 H( @- R6 {3 n9 R
   component.invoke("Quit", new Variant[] {});
& b' a0 r: r8 Q, {9 Z: h  }
5 @' o8 [5 `% W; H6 ]- @- S }
" t5 ^, P) p6 ~3 L* C' `}* H1 d3 \) I9 O! V. ^
</CODE></PRE></TD></TR></TABLE></P>6 h1 i. d4 {6 w' [: {$ N% h
<><A>2. 用apache的poi来抽取word,excel。</A>6 T1 d* y) s0 v5 A
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>9 T, S& ~2 M4 }0 H  r
<>下载经过封装后的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>
2 d4 R& B' M$ C5 w<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>, d; N! y  F7 @. C9 [) e; i
<>8 Y4 K; O0 x. S+ e$ u* [
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">/ T! A! }5 Q6 Z% w6 k. g
# w9 o$ z5 n; ]# W8 l0 c$ `( j7 L
<TR>( E* b6 o3 b# U
<TD><RE><CODE>( e0 }& {/ U1 R- u7 a" j) i
import java.io.*;7 v6 C. L  k2 A3 T
import  org.textmining.text.extraction.WordExtractor;
  K+ O! X9 z' w, ~$ W2 I/**& y4 U  Y2 {. F, P( ^. o
* <>Title: word extraction</P>8 L5 C, M- P8 f* Y6 J; h8 B
* <>Description: email:chris@matrix.org.cn</P>
$ ?1 E1 F" f6 ]- ~; P3 |- M0 L7 `+ U8 i * <>Copyright: Matrix Copyright (c) 2003</P>% |; h3 K3 x$ x& d
* <>Company: Matrix.org.cn</P>
/ G4 S3 a  I8 Z( F * @author chris, J. U+ W, `9 x& U
* @version 1.0,who use this example pls remain the declare+ T8 s1 U+ b7 N/ E1 ^
*/! Z& b- c( O) u9 n) P5 Y- {
) q, _7 `# l  x! l+ ~
public class PdfExtractor {: L8 q; G4 l; _
  public PdfExtractor() {
- W8 P1 _; i0 @6 I0 u$ y) w: k1 B  }
9 R# G- _; @# {# S& Q# Q9 @0 b# d  public static void main(String args[]) throws Exception
% c. f2 ~# F- x9 K  {
% l* e2 F( Z% |; Y8 X9 b  FileInputStream in = new FileInputStream ("c:\\a.doc");
( j( Q9 k1 g5 Y# W. i' d. K  WordExtractor extractor = new WordExtractor();) t  J6 |6 v. R( q( K% @9 m; C, S
  String str = extractor.extractText(in);0 m2 X2 u8 w, v4 G7 O2 b
  System.out.println("the result length is"+str.length());% Z' F* ^% M9 u6 k8 C8 t" E% J
   System.out.println("the result is"+str);
1 M* S" R* d3 s/ R+ }: ^}: ^5 u" h& h6 v" Q: S/ m
}
/ a- ?9 J! w. t- B, ~</CODE></PRE></TD></TR></TABLE></P>8 L& ^9 Y' y/ ]- \- k% B
<><A>3. pdfbox-用来抽取pdf文件</A>& z% D/ I) g0 v' w1 a6 i
但是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>
2 `/ u' t) ?$ j, }0 s<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>! \( K% ~8 \# Z) `# h- F' |
<>, k5 v( J4 Q2 c% F! `$ v) B
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
" g2 T/ P4 w) I$ ]+ B- z0 k* m
5 ?8 m, Q" Z/ o5 S! T' ], w: _<TR>; q# M+ C: H7 t0 p
<TD><RE><CODE>/ p, n0 ^" G# E6 [+ M; L
import org.pdfbox.pdmodel.PDdocument.
$ w" C6 W) t; s  qimport org.pdfbox.pdfparser.PDFParser;6 f, g" ]( J: P
import java.io.*;
! v+ `, S3 e& h' m; \import org.pdfbox.util.PDFTextStripper;
. q- N% ^& ]+ S0 H  c$ H6 k" x7 Q6 pimport java.util.Date;& J5 V1 k5 H5 m
/**# F/ ]+ c: R, x+ i1 @
* <>Title: pdf extraction</P>
2 v( S; C# L* I$ |3 f, n' ] * <>Description: email:chris@matrix.org.cn</P>7 F/ X  v0 K8 Q) K5 f6 \
* <>Copyright: Matrix Copyright (c) 2003</P>
( ?7 [. `4 R; d3 b+ x: i% T * <>Company: Matrix.org.cn</P>
2 w8 [3 P9 s7 v& Q) @0 p4 l * @author chris& v0 ?  O, d' D' m/ t( f
* @version 1.0,who use this example pls remain the declare
: D' }3 M# c* q2 p, G9 h5 {7 x */
7 J$ N, M: ~7 d3 h: Z9 }
6 I; B& X" Z5 O! I; J: [5 Xpublic class PdfExtracter{- l# ]0 m$ a3 |. w( t  Q7 }

* b$ J4 ]" l+ o8 apublic PdfExtracter(){
1 S. ^1 h$ C: S8 {2 E" X0 x  }
7 b. H9 I; T1 ?& G; d+ D4 ppublic String GetTextFromPdf(String filename) throws Exception8 A4 i- O9 ?* j% n0 F' G, p
  {* j; I! E, V* L
  String temp=null;* t2 K' W9 j1 _$ u2 L
  PDdocument.nbsppdfdocument.null;
) q% D; V% c) \6 Q  FileInputStream is=new FileInputStream(filename);
. L8 z% y" ]& u8 c. h4 ?  PDFParser parser = new PDFParser( is );9 |$ g3 T$ H) _
  parser.parse();
0 W- C. _( G6 J; a$ _9 o  pdfdocument.nbsp= parser.getPDdocument.);
+ t/ ~! r3 O! y" n! M/ w# p0 h+ g  ByteArrayOutputStream out = new ByteArrayOutputStream();
1 ]/ s  d! c9 {- N0 n- M  OutputStreamWriter writer = new OutputStreamWriter( out );
1 M1 O9 Y& a) O8 D  W$ e# i  PDFTextStripper stripper = new PDFTextStripper();6 _# U/ l) a# k! l
  stripper.writeText(pdfdocument.getdocument.), writer );' A7 _0 b# l* q% R) C4 {" s) \
  writer.close();5 }8 x! W4 L/ Q1 X) Q7 j5 c- g  R
  byte[] contents = out.toByteArray();
4 Q& s+ ?) Z! i5 A. \  j" X; P: M# N
" C8 k: A, i; b: q$ n  String ts=new String(contents);; a8 K8 E& Z& w$ O
  System.out.println("the string length is"+contents.length+"\n");1 B! T0 ]- ^7 I8 ?5 l5 R
  return ts;
( Y) @9 B3 ]. B4 c2 n$ m& u& R}
& ?* q7 y8 H+ X/ k( j9 Bpublic static void main(String args[])' C8 S3 V" v/ M9 R- {/ K
{& W& b4 p  }5 ]" R) y- x
PdfExtracter pf=new PdfExtracter();
8 V( V: y! x! o# h7 dPDdocument.nbsppdfdocument.nbsp= null;
2 Z3 J* h8 t* S# W/ _* R2 B
! ~/ Q( P2 h1 [4 X. ]try{
" V  f, E' M, S0 G, V' {* rString ts=pf.GetTextFromPdf("c:\\a.pdf");
3 S& F" L1 s5 ]; E9 j3 WSystem.out.println(ts);( {$ H: y$ L% {, [$ J- @
}
! Y1 z; i3 v+ A0 j* Y0 T/ o" Dcatch(Exception e)
0 i& L( P# Q" R/ d  {; ~! x" Q( g) i
  e.printStackTrace();
/ l7 X% ~( |$ u5 V( \8 o3 S! U6 L  }. p3 N0 S1 m3 W+ G' R
}
, P6 F! y0 a* \9 `8 D6 ~( J( G! P4 h# }7 ]8 y
}7 x$ h3 B* Q% u7 F. ]& j
</CODE></PRE></TD></TR></TABLE></P>
9 q7 |# ?- P; \8 T5 D+ d8 o<><A>4. 抽取支持中文的pdf文件-xpdf</A>* ~3 L" R6 W3 P( z
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>0 Z: G! {: _/ X4 N- W
<>下载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>
) R! l. D) L& z<>同时需要下载支持中文的补丁包: <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>& l1 w! H$ B( L, n/ c; c9 k
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>& ^4 H8 N9 b1 S% c. o
<P>下面是一个如何调用的例子: </P>
( r6 u% ]( O) d/ ^<P>0 u( R( ]) T& D( g
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">7 g9 l4 O  U+ U* t9 k  t9 H0 l
/ z+ _5 W2 Z. a- D. K
<TR>) h2 J9 W" ?0 I/ ^( n4 a5 e
<TD><PRE><CODE># Y' B3 [& r2 w' J+ h1 j: F
import java.io.*;6 t. e; Q% A; _3 X* b+ L9 x; C* ]
/**. I) \3 G: h2 P9 }
* <P>Title: pdf extraction</P>
' \9 E3 h/ D  J$ y! J: J * <P>Description: email:chris@matrix.org.cn</P>
" V0 P8 B8 {9 ? * <P>Copyright: Matrix Copyright (c) 2003</P>+ _' }( v: M  M; y9 a9 l! H
* <P>Company: Matrix.org.cn</P>. q4 B0 Q/ x0 R
* @author chris
( |8 s9 i( g+ d) M( l  W * @version 1.0,who use this example pls remain the declare# [0 d& l% Q! o  ?" _
*/
! Q/ v3 K; T" B1 T7 d: t1 E
% u+ C% p2 ~' @! p3 T. i# K$ D/ E  D& |) q5 M
public class PdfWin {3 o8 A+ _4 Z* z& p& ^6 t$ y
  public PdfWin() {  j/ j/ p4 w0 }; g1 I
  }
9 k, g8 F- W- o3 n. R  public static void main(String args[]) throws Exception
9 d$ D* G! p0 y  {) a/ n2 m; g, ?# R; u
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";) C# s! j' N" ^  n
    String filename="c:\\a.pdf";8 f5 F( M4 e$ |) S$ f4 w" m8 ~
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
. u! B$ R$ e4 o    Process p = Runtime.getRuntime().exec(cmd);" x, ]8 w7 q$ ^# g8 U
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());& U3 C+ ~4 s9 }$ E" Z6 a  n
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");% v- Y: X- b2 ~- K4 U2 Q
    StringWriter out = new StringWriter();
/ P1 R. \" v5 k* q! O    char [] buf = new char[10000];
2 \' n' H. q' o7 t* N$ \    int len;% ^* S& n0 C8 [0 z/ ?; G
    while((len = reader.read(buf))&gt;= 0) {1 J' p0 z7 c! v+ b( }/ [: B+ T/ }! A8 V/ ~
    //out.write(buf, 0, len);" N9 h! z; I# B' n4 D
    System.out.println("the length is"+len);3 b: B( r+ \7 |( \
    }( x+ [7 M4 I. R# X1 Q
    reader.close();
) r2 u; }4 Y! k; q    String ts=new String(buf);
5 c* U" D- K, @    System.out.println("the str is"+ts);, U: Q! s+ j' |7 z( Q4 G
  }6 l7 B9 O' ]! O- M, ~, e; P, _' h
}
6 [# s! z3 D9 a0 P2 W# t( w7 G5 S</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