数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:15
标题: [转帖]java抽取word,pdf的四种武器
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>
: @0 b- U1 Y3 V1 A: p3 ^/ C* e<><A>1 .用jacob</A>3 x( q& @  a* `* q( J' U
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>! C2 O5 n" i' _8 v
<>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>
8 X0 ^: Q" ]4 w<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>; \0 A2 [: c* {" A0 m& D
<>1 D, u6 L6 d$ Y- Y: j
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
) Q1 p* c: K) ^1 {3 g! G( }7 [; f' v1 H( C6 E# C; x5 i' B0 d
<TR>
' M2 q0 a6 f$ L  G, l  I<TD><RE><CODE>- W' a1 z+ @! y, {* T
import java.io.File;
1 j% t- e3 V/ m1 ?" \- y- Rimport com.jacob.com.*;# \! {- s, o# F+ q& P) |
import com.jacob.activeX.*;4 M; D1 ~) E2 |- d$ g
/**. W- |% T8 U' |# F+ I/ Q% I
* <>Title: pdf extraction</P>
% U! W$ u0 n3 L) k, m5 \( Q * <>Description: email:chris@matrix.org.cn</P>
; \5 S! T8 I6 ]9 Z% h! v, ] * <>Copyright: Matrix Copyright (c) 2003</P>
9 @8 v) ~  j% B" M! n * <>Company: Matrix.org.cn</P>
- w6 L4 q4 V& B% s" P2 \5 s * @author chris) a2 s) G1 ~! |; ]* ?. |1 _
* @version 1.0,who use this example pls remain the declare) ^" L$ R8 |% d9 _+ \+ ~: U
*/
) J  k$ p' e9 G: jpublic class FileExtracter{
" f6 h2 I2 I3 P. h0 s public static void main(String[] args) {
' \; I9 |4 F6 L' G3 U  ActiveXComponent component = new ActiveXComponent("Word.Application");
, N- y! s' z) X2 A7 M1 k" c) h  String inFile = "c:\\test.doc";" B# L  |4 z" m5 M. C5 K- y
String tpFile = "c:\\temp.htm";
/ U) _/ G3 O  K2 ]  String otFile = "c:\\temp.xml";
! s+ U: I* i- c: Y  boolean flag = false;- {  D2 i# ]! [+ B
  try {
1 f) d/ f; L7 D% j; v   component.setProperty("Visible", new Variant(false));$ c; {3 [8 E5 D! i9 d
   Object wordacc = component.getProperty("document.").toDispatch();
! y! c5 v3 H8 i# b$ s! |! x& \   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
/ r! z0 t. M6 O5 E6 p5 {                                     new Object[]{inFile,new Variant(false), new Variant(true)},! H0 Y9 H1 K! a( z6 |
                                     new int[1] ).toDispatch();
5 Y) r- o3 k  w   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
' j/ ~  s' H/ `7 T: V7 y: w   Variant f = new Variant(false);
) O' c# G6 p) {+ {& L. K: Q2 z   Dispatch.call(wordfile, "Close", f);
9 ]8 s' P6 |) r) B$ Z# k   flag = true;
# h4 j% o& A) c0 g+ `/ [! l$ ?  z- u  } catch (Exception e) {
8 k6 T$ f7 y* Z4 I7 s   e.printStackTrace();9 `2 q3 a+ K+ j3 b; P
  } finally {
1 T9 W" p, v+ G+ Z+ ]   component.invoke("Quit", new Variant[] {});- X* z) }  d: `) D! q: e
  }
" a- ~! Q8 r' E2 |" {% ^ }% {$ B) T: X( E* c: m& o% R% v
}
2 U0 |; j1 P: f6 v: s! Y+ g; U</CODE></PRE></TD></TR></TABLE></P>! y6 S* \1 S, s' U
<><A>2. 用apache的poi来抽取word,excel。</A>
5 j! O) J7 d% R  ypoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
3 m. E" w8 `. `& h) P5 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>$ N8 Q* R. e7 x* Y; |3 D
<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>- u& l$ B! o/ z$ t0 e- @# H" W4 R
<>+ V5 @6 M: I1 m2 @- F
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
/ o6 h" T+ p6 o0 B; B( m& y/ Q6 @* G( `7 a2 \" Y% U4 O& q4 `8 R1 I. b
<TR>+ I; Z! F  _; l; s$ [
<TD><RE><CODE>/ I! {3 ]* M# A
import java.io.*;& R) U+ h7 C& [) ~$ u
import  org.textmining.text.extraction.WordExtractor;+ n9 [& l* A0 }3 w4 V
/**
7 o) X# k: v5 m1 N$ G9 h- @ * <>Title: word extraction</P>
$ ]5 C  b7 L' M$ H+ u; g; H * <>Description: email:chris@matrix.org.cn</P>+ \$ F. m/ E# U
* <>Copyright: Matrix Copyright (c) 2003</P># ~/ v( D4 E7 v7 s5 D5 S5 j
* <>Company: Matrix.org.cn</P>" r/ [2 F/ k8 {
* @author chris
6 ~& ~- s2 R1 N7 c9 G8 e+ J4 ]5 b7 _+ f * @version 1.0,who use this example pls remain the declare
2 s! B2 p* A! z4 Y; v# Y */
' S& a3 p+ F/ F5 b  t3 E( T# f( v! w+ ^. `) D
public class PdfExtractor {" h: [# e( A9 S2 @( L, b7 ~/ B0 k
  public PdfExtractor() {: r! M, f5 Z5 q
  }6 F0 G( s9 e; p- L
  public static void main(String args[]) throws Exception
3 o: B* e* o. ?, r& \! l. h  {7 B% }$ ~: T# T
  FileInputStream in = new FileInputStream ("c:\\a.doc");0 _# ~7 L( \; }5 y1 H9 @
  WordExtractor extractor = new WordExtractor();% ^; d2 y/ V0 L% v1 m( l8 \+ f
  String str = extractor.extractText(in);' K7 W  y$ C+ \5 Q6 N
  System.out.println("the result length is"+str.length());
8 T6 E$ S: {  _2 J$ f   System.out.println("the result is"+str);
# N* X7 {8 l& ]5 l}
' F% z4 ?1 n- A1 C* X4 V. p}
% K. m7 l1 @9 ?2 L6 {; R( z</CODE></PRE></TD></TR></TABLE></P>- g0 S0 N5 N+ l( z; G; V
<><A>3. pdfbox-用来抽取pdf文件</A>5 k% c2 j) }( s$ R, L
但是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>$ A& o+ Z% ], d
<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>  H  X7 I4 A9 p& i0 N
<>- s% U4 p# M8 S& z1 F- F
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">0 Z3 h2 s" B' E0 `9 J
. d/ R: [! S! b  j4 o
<TR>
4 r/ M- i& ^" _2 \- o9 u- M# A0 @<TD><RE><CODE>
* _" ?6 E: V0 e# H) ^; ]import org.pdfbox.pdmodel.PDdocument.
; `' M) K, {. H7 r( ^, Z; v, bimport org.pdfbox.pdfparser.PDFParser;
$ b/ J: |% w' d, `" ~  j' Ximport java.io.*;
, y/ T0 U- h6 S, F6 ?8 |5 P6 `import org.pdfbox.util.PDFTextStripper;
# B% V* V  _. W' iimport java.util.Date;
1 t$ Z' ]/ n) Y/**3 h/ ]/ w8 O* H+ Q* Q
* <>Title: pdf extraction</P>
* P8 b+ w6 O. w% G4 W2 ^ * <>Description: email:chris@matrix.org.cn</P>5 }6 L3 \* t0 c) T- _
* <>Copyright: Matrix Copyright (c) 2003</P>. n* Z. h0 C# m( W: q, v
* <>Company: Matrix.org.cn</P># D( k# m0 [- L' H2 K+ H
* @author chris
) S( c# p+ O! I# N * @version 1.0,who use this example pls remain the declare
8 u! y7 _5 x% z4 K0 ? */+ s5 e. {7 T  {, f, t
9 F$ \: r# b' y; V8 }- v/ |" M
public class PdfExtracter{! @$ C, Y& S* g$ c/ p$ k8 u- J

. s% m2 a# p5 {6 Qpublic PdfExtracter(){; w, {$ M, g; `: F
  }
) d5 x: d* }7 Fpublic String GetTextFromPdf(String filename) throws Exception
& q$ n* N7 b! i6 y1 s8 a  {
. R7 q8 z$ q/ ]- u2 k9 R- m  String temp=null;+ g4 u6 b6 g5 W2 X3 p& P% s' x
  PDdocument.nbsppdfdocument.null;# i# p, n( s' m1 Y! \8 y) g, L
  FileInputStream is=new FileInputStream(filename);/ s. Y# T" b$ f4 a0 o
  PDFParser parser = new PDFParser( is );; g- a' Z8 S; f1 f0 A6 f
  parser.parse();7 z8 A1 e* n, I+ e. Y0 \1 s
  pdfdocument.nbsp= parser.getPDdocument.);
2 S8 x0 W8 J# i6 |; B9 |( N  ByteArrayOutputStream out = new ByteArrayOutputStream();8 |6 ]7 s$ f$ {& t' w
  OutputStreamWriter writer = new OutputStreamWriter( out );
! k- i; d. h8 b2 V! F' H  PDFTextStripper stripper = new PDFTextStripper();' Z! n: `9 m* \3 t! L. B& k
  stripper.writeText(pdfdocument.getdocument.), writer );, K  j9 c- H2 \. I! H
  writer.close();
5 Q! H* q8 J) p# m& V) D  byte[] contents = out.toByteArray();- {/ f8 ~( x: a0 m- b$ `7 e
7 }: H* z0 U( Q7 u" K
  String ts=new String(contents);
) k3 E3 ~6 L: K$ J6 H; J  System.out.println("the string length is"+contents.length+"\n");
7 ]' x7 B& v0 r: w; W) i  return ts;# P+ _( v0 `" `$ T( ]0 ?9 u
}0 ?5 y' C2 e. E0 ~( m( h
public static void main(String args[])
/ ^% G' m# w  R. ?) r2 t{
* K% q; f* {" J4 gPdfExtracter pf=new PdfExtracter();) ^/ O( @# Q, G6 p% L! o
PDdocument.nbsppdfdocument.nbsp= null;9 Y. l# |/ i( f2 I" o1 s

) r0 `$ C& f, h1 ^1 q2 |% l6 C5 Y9 y; Utry{; y& M# i$ o  ~7 T7 a( V! _) A; F
String ts=pf.GetTextFromPdf("c:\\a.pdf");4 B' a9 b& ?7 I# Q
System.out.println(ts);. p6 o6 [7 m0 |2 G0 l, l- M0 C
}  o4 [1 M" d& E1 {) b+ Q
catch(Exception e)
4 B( Y* d) n* e* S8 e: y3 e: K- K  {
1 i  \$ S+ Q9 @- o% A  e.printStackTrace();: K0 i- x0 F6 {5 {  ^
  }
3 T$ k( }  K" C$ r7 Y7 B: r}9 F* O7 b  T2 d, ^" j: q, k0 d  J8 m
, d( h$ ^8 q$ v0 J; B+ _1 D- r
}
: ]: M; }8 L; q$ k# E9 \3 K</CODE></PRE></TD></TR></TABLE></P>
0 s7 g& r. b0 s6 C3 w1 t6 u) y: f<><A>4. 抽取支持中文的pdf文件-xpdf</A>9 a  l) J# s! B
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
4 D2 a6 W3 U- ?5 P0 h7 M# i9 @<>下载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>* p1 G& m$ k  o7 |- 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>
. h$ V3 g% X2 q<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>( I0 m, F" g4 ^, |5 ]
<P>下面是一个如何调用的例子: </P>, o* G6 U" Z8 a; C3 _
<P>
7 O5 M) }2 _3 P7 u1 k<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
' j( Z3 e0 D5 M- n% m6 I3 O. N% W2 m/ ^& O' Z. Q9 o6 V* Z
<TR>- ?5 D- e! _% M' U: G4 L  w
<TD><PRE><CODE>5 T3 U) y3 p* \& U
import java.io.*;7 x0 P# C5 y% P/ w$ E5 w4 S
/**
5 D+ M" _7 d* `" b( e5 z * <P>Title: pdf extraction</P>
. h4 G: F" ?2 ?& H- {" M( D/ d3 p * <P>Description: email:chris@matrix.org.cn</P>
0 k) p! b& B, k# k& _& h3 T- G * <P>Copyright: Matrix Copyright (c) 2003</P>9 E; |8 ]4 h8 R
* <P>Company: Matrix.org.cn</P>
0 u3 k; ]/ O1 o6 K& @% Z * @author chris
6 O% ]+ h' L7 e7 G9 I' O * @version 1.0,who use this example pls remain the declare
. h+ d% N, G" M7 E0 Q */. p1 G" k3 T/ r: X* _% m# @, q

3 V5 R/ |: x# U
2 E  Z4 ^0 I$ Zpublic class PdfWin {. Z- U8 T. z" p) z( |0 W2 t/ r
  public PdfWin() {7 C* ]/ c7 {7 w9 t
  }
/ p4 w$ L) O. z. L  public static void main(String args[]) throws Exception
9 O- ~3 Y3 }, v' @- {  {0 V) }# ]$ \+ o- k+ [. Y
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
: g1 V7 U) {7 m" o" b  {8 C" T    String filename="c:\\a.pdf";7 S4 ^( N6 }" c; s0 M- b
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};+ ~, U! t( {5 b$ @) ?6 Q. C5 v% {9 P
    Process p = Runtime.getRuntime().exec(cmd);
; `( T3 ?1 X; l+ T% g' g    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
1 |+ k3 M! n7 H+ ]( _( b    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");% c1 d8 h6 S1 w9 Q, i$ D
    StringWriter out = new StringWriter();7 s' x- U5 S! d
    char [] buf = new char[10000];% k( H$ [, ^. `/ S8 N1 r
    int len;5 t1 T1 u- E: P
    while((len = reader.read(buf))&gt;= 0) {
' m% Z! `7 d6 b, R    //out.write(buf, 0, len);
$ B2 B% R( F) e; a4 b    System.out.println("the length is"+len);
9 |9 H$ ~9 f0 k9 L" B    }5 C# P- b5 D1 ]( `+ U- u
    reader.close();
. }# E; Z5 @, _. g+ O    String ts=new String(buf);4 @/ U4 C+ @" {6 I! T: t! r
    System.out.println("the str is"+ts);
+ x$ a. \0 @) g  }7 h$ z" Y) l) [: J6 Y* [
}
4 }4 g$ F+ a/ s* X</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