数学建模社区-数学中国

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

作者: god    时间: 2005-3-31 01:15
标题: [转帖]java抽取word,pdf的四种武器
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>
# Z; S* [+ ?8 ?' a; g7 w<><A>1 .用jacob</A>
5 r% @9 V/ {* K: [. X其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>% L3 C1 v* i# \  f+ I) }
<>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>
: s9 X9 u: W. F& u6 m# y, z( w' w<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>) w2 |% Z: m1 v7 D
<>
! P! w5 V: F( d! J! t<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
8 V5 E" a2 Z+ Z& |" M' I# s
. O# O- j. I% b; ^! S<TR>$ ]6 o3 H( Z8 d  t! J/ \
<TD><RE><CODE>: U) R3 G0 m8 D+ [; o4 b0 z( W
import java.io.File;
) T+ r2 z: X# O+ n0 q$ ximport com.jacob.com.*;
% S$ z7 V0 u7 M. K" H" b/ M. t' zimport com.jacob.activeX.*;
5 D" y4 ^5 q6 p( T) v  |/**
; @; v2 [  o0 Q' r! ]! b * <>Title: pdf extraction</P>
/ s# [8 O+ s4 J0 u: A( g * <>Description: email:chris@matrix.org.cn</P>
. g5 e7 u& V% G/ }! Y; {5 u. W * <>Copyright: Matrix Copyright (c) 2003</P>
  ]3 k& b  H) x% j * <>Company: Matrix.org.cn</P>
9 E! C) b. b7 ]6 e4 x * @author chris
0 r% G4 l8 x1 R2 q- a9 u$ N) Y2 |7 O * @version 1.0,who use this example pls remain the declare4 L; X2 C6 D; L; g
*/
! ^! f$ |' s2 O& s9 Q2 npublic class FileExtracter{
; |" ^2 m0 j. I( Q) m5 i% H public static void main(String[] args) {: R* A2 _- |. R0 N9 i# R* ^. V
  ActiveXComponent component = new ActiveXComponent("Word.Application");
" H; B8 t8 O( K7 B  String inFile = "c:\\test.doc";4 ?# L+ s% K/ U, [! r
String tpFile = "c:\\temp.htm";; ], |( E6 w6 X
  String otFile = "c:\\temp.xml";
! C. F3 p8 }1 b7 w0 r$ v  boolean flag = false;
) W. u( f3 i( B; v  try {% k# H7 U7 W" a$ e, w! b
   component.setProperty("Visible", new Variant(false));
% K; ?+ N* o' d   Object wordacc = component.getProperty("document.").toDispatch();( c; m! t( d7 G" K5 h" a/ J
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, # a  S* x: @; p' S$ P
                                     new Object[]{inFile,new Variant(false), new Variant(true)},3 Z3 @, d8 w( ]. @7 C6 J
                                     new int[1] ).toDispatch();
- U$ O# O; c5 I8 v  R( k   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);2 A$ F0 J& U5 Z! c$ ~+ v  x, ?
   Variant f = new Variant(false);$ t1 V4 p/ v7 x8 L
   Dispatch.call(wordfile, "Close", f);& r& V+ \: U7 t6 s! A
   flag = true;
9 z- J( m- X8 A# O9 E( t1 N) R) Z- }  } catch (Exception e) {
' j! N6 z) K; [, {   e.printStackTrace();
4 @  l. w" e/ d- k, z  } finally {% }. ~5 F# G3 z- b6 h
   component.invoke("Quit", new Variant[] {});
4 O/ |( ?1 J! s, G% y! a7 \, c  }9 l5 |1 v5 s: \2 n9 c# k4 ^5 i
}0 ^: M. h5 \. `4 P
}9 {6 Z! [- I' z4 f# S
</CODE></PRE></TD></TR></TABLE></P>- H: [" b( O: I$ }7 G
<><A>2. 用apache的poi来抽取word,excel。</A>
) j( {$ w& S2 h3 k5 ^poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
% l/ [  {: @) A<>下载经过封装后的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>
. b) J( k) }- q<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>! `( [6 ~5 A: K
<>. E! N# t. v; O9 V
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">; H8 A$ Z  e# c

0 Y4 J" g+ {" ?$ \, n6 B% _0 i<TR>* ?) ?" o: k: W. m# U7 [1 }
<TD><RE><CODE>
* e7 F0 x) q9 I0 Limport java.io.*;
1 L6 E# G( X# gimport  org.textmining.text.extraction.WordExtractor;" ~+ ~& _% q6 Y) B9 |9 \" }+ ^
/**
2 W, p3 n" i- G' o0 E * <>Title: word extraction</P>
' \: a# ]& v# b6 E5 T * <>Description: email:chris@matrix.org.cn</P>3 F+ ?2 q" H) |, u
* <>Copyright: Matrix Copyright (c) 2003</P>7 ?2 R0 @) Q, T! G
* <>Company: Matrix.org.cn</P>
- i. I1 K2 K! V, g( q * @author chris$ d7 S' t! F3 R: Z
* @version 1.0,who use this example pls remain the declare
# _& Q7 f  f' Y( L+ | */( w6 G/ K8 r+ o* a; h% _' m
4 O7 e! U% x+ _% l
public class PdfExtractor {- i7 G9 m, w: x& l$ [& J
  public PdfExtractor() {
4 b3 y: b7 }/ e& V3 u8 |) K) g  }" ?* o/ ?+ J  \$ K
  public static void main(String args[]) throws Exception
5 ]/ `* C* L9 b3 k. @( i  {
* O( t% S/ k* x. P* [9 x! Z. C8 q  FileInputStream in = new FileInputStream ("c:\\a.doc");
. L2 W, Z. o( _# R9 b. Y8 H  WordExtractor extractor = new WordExtractor();' Q1 C/ \4 O. |. A! |8 f. K3 n2 L
  String str = extractor.extractText(in);! L" T$ [/ M+ V* ^6 f  X* W1 L
  System.out.println("the result length is"+str.length());  j' m% F: i0 R4 M
   System.out.println("the result is"+str);5 R/ R' k  t& E5 h# L
}5 c; f2 T/ s% V! X
}
+ R+ E6 Z- ~" Y</CODE></PRE></TD></TR></TABLE></P>+ g" q" e2 V0 g; @# Y1 G7 {* ^
<><A>3. pdfbox-用来抽取pdf文件</A>
+ e% Z8 b) S% Z0 R1 F  a但是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>
- w9 y4 E# a) }% u<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P># G+ R9 }7 j# }# v
<>, T3 e% R" p) u; L2 J
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">3 m8 `5 O' r. \1 {) b1 X" d

+ s( X, T  R! q" x<TR>
  a5 }% ?" e. ]% ?* z<TD><RE><CODE>
; p3 `4 B( }9 }+ ]0 B# t* Jimport org.pdfbox.pdmodel.PDdocument.
+ l# @; p( Z8 K/ b; Gimport org.pdfbox.pdfparser.PDFParser;
2 K+ w2 m" b2 G# Simport java.io.*;
5 U7 v6 ]* t5 ^9 e8 M* [import org.pdfbox.util.PDFTextStripper;3 c( f. C/ N9 s; R/ ^. G
import java.util.Date;. G0 ~8 v9 g1 F+ J" G$ x3 J" b3 e
/**
1 i) I, r0 ?: m * <>Title: pdf extraction</P>) E2 h  D7 F0 y
* <>Description: email:chris@matrix.org.cn</P>
4 v$ B! V9 m' i/ N * <>Copyright: Matrix Copyright (c) 2003</P>( p, |2 y( T) v! E7 l
* <>Company: Matrix.org.cn</P>
  t  A- c* h" h3 x/ Z * @author chris
& X9 O# a$ |$ i, s3 }" D+ u * @version 1.0,who use this example pls remain the declare) X. E2 S+ L' o& N* u
*/
- U* a9 o/ _; {4 P6 g6 m( i! q
public class PdfExtracter{
1 h! Z/ N4 M1 b$ e3 p8 Z0 Y% q
* s0 Q' Z( \) U9 xpublic PdfExtracter(){! B1 \6 f9 F  }% s& D9 t: ]
  }/ W, v; g; U/ Q: d, _. O4 _4 C1 Y! c7 v
public String GetTextFromPdf(String filename) throws Exception
8 l7 q" Z! J% y: Z  {; v. N- n  F: ]! X* X6 p
  String temp=null;
* L* M" t0 Y* c4 V3 Z/ D! }  PDdocument.nbsppdfdocument.null;0 u( j& M+ c) V1 T( @
  FileInputStream is=new FileInputStream(filename);' c4 x0 t$ G% L! K7 t7 r1 K/ y
  PDFParser parser = new PDFParser( is );
% t1 I. ]; k( O7 |! R# r  parser.parse();
% b, u" M  m3 n4 v/ [2 v$ k. i  pdfdocument.nbsp= parser.getPDdocument.);0 M1 v8 G5 |/ p3 @' ^$ f; D2 Q# j
  ByteArrayOutputStream out = new ByteArrayOutputStream();
9 ]3 E; `) @. `% x  OutputStreamWriter writer = new OutputStreamWriter( out );. X7 i/ |8 _% B% k
  PDFTextStripper stripper = new PDFTextStripper();
; e6 w9 m4 M( ~% S9 b+ x, p" s' D+ _  stripper.writeText(pdfdocument.getdocument.), writer );- C* {* u' }, n: E& r" o
  writer.close();- Q9 w) g! |1 i: N& u. C& k
  byte[] contents = out.toByteArray();
' n: Q- f3 ]3 x0 O7 d; v  X7 w- b- z: R: g6 j* a, m
  String ts=new String(contents);
, e5 `7 L4 u- k# z- m8 g0 g! {* N  System.out.println("the string length is"+contents.length+"\n");
% t2 ]( y0 ~' {8 z+ Q! i  return ts;% \- b7 O% G6 k/ E* O  m! T8 T
}
6 z/ v( d& l0 A3 g& r, Epublic static void main(String args[])5 f- E6 T0 L8 i
{" `  A/ k/ h; Z5 G8 X
PdfExtracter pf=new PdfExtracter();
, O2 S1 R+ [9 R; M6 SPDdocument.nbsppdfdocument.nbsp= null;
4 z1 U5 Z$ [" N9 r8 Q/ k
4 N- z- C% z( p; atry{- g8 B: O. V; k2 k& B" X9 r! J
String ts=pf.GetTextFromPdf("c:\\a.pdf");
, {& n% {# y4 u: R2 z! |/ M5 ISystem.out.println(ts);% y, K9 i; t2 _& s- x: a  E
}5 h/ T5 L. r& b+ O  Z' b
catch(Exception e)
- B- }. I# Y' F4 P6 B5 e8 }  {7 n& L. E  z+ e. l  {; ?% V- N
  e.printStackTrace();7 y4 d/ q7 M& p0 g+ ~' Z+ ?
  }
& ]5 r+ C  M7 T9 F' f# K4 U; q7 s}2 X6 J  v8 C9 G& x( p1 i; {

' b# B7 [4 L. a; M! Z& W8 s; L$ v; }}
& R/ p! |1 e5 i% A; J" f3 i</CODE></PRE></TD></TR></TABLE></P>
9 n8 T: ?  m4 s% x' C<><A>4. 抽取支持中文的pdf文件-xpdf</A>
( D1 b3 f; m9 S% B1 e( B. Wxpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>3 c' [+ I: h3 |" N; @& b
<>下载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>
, Y9 I2 Z* _( K" \& X3 ~<>同时需要下载支持中文的补丁包: <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>' h8 C' I0 p4 v9 b. |( ]
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>" K( n1 t, c3 r3 y/ r
<P>下面是一个如何调用的例子: </P>- C2 h; E0 m8 I& H! D
<P>
' J  Z  a( c. v7 o; k: L7 N/ V<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
5 K# H0 m! t0 @$ V
5 |7 ?6 N; X" k8 z% k/ [4 ]& a<TR>
; [* A# S; i5 }+ o! x8 m<TD><PRE><CODE>" _2 _. v% o& q( |0 Q( X5 h  E
import java.io.*;
: x$ I6 \' ]3 M0 c1 D5 n/**0 l9 U& s6 @! t
* <P>Title: pdf extraction</P>( e: J8 h# p( R* o% m! V1 u
* <P>Description: email:chris@matrix.org.cn</P>
% w+ a; }& h0 k8 }1 X) a8 l * <P>Copyright: Matrix Copyright (c) 2003</P>
4 z  t- c7 P% \ * <P>Company: Matrix.org.cn</P>
7 U; N7 S! c6 X * @author chris
" S5 i: Z% |5 a, K$ j * @version 1.0,who use this example pls remain the declare( j) L0 s: y% B1 r& `
*/
# |, G4 ?8 x% Q7 d( w8 [/ I& W
2 ^4 g! R9 {2 g1 Q5 J4 z
& g. C% F7 P: q$ o/ `public class PdfWin {6 s2 C9 M* y1 m# `
  public PdfWin() {
+ [2 ]# J  [( B, Y/ E, K  }
2 d8 R  J+ ?- m  public static void main(String args[]) throws Exception
, F5 x4 D' b* c7 p7 D1 c  {, u& Y' q+ O& R
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";+ o9 q  S4 g/ |6 K4 L
    String filename="c:\\a.pdf";: V: {' ~1 j7 S  J. {
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};" {; f" n  j' H2 y* [
    Process p = Runtime.getRuntime().exec(cmd);0 r  w/ x. k$ a$ E: B+ _
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());3 J( l' j) I6 c3 X  _4 h
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
8 U$ r+ i( L# `' s. ?. }2 M% |    StringWriter out = new StringWriter();. ^$ x2 u6 H3 U7 i
    char [] buf = new char[10000];9 r$ b* b) q: e& Y$ N, t  ?
    int len;
( ?6 t" G+ P# L- K7 U' A" `% V    while((len = reader.read(buf))&gt;= 0) {# a, j# X! m1 d  Y; t
    //out.write(buf, 0, len);! q, |4 G- r9 z) n3 Q1 F
    System.out.println("the length is"+len);- j0 z! o1 `3 z- Y
    }4 y/ E1 x) U( x- P% ]8 h
    reader.close();8 J/ O+ P  K$ u, U' D$ _2 b0 J4 q+ Y
    String ts=new String(buf);# ]9 g9 d: M, D
    System.out.println("the str is"+ts);
3 I+ Q. a, X7 I2 N5 U& J  }
2 V- T; t9 l6 F}
( A2 S6 o3 M* ^! b+ h8 m</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