QQ登录

只需要一步,快速开始

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

[转帖]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>
6 _8 D2 {3 @9 U8 R' n# Z<><A>1 .用jacob</A>
# s  w- X3 ~$ n# m! ^1 `6 `其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>
* u9 N; l5 m1 p, ?/ D<>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>
9 }. H; z, O' O  i) H1 k<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>9 d. a: g, F1 T4 I+ f, C
<>. J; g( z' m$ Q) @; J
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">3 O2 W, i; i" ?1 O) O4 J* o9 v. O
7 H8 w" L+ A- e4 t4 ]
<TR>
' N  E; T0 V. m0 X% p9 d. A, G6 u<TD><RE><CODE>
& s: `) O, I, A5 [import java.io.File;
7 ^9 `8 }/ |) P5 s% Gimport com.jacob.com.*;: |5 M  A8 r/ ?
import com.jacob.activeX.*;* X+ ?' @  u7 R
/**8 G2 M0 L8 f. A8 m+ ^! j
* <>Title: pdf extraction</P>
" O# h( a' b% ?. u4 m) U8 Z' S8 t$ Z * <>Description: email:chris@matrix.org.cn</P>
7 ?: @3 |7 c6 ]" P2 H * <>Copyright: Matrix Copyright (c) 2003</P>! Z8 u+ B, L% ?1 r! ]/ {2 \
* <>Company: Matrix.org.cn</P>
; Y  S/ F$ z* p2 x1 O * @author chris2 e% @: b4 @0 h& ^' Z! R7 R
* @version 1.0,who use this example pls remain the declare
* G; g- R% P$ K) i6 _3 B4 H# R */
0 s5 w8 s- |' K! M& {public class FileExtracter{& F3 A: _0 P0 J* ]) ~) t# w1 ~
public static void main(String[] args) {1 p4 s% i4 D" R4 T8 j; S' _+ Z
  ActiveXComponent component = new ActiveXComponent("Word.Application");0 n7 e/ l5 Y2 p2 e
  String inFile = "c:\\test.doc";
% [6 e+ d. B& {& C+ y4 X4 f% s+ ~ String tpFile = "c:\\temp.htm";& W3 _; _' o/ r; r+ V2 v. o
  String otFile = "c:\\temp.xml";
7 t) c+ L' q! S4 N- F" R3 N# G  r6 U  boolean flag = false;
3 R0 W: w+ X7 s# r; R7 _2 \  try {4 V9 O2 \7 p5 x; T9 X! F$ e3 e0 D8 U6 H
   component.setProperty("Visible", new Variant(false));- I  F# s, Y1 H
   Object wordacc = component.getProperty("document.").toDispatch();6 w1 ]  W& T& L0 d; b  ~0 B
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, 2 d8 w* A$ y9 i
                                     new Object[]{inFile,new Variant(false), new Variant(true)},; w" r# X' l+ C/ @( ]  @
                                     new int[1] ).toDispatch();
" p' l/ T" m" [; Z   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
$ p6 w) J" K; o, o   Variant f = new Variant(false);. B7 _  k. o) Y, r# K
   Dispatch.call(wordfile, "Close", f);
3 y$ h" G1 {3 r( a9 i& d3 _   flag = true;
5 M' b: i. u' L8 D8 T+ _! c  } catch (Exception e) {- O+ U# G& }8 x. }" Q: Z7 }0 ^
   e.printStackTrace();6 y- E/ T0 Y) `& H" n
  } finally {+ w2 d: _' \5 W% e$ N
   component.invoke("Quit", new Variant[] {});
7 s& }* ^$ r& O0 \5 }  }0 l1 S0 u0 ^9 h6 {$ i
}
0 {8 F$ h6 S/ W$ h}3 A" ^0 U' L! c
</CODE></PRE></TD></TR></TABLE></P>2 W% ?/ q. e0 v7 [- x! ?6 |
<><A>2. 用apache的poi来抽取word,excel。</A>
: e+ K' S) |; z4 Jpoi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>) G; s5 m7 M! r! J6 v
<>下载经过封装后的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>! \! G' S, z2 e4 f. x
<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>* \" z6 b' T. o1 E  X( M
<>2 J6 b( M2 s4 y5 Z& H/ m1 a$ C" B
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">7 n. _9 z9 ^: ]) Y" n
' E/ N9 H5 f$ A/ k/ p# G1 L9 ?
<TR>
' E, s% c0 N3 ~( e! {* p& i<TD><RE><CODE>
8 `& B9 C8 k; c0 i+ x6 |import java.io.*;! D) s9 m# g; B; \( S  L
import  org.textmining.text.extraction.WordExtractor;
2 P% L, W  |5 N, x) w/**5 D4 \9 L3 n8 X/ \2 Z
* <>Title: word extraction</P>* J# [( u9 d. B1 o. R
* <>Description: email:chris@matrix.org.cn</P>9 ]: R: H" N4 e6 j% p
* <>Copyright: Matrix Copyright (c) 2003</P>  X0 F3 ^5 Y4 Y4 ?" l
* <>Company: Matrix.org.cn</P>
; w9 B) I# p! E) R * @author chris
1 \- A9 A( W& i * @version 1.0,who use this example pls remain the declare, q5 E6 ~) n3 ^8 b0 r; J  B/ |
*/
4 Q( M& D" A) \1 `& H: c: h1 ?7 ?0 e4 D2 a6 f7 a' ^) k8 v% k
public class PdfExtractor {
* ?9 n; R9 Z8 d8 T7 X, }7 m  public PdfExtractor() {
7 M. s* h! u& o% L1 f  }$ b2 d' q4 \2 s
  public static void main(String args[]) throws Exception: y7 |9 H3 [8 H7 f6 U: |" c
  {7 o) }+ c/ i7 e) T4 ]
  FileInputStream in = new FileInputStream ("c:\\a.doc");/ p% n# `( V. I, o
  WordExtractor extractor = new WordExtractor();. H  a: T$ c7 F9 K2 M% x
  String str = extractor.extractText(in);
; P- H8 F8 ?) p4 z7 U% |  System.out.println("the result length is"+str.length());, G7 F3 n$ o9 y1 D6 ]) u
   System.out.println("the result is"+str);
  f7 j  E  n$ s5 I. `! m& Z  v}1 O8 E4 u$ K$ `  P2 s- u) m
}
7 I1 e6 F8 P9 z9 S6 R</CODE></PRE></TD></TR></TABLE></P>
6 {/ H7 u* o  c, M" i1 ~<><A>3. pdfbox-用来抽取pdf文件</A>
% V0 I& M' N9 E: |+ X2 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>0 F! r6 N% d* ^" V0 D/ j$ }) ^
<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>
9 N" ~' R. d: m2 |# ]<># W" j+ |1 b, k  O
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
8 S5 T4 q: F( L" ]1 e& |# s% J' W+ p$ x, e8 v
<TR>6 C0 }( g0 j" `
<TD><RE><CODE>
1 o8 [4 l+ T, q) A( Wimport org.pdfbox.pdmodel.PDdocument.
, r' a/ t  K7 I5 f  Cimport org.pdfbox.pdfparser.PDFParser;
. {- B- n/ y% f: zimport java.io.*;
& h/ |- M* _" h; k, y  c- L3 bimport org.pdfbox.util.PDFTextStripper;/ H& @3 y, R( a  u2 R9 p2 V
import java.util.Date;
/ W2 V5 Y: L- |: G( J7 b0 Z3 L/**
- D# X( A: l& U) G- h8 B * <>Title: pdf extraction</P>5 S3 m0 n) O& G8 d; L8 ^
* <>Description: email:chris@matrix.org.cn</P># W3 r  X' b* J1 m
* <>Copyright: Matrix Copyright (c) 2003</P>3 O2 i9 `1 n* [, N4 X- W* W
* <>Company: Matrix.org.cn</P>
0 a5 E1 ~) o- `: s' C! f: Q * @author chris
6 y4 n9 ~2 f- j, [* y- a( X) X3 Z* m3 n * @version 1.0,who use this example pls remain the declare. A+ n. Q, b0 c+ u& X
*/
1 T3 K3 _# M$ [- s: l( \$ i
# ~- E5 L5 {0 Q# ?5 @' Lpublic class PdfExtracter{1 _9 }" s, O. `8 ^) P+ o) ]

2 U, d- h' F$ g3 K9 h, b2 _. z9 w+ Mpublic PdfExtracter(){1 y& S% l, ^9 U8 \8 m' K
  }
# L  O3 z" P; ^) }4 Vpublic String GetTextFromPdf(String filename) throws Exception
" P7 d) D& _2 \' J5 u  {; f: x( ^- r. _  [
  String temp=null;# r2 Q( g5 X% h6 e. J8 }! F7 h
  PDdocument.nbsppdfdocument.null;! o; O& l4 _% i2 H
  FileInputStream is=new FileInputStream(filename);
. Q' j& Z  A: V, j  PDFParser parser = new PDFParser( is );/ S0 x: o  I) b  \. W2 e
  parser.parse();
' m, U5 q6 ?/ Y3 A- a  pdfdocument.nbsp= parser.getPDdocument.);/ t+ t! _( x7 }) b: p
  ByteArrayOutputStream out = new ByteArrayOutputStream();3 W4 H, L* i4 L+ y3 ^) _+ w/ i
  OutputStreamWriter writer = new OutputStreamWriter( out );8 ^/ K; G% n/ x6 S2 C! v1 F7 V
  PDFTextStripper stripper = new PDFTextStripper();; I/ @3 J4 y9 D. W4 z
  stripper.writeText(pdfdocument.getdocument.), writer );, H1 M5 d1 [9 ]* S, |6 {, a- [$ t
  writer.close();4 A7 n$ c/ ?9 m0 t
  byte[] contents = out.toByteArray();
% U) O5 P, S% }2 ]
$ k- I6 n1 S. c8 W/ B  String ts=new String(contents);
0 B; l& c3 t/ |* j+ v2 d, _2 I  System.out.println("the string length is"+contents.length+"\n");
& U0 Y7 s1 A4 `7 ]  h6 N  return ts;
* }3 [4 Y* |# N" k# t$ m}: y# n5 D2 P1 [( k
public static void main(String args[])
  f2 C7 I6 [+ z  O# q. l  G& @{
  T* }2 T- J/ u' f5 H& K' BPdfExtracter pf=new PdfExtracter();8 n" X) i8 @; h5 X
PDdocument.nbsppdfdocument.nbsp= null;& K4 H' u7 l9 j' b' {, t

* A/ O3 [) ^% A) U* w7 \& `) r6 dtry{- u) {$ K, \& S" ?8 U6 ]: W+ a! h/ Q
String ts=pf.GetTextFromPdf("c:\\a.pdf");) l, q6 ~/ D+ J" C
System.out.println(ts);9 f) B1 o& m  x# v- |
}
3 ~) [2 E9 w7 [9 `) x" U4 Ncatch(Exception e)
7 Q$ `; W1 M0 V$ Y5 P9 `  {$ J" E; B9 U  g/ N* w
  e.printStackTrace();/ [* F3 t' {: A8 D; n) q
  }+ A% H* A/ k8 n# r* R  s5 w+ t; g
}3 D, q* r  n& i" Y8 O7 }' x' t

4 V& V$ L# f9 j0 }1 J. L}
. T4 M8 b$ ?& N. C</CODE></PRE></TD></TR></TABLE></P>1 Y( E5 P# T. }7 y% t( P  P
<><A>4. 抽取支持中文的pdf文件-xpdf</A>
; Z' e3 t3 i" o" A7 p+ k' G' rxpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
. [. l* R' Q6 V7 U% ]& a" P<>下载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>
0 I0 }8 h5 R; n* j! m<>同时需要下载支持中文的补丁包: <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>2 B# G+ }! Z% ~
<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>: w, l2 @- t* E7 B
<P>下面是一个如何调用的例子: </P>; e8 K3 ?1 [& H; h
<P>% I4 ~. l! S; i" K5 }3 r! R: D
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
- Z5 I' Q* g# b( b/ F' G; l9 G
) W# A. G1 H% S<TR>
5 q9 b7 P6 J, i+ P4 j<TD><PRE><CODE>8 L2 B0 K7 x7 P1 W6 M
import java.io.*;, A" }* N! x% l; S; l4 U  l2 i
/**
2 r- b! K  W' v( e$ Q4 F; e$ q * <P>Title: pdf extraction</P>
2 X( W6 r4 B+ }4 U8 W( x * <P>Description: email:chris@matrix.org.cn</P>! ]; p7 }4 j/ z/ `8 p  k2 C( S  n- k
* <P>Copyright: Matrix Copyright (c) 2003</P>
4 e! A! j  U# G- Z! k2 B6 d+ d * <P>Company: Matrix.org.cn</P>$ K7 |) M" w- O; n3 X
* @author chris
0 Y( {) I: s7 t) p( B$ p * @version 1.0,who use this example pls remain the declare6 T9 U# t  l3 ]
*/7 p5 g" g, T  X+ s

$ `8 r! {! s* u8 ]3 I, Y
. r2 r" w' ^/ @2 Opublic class PdfWin {! k3 J9 b+ S, z" P( f
  public PdfWin() {
6 k. {! B: j6 N' z8 m0 r1 m  }
3 h' `/ E! P) @; ^7 J8 {# l: j  public static void main(String args[]) throws Exception8 E. \! N6 |+ v. u( R
  {
' {$ {1 M$ m, D, {  c    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";0 b7 ]1 ~; O% C( E; B
    String filename="c:\\a.pdf";0 Q8 {' P  _4 `1 }2 ]: b6 y
    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};0 L# W+ Z. ~& N. Y# I
    Process p = Runtime.getRuntime().exec(cmd);4 a! j# e: u) F; g
    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());8 c1 I% l/ ?1 v2 r' J) v
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");9 P- j) d* u: ]( s, E& ^
    StringWriter out = new StringWriter();
4 D* L1 k7 Q# `, c3 [: z    char [] buf = new char[10000];; z9 ]" P, C) ?# f
    int len;* Q0 w5 p3 H8 L& y
    while((len = reader.read(buf))&gt;= 0) {" u; ]& W# B2 {' t) A0 s! B" \2 F. E
    //out.write(buf, 0, len);
) w! k$ N5 O$ |, U) b# h& u    System.out.println("the length is"+len);# W. M8 n7 ~- [5 t
    }* w6 ^' T2 ?  Z: m3 p, K/ m8 w
    reader.close();
1 k7 Z. d; h& l' ?+ p    String ts=new String(buf);5 z+ f7 v; @: {$ W
    System.out.println("the str is"+ts);4 J) k& M: @/ Q% `7 Z- B$ l4 x0 @
  }( B5 i- _* C& S# Q
}
& P1 L) K) Q" I  K5 V( ]( a</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

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

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

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

    蒙公网安备 15010502000194号

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

    GMT+8, 2026-6-15 01:48 , Processed in 0.443687 second(s), 55 queries .

    回顶部