数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:15
标题: [转帖]java抽取word,pdf的四种武器
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>7 b% ?% Z: h2 Z8 v. H- ~' p
<><A>1 .用jacob</A>4 w3 V# _" ~* x/ j. C7 _
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>
7 ]/ Y9 F2 P; y5 J<>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>$ \* G0 J- B2 G$ t, y
<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>
% m/ L" L$ x- ?, q<>  \6 n% d  V7 A% D8 Q+ f6 ^
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">( r, r# I8 y+ ~8 ~6 O) J8 I$ U

' y  Q8 B3 ~. {  S: F( Z9 P<TR>
3 S2 ?; t$ M5 T<TD><RE><CODE>
0 I* Z# ~) J% p6 O/ P) Himport java.io.File;
2 o- `7 t' F9 k! jimport com.jacob.com.*;# A( g5 i+ N! }  }  }
import com.jacob.activeX.*;
; F1 t3 I5 i, P5 b/**
6 a2 R7 ^5 B5 N% R4 p) c6 R * <>Title: pdf extraction</P># W7 g9 Q+ Q% L+ N# ^/ U8 A  V
* <>Description: email:chris@matrix.org.cn</P>* Z' N4 W) J. h* p
* <>Copyright: Matrix Copyright (c) 2003</P>
, p" r( c/ T7 g5 T1 S# w8 G2 r" _1 | * <>Company: Matrix.org.cn</P>  X' C2 E; m/ r* e
* @author chris) V+ X9 c8 h+ z  W- ^
* @version 1.0,who use this example pls remain the declare0 r0 N2 Y& `4 u; r
*/
7 U% V- F' m2 b3 ypublic class FileExtracter{  U3 O% Q  ^0 t* H5 ~
public static void main(String[] args) {- }/ d) t# I8 v6 O6 l! @6 O
  ActiveXComponent component = new ActiveXComponent("Word.Application");7 n0 e1 F  j9 F7 r
  String inFile = "c:\\test.doc";
+ ~: C$ U3 `8 |' i/ X" \- Y$ b% \/ s String tpFile = "c:\\temp.htm";
( `0 Y) x) E  ^, r2 ^  String otFile = "c:\\temp.xml";% E: G" O$ P& {0 u8 t
  boolean flag = false;
: E8 m( s+ K4 r" m8 y  |+ Y  try {
9 H! D" `3 |' ?* K9 e, w* l3 W   component.setProperty("Visible", new Variant(false));
. K* n* @, F4 X$ U: z   Object wordacc = component.getProperty("document.").toDispatch();
9 ?' S+ D# S% v. K6 b/ `   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method,
% Y6 S2 f5 A" G7 _% i                                     new Object[]{inFile,new Variant(false), new Variant(true)},, |2 ?2 E9 Q. b& V) [
                                     new int[1] ).toDispatch();
* `" ]6 f( l) q* `: m& N   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);, t1 Z. b; l9 q! H6 b9 ^
   Variant f = new Variant(false);
. b9 \- ]3 w* t4 V2 G, ?   Dispatch.call(wordfile, "Close", f);+ P5 V8 e- |% F2 C" w
   flag = true;
5 l% ~' C1 ?  t; J$ z# @/ e+ y, j  } catch (Exception e) {* n6 S) s! \* \( [( S/ G
   e.printStackTrace();
* k# o4 P4 \9 e9 S, H9 @2 k  } finally {
; R# T( P7 D* ?4 Z   component.invoke("Quit", new Variant[] {});' o3 q3 |6 x$ u. D! D+ V/ _9 y" E) I
  }
4 Q: c& t& K/ O9 a; a9 K& ? }& \6 o. `4 Y: A5 g+ V' m
}
) d) W. }. K( L; \' z$ W' n</CODE></PRE></TD></TR></TABLE></P>/ e9 [8 B; m1 V+ j8 G
<><A>2. 用apache的poi来抽取word,excel。</A>
) N  O5 d  g, T5 L% dpoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
% E1 t3 A. r$ m; z% x9 t+ |, h<>下载经过封装后的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>
% x$ K8 p7 v- R<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>1 S( k5 Y: q) o% R( B( }
<>
; m# J  L$ w) T<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
: H( [6 f; d& }; a, ?1 C4 N7 L2 ~' S1 l) r( f$ H3 Y. q; R# K
<TR>
1 \, Q1 o4 k: G7 O) {6 ?<TD><RE><CODE>
  Z! Z& }) e$ J# kimport java.io.*;
  \) X+ c) |7 m9 _. jimport  org.textmining.text.extraction.WordExtractor;
+ F% o& ^# A, R) f$ L1 Q/**
1 g3 Q' r# q) n7 o8 z# j4 c * <>Title: word extraction</P>
+ Q8 G* J) r  ?. O * <>Description: email:chris@matrix.org.cn</P>) n6 P: q: K$ F, y: b/ T* x' h
* <>Copyright: Matrix Copyright (c) 2003</P>7 Y/ `. v5 a& `; B7 U
* <>Company: Matrix.org.cn</P>
0 j, g; w3 |6 G' l * @author chris
* E1 L' L3 m: _, m. r% i# E$ u, ]3 j * @version 1.0,who use this example pls remain the declare3 Z1 H. h$ s3 F
*/
% P6 w1 F& G0 T& H  R% _. W8 O5 i3 G
public class PdfExtractor {
; B% Q# _! L* E1 u+ J* g  public PdfExtractor() {
; _5 l& b7 G4 ]0 d, o  }
) l: F; \6 X; j; X8 Q- `  public static void main(String args[]) throws Exception
7 o8 X1 p9 S  m+ U" l  {# X+ p9 m9 J0 N! D( G/ Z
  FileInputStream in = new FileInputStream ("c:\\a.doc");
# `6 ]5 u0 Q# n+ e  WordExtractor extractor = new WordExtractor();
* D* T: A1 v3 k, G2 w  p& e  String str = extractor.extractText(in);
5 q2 ^* Q) V6 h. Y  System.out.println("the result length is"+str.length());
5 B* Z# O/ a2 c: _7 Q6 F; f1 N   System.out.println("the result is"+str);
( M6 G! I4 l- n+ \}
  m$ k$ N% U: X/ T# {) u' w}% c+ J: N8 {% f5 s, s% v1 v
</CODE></PRE></TD></TR></TABLE></P>1 O6 j! A1 M2 K5 f2 C0 ^
<><A>3. pdfbox-用来抽取pdf文件</A>
! w/ w2 T) {. q1 \7 ^$ ?但是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>- Q0 B! H. m; ^: ^% ?8 z' J
<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>; Q  P# u+ E% |
<>
8 h9 a+ b3 X* I% t+ q' W<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
" I$ l) m; a2 b* r6 k. R, y! d
1 ?' U  `7 a% f/ J! H6 Y4 S<TR>6 a0 L" _: y% H# i& p& u
<TD><RE><CODE>7 S, T) Q4 ]6 }3 q
import org.pdfbox.pdmodel.PDdocument., F% t' O" T* _
import org.pdfbox.pdfparser.PDFParser;
& X2 c$ A, j5 \8 Y( q  W; O  Bimport java.io.*;8 u7 v8 g: T7 p4 O7 D0 G3 R
import org.pdfbox.util.PDFTextStripper;8 X6 a* ?+ B8 f, n5 B. z/ k
import java.util.Date;
3 a0 {0 l/ R* a2 x/**
/ U$ Y' I- e6 n2 g4 B* e. K% x * <>Title: pdf extraction</P>
6 @7 r3 k3 i+ R: H * <>Description: email:chris@matrix.org.cn</P>* j0 O4 w8 \% j" l/ T
* <>Copyright: Matrix Copyright (c) 2003</P>. c2 A0 u6 i5 [1 a. D9 p
* <>Company: Matrix.org.cn</P>! F* O( ?- x+ ?9 Q( h! {1 j) ]
* @author chris; r8 Z* K1 V7 G2 B0 _" k/ X) G
* @version 1.0,who use this example pls remain the declare
% v+ b) {. u4 M; Y# L, u, Q *// s1 T( l- a' {# u% w& _

& Z$ Y6 W& I- I$ i5 rpublic class PdfExtracter{6 `  U/ Z* c& o& K1 j
* F& V% I0 F- B+ g& ?7 l
public PdfExtracter(){
4 {' m( v# C* N( k! |. V1 m  }
6 h7 b: U" O& i$ @% N7 J% g+ e1 N: ppublic String GetTextFromPdf(String filename) throws Exception
8 b3 P* m3 m: e3 t  {
' W; n0 d' ~/ w) Y* N  String temp=null;
7 t  r, Y4 ]- [; q( w8 U) y  PDdocument.nbsppdfdocument.null;
4 V1 n* }+ d7 N  FileInputStream is=new FileInputStream(filename);1 E0 F0 }4 d" s+ u/ ^
  PDFParser parser = new PDFParser( is );
0 L: F; _  z, F2 b8 V: q0 n  parser.parse();. u& F) f: Z& i3 ?% G
  pdfdocument.nbsp= parser.getPDdocument.);
) }* Y2 p/ S5 i' I  ByteArrayOutputStream out = new ByteArrayOutputStream();6 _# o0 G- x& E5 B
  OutputStreamWriter writer = new OutputStreamWriter( out );
/ Z( B3 l3 x0 ?) Y" s) K  PDFTextStripper stripper = new PDFTextStripper();
) F& |" X; m- v* h) B  stripper.writeText(pdfdocument.getdocument.), writer );
. I2 Q2 `5 _7 K3 \. u  writer.close();
$ A* c8 P4 |" e& |' ?. a- Q  byte[] contents = out.toByteArray();* |- v* _3 {3 V" x$ e
% r2 ]: ^4 X! j% g9 x
  String ts=new String(contents);7 j% D/ \5 H$ X% U/ m
  System.out.println("the string length is"+contents.length+"\n");! l. _  Y, _/ X+ M5 H% f7 R
  return ts;
8 c/ f# R% e2 |7 c/ e+ E* Z}
5 W- ^+ X9 s4 r; Lpublic static void main(String args[])0 u* o0 \& L7 b0 B: {7 Z, v6 H
{
7 L1 U* h6 R% h8 D: Z5 Q2 ^PdfExtracter pf=new PdfExtracter();2 H+ i; K" D1 ]9 \1 i4 l
PDdocument.nbsppdfdocument.nbsp= null;
  w! T6 G; R( P% `% q- {; Q- g
) A  W" k4 T, p/ o- Z( Utry{1 i3 a2 ^  c# F0 g7 J
String ts=pf.GetTextFromPdf("c:\\a.pdf");
3 L0 {+ p4 K( ?System.out.println(ts);
( l0 U3 r& g. J, K$ z5 ~0 u}
4 d' y* `/ J# g  D; o# E. s+ W$ Ecatch(Exception e)
+ e3 Y+ V) t# f  {9 A& ^. }  l4 x
  e.printStackTrace();
# G3 @. ^( m2 ?! v7 x% u  }0 p" V  o$ Q* s+ Q' _8 e' G  l/ g( E& g
}+ R3 w# T; @1 j0 S3 I) c
& K# \& O3 B, U+ r9 g1 U
}
2 v4 h2 ]: g  x</CODE></PRE></TD></TR></TABLE></P>' T8 ~" z3 z, E) E
<><A>4. 抽取支持中文的pdf文件-xpdf</A>! i, `$ y* F' R: I3 X
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
( W7 g8 a1 D# ?$ 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>+ X; R# J- }1 }0 F) S2 \
<>同时需要下载支持中文的补丁包: <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>! `) S: K+ X0 D+ K
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>: `* s, k7 ?4 h* d" c
<P>下面是一个如何调用的例子: </P>3 j2 `+ X0 D% w1 T
<P>, |% ?5 m4 ^  R& _
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
4 _* @) z2 h4 E, O/ e% p/ z5 w8 B% W0 {( C
<TR>
+ f& q4 ^9 U' @& o6 g  P<TD><PRE><CODE>, i/ e- A  m! i9 J* j
import java.io.*;' `) P- s3 o" u8 C% h* j/ `
/**
: E, f! e1 ~7 c, Y1 x * <P>Title: pdf extraction</P>
/ x3 A1 A" t' J: {9 s * <P>Description: email:chris@matrix.org.cn</P>
3 p. M! t. G# h/ K * <P>Copyright: Matrix Copyright (c) 2003</P>' w. \! c! @! \$ c2 J) D
* <P>Company: Matrix.org.cn</P>
& i, {* F; o- b9 u1 C1 p * @author chris, m% }6 I: T0 P  ]% G$ _
* @version 1.0,who use this example pls remain the declare
' O7 Y2 U0 }$ C: i/ {/ t2 F. H: ]* d */
9 ^) I0 z4 V# ?, s/ C' N7 ?
5 n$ v: B4 h5 K5 x  c. A8 j* Y6 A4 W  F2 w9 X  C: ?3 F
public class PdfWin {
, ?9 d( D  ~& @9 P% x1 P/ Q; A  public PdfWin() {
  |. _5 p5 h9 j) p: \3 K  }9 r. F( Q- L" R/ J- S' u
  public static void main(String args[]) throws Exception& k) W( J% \# [) ]
  {; I& }8 |; }3 g8 ?" I
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
% _, g; @; O) ~! W- V' s    String filename="c:\\a.pdf";: }2 w- w  p5 ]' A; b! P9 n
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};$ C+ [  l/ a- ~# c) t: y4 o
    Process p = Runtime.getRuntime().exec(cmd);. M4 ?% G/ R: V2 ~  v) n
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());. F, _" H( f7 g( P: n$ @
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");7 I9 e* d. r( \& r2 ^
    StringWriter out = new StringWriter();  e, \- y$ v  ?% v7 ?# e1 t
    char [] buf = new char[10000];5 H1 G: z5 L) f
    int len;1 L, F! m7 R" T+ ~( z& _* a: d
    while((len = reader.read(buf))&gt;= 0) {
4 s. g6 K$ m" w    //out.write(buf, 0, len);! A  i% x: w9 P5 G9 e
    System.out.println("the length is"+len);+ V3 A" \+ `1 }3 R" z
    }% l3 x% k$ w9 D- w: e9 a% \3 k$ m
    reader.close();: A0 X+ D7 u! `: P
    String ts=new String(buf);
! \9 R. A0 H  [    System.out.println("the str is"+ts);& G) T4 e& s( E! f
  }
- ?& T0 V& p! ], r}
; Q3 l5 c: b: |8 U</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