QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4200|回复: 2
打印 上一主题 下一主题

[转帖]java抽取word,pdf的四种武器

[复制链接]
字体大小: 正常 放大
god        

206

主题

2

听众

882

积分

升级  70.5%

该用户从未签到

新人进步奖

跳转到指定楼层
1#
发表于 2005-3-31 01:15 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<BLOCKQUOTE>很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word,pdf的几种方法。</BLOCKQUOTE>
$ n9 K# Z' q0 Z- _3 f# p& r<><A>1 .用jacob</A>7 @9 P5 y# c& r. w0 G
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>) o) a8 J* y& X. I& c
<>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>- m% Y' U, L; G5 |6 o4 ]; D
<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>
5 A% @$ m. C2 N& W<>
4 Q& }0 V( r$ w: c( \<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
$ i: {& E9 N7 Z' K" x/ j. J0 E* {
9 _. T, a5 l" ?: G! Z& Z- x0 ^<TR>
. b0 W* C! m9 Q5 p5 g2 k<TD><RE><CODE>
7 ^; u3 ?/ x! I0 Q% kimport java.io.File;$ H3 |/ v, g# A+ o
import com.jacob.com.*;
. K2 o2 E4 B* g$ Uimport com.jacob.activeX.*;
$ A$ k4 V& g5 M4 o2 y, j  {7 _" ^/**9 @  B: p( H: }$ M
* <>Title: pdf extraction</P>  t+ }$ q# c# ]. Z; V' l0 a1 |
* <>Description: email:chris@matrix.org.cn</P>
# G+ S5 Q7 g* B+ |0 }9 @" i * <>Copyright: Matrix Copyright (c) 2003</P>/ M; N/ ^$ R0 x" u. ^
* <>Company: Matrix.org.cn</P>
7 {! g9 u3 y& D: Z- @5 ~& {; H2 U * @author chris
; N6 i  v( u: m8 t8 u; K- v * @version 1.0,who use this example pls remain the declare0 S. D; c  b2 m* x) p, Q2 p/ A
*/
$ r# Z, h. u+ b  fpublic class FileExtracter{$ o4 S$ D- f8 u
public static void main(String[] args) {
1 \+ Q3 r3 J: z  ActiveXComponent component = new ActiveXComponent("Word.Application");* f* K6 z4 A, X7 T8 P, s  q
  String inFile = "c:\\test.doc";) B9 v* K6 T, ?# U3 F
String tpFile = "c:\\temp.htm";! @3 P: \4 p) @1 z* F) q
  String otFile = "c:\\temp.xml";9 `9 c) ?9 M! @* f
  boolean flag = false;
5 H" _: D( G$ S" u  try {
2 m& i; G+ }: h' _  p* M0 z, T" G4 l   component.setProperty("Visible", new Variant(false));
! y- J1 I* C' t3 y. D   Object wordacc = component.getProperty("document.").toDispatch();" I; Z- K3 q" O4 W& o: V
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, ; X; w$ ^  G1 R
                                     new Object[]{inFile,new Variant(false), new Variant(true)},
- c( g8 V1 e, J% W                                     new int[1] ).toDispatch();
% ?5 b) h1 _$ k; [# z/ g8 m   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
2 Z% h& I" U; A) y   Variant f = new Variant(false);  l$ n8 b- o7 @3 B
   Dispatch.call(wordfile, "Close", f);
1 y5 s9 L7 ^2 b, A. B. k8 R4 Y+ o   flag = true;
4 Y# y+ G7 Y0 q" g. X8 S- L  } catch (Exception e) {
' _: h) _7 L% T/ G; R6 @# N   e.printStackTrace();
  V) b) J5 s& @" e- e+ J: i2 n  } finally {
  l  `7 G9 T1 I! |   component.invoke("Quit", new Variant[] {});0 ~0 e" x( p; M
  }8 v2 |' K* [+ C0 u
}0 U; v/ J8 e! d( i
}" }& X3 A# A- q
</CODE></PRE></TD></TR></TABLE></P>3 R! G- d$ W% _6 U; p2 M- K1 h- E
<><A>2. 用apache的poi来抽取word,excel。</A>
' @5 t' T1 t6 Y3 Q% Fpoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>. J3 R( c5 z2 R+ S+ g8 T' Z
<>下载经过封装后的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>0 _9 \+ [0 f4 l- H  e; W$ a* v0 J
<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>, Q. O' `5 D4 c( h* ~$ l
<>
* C' E1 I/ `* p; Y! z  G5 E<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
  _+ |6 _0 x4 g4 G/ R' ~
8 }# E% Z: f% D0 L. t- d) H9 {<TR>' f. Z3 D7 w/ x' ]9 A' o
<TD><RE><CODE>
# q3 a# i% u: C5 ]$ e# @) s- A1 Nimport java.io.*;( f; v6 A4 n! X
import  org.textmining.text.extraction.WordExtractor;$ [& n  c' u$ M8 \2 n) Q
/**
, r) c! g# d2 B, K * <>Title: word extraction</P>
* A: H: O/ S1 J6 Q7 t * <>Description: email:chris@matrix.org.cn</P>
! {0 e. R! G5 o+ w: S7 M * <>Copyright: Matrix Copyright (c) 2003</P>' U' F1 b& X9 K: q) q$ O
* <>Company: Matrix.org.cn</P>; M/ h9 `5 F# ?9 W. {1 [( g; Y  s
* @author chris/ m8 n7 V3 C  P
* @version 1.0,who use this example pls remain the declare
2 P7 O7 ?+ U; T! F6 T */8 w3 f' ]7 Y3 C  \/ X; e9 I7 p  |  ~
  x% r5 @/ D' p+ [" B5 t
public class PdfExtractor {
) T! ~6 B$ I$ L& ~* f, ]  public PdfExtractor() {, a! C: O% P' B
  }
' R9 I4 B- G+ r6 m. b  public static void main(String args[]) throws Exception
% Z" {' C0 s$ T9 [  m5 H& I1 \  {3 o' q3 R9 r) t6 ?
  FileInputStream in = new FileInputStream ("c:\\a.doc");3 F/ c1 _8 M3 Z- z
  WordExtractor extractor = new WordExtractor();5 l5 D' q# K% y" \* [9 H' `
  String str = extractor.extractText(in);
- m3 T6 b# I$ q- h$ f  System.out.println("the result length is"+str.length());* c" d% ~4 M( Y  \
   System.out.println("the result is"+str);
- M3 G. |2 ?5 ^" b  x}
4 }9 W0 G, j/ i6 v" E/ ~) `}; a: F) h$ @, P* P
</CODE></PRE></TD></TR></TABLE></P>( {7 f, D9 _! N) C7 A: V
<><A>3. pdfbox-用来抽取pdf文件</A>& d- Q- }% b6 f0 D
但是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>
# C7 F7 B4 d6 T0 @<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>) A, Q" ~# @6 U
<>  k' F  E& Y8 R6 d; \
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
" G/ T) k+ m+ K2 @, g/ V. f' k: ^1 I0 |6 g- w' J
<TR>
' H, f2 [- ~9 w0 O: w3 S, T5 J# z<TD><RE><CODE>
$ ?# V7 m; f! E9 ?8 y! {! l; V$ K& himport org.pdfbox.pdmodel.PDdocument.# S! F- |1 g) j9 G2 s
import org.pdfbox.pdfparser.PDFParser;
1 ?. Y. @6 c/ p, Zimport java.io.*;8 X' c2 V5 V) `/ f/ Z
import org.pdfbox.util.PDFTextStripper;/ R  ?7 B: E3 l6 K# @' W1 X
import java.util.Date;: U9 k# W8 ?6 I# g
/**
7 H: h( i! I6 r# D% U5 E& }! B * <>Title: pdf extraction</P># K2 a" {0 N( J- B, L  g8 ~
* <>Description: email:chris@matrix.org.cn</P>
8 j1 f! u  ?2 t% Q * <>Copyright: Matrix Copyright (c) 2003</P>: `9 J" H! K3 S
* <>Company: Matrix.org.cn</P>* O1 `4 r4 S  D: y, H  S: [* C( U! P
* @author chris9 v3 w4 d) L& n0 z) R
* @version 1.0,who use this example pls remain the declare
6 U( p( n% R0 D7 [5 }$ | */
4 J) z  `( [! J2 d, U! r. C5 E
% a8 a1 [( J2 t; u' {  Bpublic class PdfExtracter{( H* N4 ?; a8 i+ k* Q+ H

' {4 X8 E. V# B/ A! Zpublic PdfExtracter(){
9 a: \+ I% I& G  }
+ o- n# F) a' r" ?public String GetTextFromPdf(String filename) throws Exception% i8 b2 e5 @5 N4 f
  {! y0 I: X! T# [
  String temp=null;
* n  ~# J8 s: X# C7 h  PDdocument.nbsppdfdocument.null;
9 @' k2 k' _* j  FileInputStream is=new FileInputStream(filename);
+ c8 {2 Y5 _" R1 g; d  PDFParser parser = new PDFParser( is );% @' d% `, m1 Y5 H
  parser.parse();/ b" C8 x4 ^* p% W
  pdfdocument.nbsp= parser.getPDdocument.);2 p/ U( n, {7 a1 N! @
  ByteArrayOutputStream out = new ByteArrayOutputStream();
! U9 K3 \6 Z: {/ i3 I8 c  OutputStreamWriter writer = new OutputStreamWriter( out );6 b* P3 E% f, t
  PDFTextStripper stripper = new PDFTextStripper();1 W( F8 O6 b% D4 H2 k0 F7 ~
  stripper.writeText(pdfdocument.getdocument.), writer );- l$ R) k8 s0 j" s) h0 u4 L
  writer.close();
, t; o4 X9 _- V% h  byte[] contents = out.toByteArray();
. t; O" y& ~4 B9 s4 _
. ?7 ]( e6 l! Q- B" h& S  I  String ts=new String(contents);& x$ \- |" l3 S' I, V6 x3 }4 j
  System.out.println("the string length is"+contents.length+"\n");& j, v: u3 t# k8 G. @. F+ @- Z
  return ts;9 J8 b7 L* Y" S9 y
}- W7 n: W4 ?! @" Q
public static void main(String args[])
# S$ D" m0 t' p{9 v9 U7 t& C5 m; j# ^! C
PdfExtracter pf=new PdfExtracter();
% h0 m7 z& m6 LPDdocument.nbsppdfdocument.nbsp= null;
1 M7 x8 H2 T4 w/ b4 F8 D1 m. r6 n2 @4 c  J9 w$ F' ]8 ?
try{
3 H  l1 y- M5 q) }# J/ KString ts=pf.GetTextFromPdf("c:\\a.pdf");
' D( E& |8 B2 E- B: u; QSystem.out.println(ts);' T5 K; Z. a8 T" V2 e% t
}
1 R7 g: i; r4 X: Ycatch(Exception e)
" D" l8 c1 a/ E9 O9 |4 \7 W  {
; |5 D( v2 K' m9 m9 x: y3 N% X  e.printStackTrace();- k* X+ }; V7 o/ J0 m& _6 U
  }
! H  w  y4 y4 w  E' b: s/ l5 {}
% w% l  L* j& n+ L, g5 u6 ~" y4 Y1 _' a2 X2 T1 O
}' I% t' K8 y5 \
</CODE></PRE></TD></TR></TABLE></P>
1 M/ ?3 _. U  b+ z7 S9 Y<><A>4. 抽取支持中文的pdf文件-xpdf</A>
% k3 Z6 Y0 t! e( s5 u: gxpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>" x- c2 h- T7 Y# q# h4 p* x
<>下载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>3 C2 d4 P- T2 p1 |( V
<>同时需要下载支持中文的补丁包: <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>. B8 M, y7 ^3 g
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>
2 a& M5 B- J7 E. X<P>下面是一个如何调用的例子: </P>
% U; C6 w1 ~0 K& f+ ?  ]<P>. p: S4 X6 X/ ]  }7 h) b
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">. T/ g) C4 R: J6 u; H
) E  O; O1 ?" M1 I9 A
<TR>. X0 D- f2 f. c* p/ ?
<TD><PRE><CODE>( L5 S  h0 E! l% c8 v* R
import java.io.*;" t+ I) V0 V2 X" t; B
/**
  G! Z1 p: l1 A  k* A+ @ * <P>Title: pdf extraction</P>
& R/ c6 F/ \% R/ C5 q* o+ n * <P>Description: email:chris@matrix.org.cn</P>
4 N  C. I: U" I' k8 A% T * <P>Copyright: Matrix Copyright (c) 2003</P>4 h# f, `3 u7 t8 ^
* <P>Company: Matrix.org.cn</P>
0 Z; f. V8 x8 C+ @2 M# r * @author chris1 J! z5 f) f+ h+ O+ w
* @version 1.0,who use this example pls remain the declare* c+ T( P, O/ P0 i9 y* H
*/
. W0 ~2 E/ |3 V* c3 v4 c8 \
+ c& `  z( _3 a5 o/ ~6 P& ]6 Q3 a, ~5 p+ d0 z1 p. Q. V% J( |) _
public class PdfWin {- j( w) `; Q4 v  m
  public PdfWin() {! E. x/ ~* h% S- m% w8 Z
  }) v0 K( \7 z  g* w) Y8 D
  public static void main(String args[]) throws Exception
, o9 Q& Q2 U0 i  {
! m/ N& M1 e5 T# R8 d    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";, |, x( z6 [, y/ Y/ {8 N
    String filename="c:\\a.pdf";+ @$ R6 c* ]6 @
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};& z/ {3 V* i2 y2 ]* a3 {8 s9 y+ }
    Process p = Runtime.getRuntime().exec(cmd);1 @4 K: ?5 ?$ P* H; L5 O
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());; I" T! T8 R: Q; _$ i5 v
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
3 b* }1 e" k* ^5 ?& |5 }    StringWriter out = new StringWriter();  w$ A3 `' ^4 k/ i
    char [] buf = new char[10000];
# K0 d3 s# ]7 g8 H0 c( g3 k, K7 ]  w    int len;) ^: e# u: c9 e& y
    while((len = reader.read(buf))&gt;= 0) {
# v$ A5 n/ \7 O6 L    //out.write(buf, 0, len);
8 e% ]8 h4 }6 a# T( o% r    System.out.println("the length is"+len);' u# F" k7 w) F9 W8 S/ [' L
    }8 k" |- \' n% j: Q
    reader.close();0 ^: e, l3 P7 N; l& ^5 B
    String ts=new String(buf);6 Q5 ^3 t& f1 l9 Q
    System.out.println("the str is"+ts);
  {& |5 a$ h, g/ V7 c/ g0 J9 h  }$ v4 D" H: }( w- p6 Q
}. X3 m" `7 w' [3 k8 x' a% j
</CODE></PRE></TD></TR></TABLE></P><!-- RESOURCES--><!-- AUTHOR BIOS--><!-- Make author heading singular or plural as needed-->
zan
转播转播0 分享淘帖0 分享分享1 收藏收藏0 支持支持0 反对反对0 微信微信
如果我没给你翅膀,你要学会用理想去飞翔!!!
39133120 实名认证       

10

主题

4

听众

1045

积分

  • TA的每日心情
    开心
    2014-9-6 09:56
  • 签到天数: 209 天

    [LV.7]常住居民III

    自我介绍
    希望和大家交流学习!

    群组小草的客厅

    群组数学专业考研加油站

    群组数学建摸协会

    群组数学建模培训课堂2

    回复

    使用道具 举报

    3#
    无效楼层,该帖已经被删除
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-4-19 14:40 , Processed in 0.423810 second(s), 63 queries .

    回顶部