QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 4234|回复: 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>3 B) A; Y6 P0 R# k% |
<><A>1 .用jacob</A>
! T5 c$ E# E" d; o# j其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。 </P>( d6 x" _1 k7 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>
, W" {( N; g0 m. p5 y! \<>下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个简单的例子: </P>5 q7 ~( K8 O) y, g& v
<>5 V7 j+ F$ T# M9 A! }/ t
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="80%">
$ ]* V. A; P$ P5 B5 r) L+ q2 _7 }$ S1 k4 l9 O; I, m7 Z. |
<TR>4 x+ c3 b/ s. p; K* ?, \
<TD><RE><CODE>
0 b: E& r9 ?8 m5 w; s# ^5 x6 qimport java.io.File;
1 \/ C9 M2 m9 e/ \$ Bimport com.jacob.com.*;
# C2 X1 w* o3 z: _8 q* N, I* Gimport com.jacob.activeX.*;9 G7 T- u6 @* ?) |
/**
4 g8 t1 f3 G& a8 M6 L6 P  z0 C1 m+ _ * <>Title: pdf extraction</P>) T7 O; F# x; |) a  K
* <>Description: email:chris@matrix.org.cn</P>
6 I2 i/ w$ ]5 ~" x1 H * <>Copyright: Matrix Copyright (c) 2003</P>6 \1 b; e( H" ?$ T: n$ c
* <>Company: Matrix.org.cn</P>
1 l* t. ~0 Z, i% n- X * @author chris
" S4 e1 A- L  a7 b5 b/ v+ R# x * @version 1.0,who use this example pls remain the declare$ u5 }# B% i; b. O" a- S! B
*/
2 h# Z* W. B( g& c0 Y3 q/ vpublic class FileExtracter{
# q% U2 l) I8 g* k6 Q' g" u; t5 W public static void main(String[] args) {
* P+ Z! z3 T7 p- v  ActiveXComponent component = new ActiveXComponent("Word.Application");' D( ], n0 d+ Y% p% w
  String inFile = "c:\\test.doc";# }: l( d5 C4 n$ `+ K# Z0 K, r. O
String tpFile = "c:\\temp.htm";
$ x  O$ x4 `7 C9 a! _3 T  String otFile = "c:\\temp.xml";
" X2 z, L9 h; P# d$ }# h  P  boolean flag = false;
5 W1 P. X$ o1 X% B  try {
/ u( |/ q5 C/ s* {6 u9 m   component.setProperty("Visible", new Variant(false));
4 k- s9 R/ C& w, P( u   Object wordacc = component.getProperty("document.").toDispatch();2 e8 s0 h8 d! t! v) Z. j/ N  @
   Object wordfile = Dispatch.invoke(wordacc,"Open", Dispatch.Method, ' P5 ]  P) v% j9 d2 ]$ E6 K5 v
                                     new Object[]{inFile,new Variant(false), new Variant(true)},4 k+ R. E+ m5 ~% v
                                     new int[1] ).toDispatch();' E) L; B* k" J( m
   Dispatch.invoke(wordfile,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
& R: m: N  N5 r1 w5 F4 ]0 g: G   Variant f = new Variant(false);5 B$ G% m1 P6 |9 p* d" R) S
   Dispatch.call(wordfile, "Close", f);
9 A9 G; F5 T6 d& G4 l' \& @   flag = true;
9 l4 `3 b0 E# v/ ?  } catch (Exception e) {$ X0 u2 O! d' A/ t, D
   e.printStackTrace();1 L3 Q( O0 O& B! @; z/ j( B
  } finally {8 x6 s  I# N  y
   component.invoke("Quit", new Variant[] {});% C( |- P0 L: b- {5 H2 P
  }6 Z( g, H2 y$ u$ q# a) @& f
}
5 t+ Q3 M" x% ?6 q9 k; R}
8 o$ l4 a: H: i: `% u/ Q4 m9 z* `  x, B</CODE></PRE></TD></TR></TABLE></P>) f' X5 H  _. d
<><A>2. 用apache的poi来抽取word,excel。</A>
3 l( a% ^/ `. J  }3 {poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你: </P>
( Y8 p: e( J" K- T5 J<>下载经过封装后的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>
/ r2 u9 q8 @* F8 Q$ X<>下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子: </P>1 U4 P+ _  a' F, \9 I0 h
<>
% T1 a( A+ r3 b: ~<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
9 h: M$ X8 Q$ I2 S- z8 R% t
3 F; z) M. D: v0 l* Y<TR>/ j# |' J- G4 L' O+ u/ Y
<TD><RE><CODE>) }, R+ Z. k8 E5 J, _' m# a
import java.io.*;
6 U/ B2 W4 U# S. iimport  org.textmining.text.extraction.WordExtractor;
0 D; w. A& n7 v- }/**
  c8 W* d5 ?2 ]% G2 @ * <>Title: word extraction</P>
0 l3 q: h. z7 U4 n4 X4 ? * <>Description: email:chris@matrix.org.cn</P>- J+ U: q6 N! M& I6 \! |
* <>Copyright: Matrix Copyright (c) 2003</P>' i6 [1 R4 c7 x- p- }" V/ D( }
* <>Company: Matrix.org.cn</P>; b' d9 r4 u4 Z+ ~
* @author chris4 p; _* S6 K# B- I0 M5 ?$ ~0 J8 Y9 U
* @version 1.0,who use this example pls remain the declare
' u# C5 W, l  G! a7 a8 ]4 u% ?  W */( J& U7 j+ e% z- W/ e: B

  {8 T2 L% m( E) z, q( jpublic class PdfExtractor {
5 S1 \* W7 ~, R6 i4 A1 M. o  Y  public PdfExtractor() {
: K& }( p9 P) b9 l2 l0 l  }
, {; \$ g5 B2 e. x, j, z8 b2 A( {+ I  public static void main(String args[]) throws Exception/ ^0 f9 h$ `- {$ C1 V
  {+ ^$ K' [5 ], ^- ^
  FileInputStream in = new FileInputStream ("c:\\a.doc");
$ B# N: \! _% m+ V  WordExtractor extractor = new WordExtractor();2 H5 S# g# ]' j" A: n7 b
  String str = extractor.extractText(in);
# ~$ {  `" M5 z" M3 b  System.out.println("the result length is"+str.length());  G) K+ l: b  f) J* r$ M9 C
   System.out.println("the result is"+str);
& n' S8 j% ^4 G) J) A9 D2 N}, Z- t" n* `' S  n  R4 f5 h
}* N2 k$ \" G$ a+ S) z4 ?" v
</CODE></PRE></TD></TR></TABLE></P>; u- D$ @. R5 T% t0 D* `0 w
<><A>3. pdfbox-用来抽取pdf文件</A>" V! s2 ~$ D$ u  i6 q  ~& v
但是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>9 c; v2 R/ S4 g2 G+ o1 |
<>下面是一个如何使用pdfbox抽取pdf文件的例子: </P>
  A% H# h5 S4 `/ U- s<>+ x# r8 I' e/ m3 c5 d) H
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">  Z2 T$ [0 O0 g8 B3 x$ _' D
  `& \+ J0 G4 M. g
<TR>
" F! {0 E) r  V( Q: }" |' k<TD><RE><CODE>
4 H! }, L2 c- Q4 E! pimport org.pdfbox.pdmodel.PDdocument.
  z1 \, Y* h$ B; K- k# ^1 Q# Jimport org.pdfbox.pdfparser.PDFParser;0 t7 B! v+ y: t; H; T  Y; ~1 ~
import java.io.*;7 B6 b0 U& b* H. ?  U, j
import org.pdfbox.util.PDFTextStripper;3 d  O1 m+ R5 p+ W. t8 g
import java.util.Date;8 t5 S9 d# |; f1 y8 o" e$ [
/**/ V* B5 d8 w  ^& i9 c, q$ q* N% [8 i1 c
* <>Title: pdf extraction</P>
, f6 }1 A- P$ k  Y' \' r' s * <>Description: email:chris@matrix.org.cn</P>
' P* z' q( }- H% `$ X+ s * <>Copyright: Matrix Copyright (c) 2003</P>" H9 h1 J1 G$ q9 Q
* <>Company: Matrix.org.cn</P>
7 l. O5 m) h* [- ~9 C * @author chris1 D: I" I" }0 ]8 d: |7 B
* @version 1.0,who use this example pls remain the declare
) D0 A% T( R0 J9 r6 k$ f */: z0 s' @1 G' w5 l  j' J

/ }. M( H9 u  w8 h; xpublic class PdfExtracter{
: ~/ c, q* L- n
, ]8 Q; {  l: o8 O, O2 ^3 Mpublic PdfExtracter(){7 a3 S/ ~. M) k2 S" M3 [
  }
* G7 ]7 T9 y+ S  ypublic String GetTextFromPdf(String filename) throws Exception/ @& {& b: h* i! x: {
  {
8 @% c& I4 l: Z; U! B1 i) f5 X% Y  String temp=null;
5 n( i& `7 }% l  PDdocument.nbsppdfdocument.null;$ r, u5 q4 o1 `5 q3 {: V: G/ ]$ r
  FileInputStream is=new FileInputStream(filename);( R* D6 E$ a% ]3 N2 R
  PDFParser parser = new PDFParser( is );
3 X) Z1 w5 F& ~1 M  parser.parse();- I$ ^% e- c$ U5 F$ R, W2 T
  pdfdocument.nbsp= parser.getPDdocument.);9 J% @9 T; t* C8 q% ^% `! g1 P
  ByteArrayOutputStream out = new ByteArrayOutputStream();
; c* u' y% }, M  OutputStreamWriter writer = new OutputStreamWriter( out );
4 B+ v* A: F5 m/ z  PDFTextStripper stripper = new PDFTextStripper();, V3 t3 i/ ~, h$ P0 i/ F4 r) N
  stripper.writeText(pdfdocument.getdocument.), writer );6 m5 z+ n3 U  H* [
  writer.close();
, m" N# b5 Q4 f9 u  byte[] contents = out.toByteArray();
9 T  U8 l8 [4 P0 r2 m, B" j5 b3 c; V0 z, Y& Y3 ]  j& [8 g" s
  String ts=new String(contents);
4 k8 c" x" t7 d  System.out.println("the string length is"+contents.length+"\n");& z1 ^* T) _4 V& E% v1 d
  return ts;' L- K0 `; I: C) {- k
}
# m7 v- [/ ~$ g' ?! g# opublic static void main(String args[])6 W' x% T1 g! w9 q
{
- E# |6 q$ X/ q1 P; [PdfExtracter pf=new PdfExtracter();
4 x) w+ h3 E0 u/ [% [' TPDdocument.nbsppdfdocument.nbsp= null;
7 Y/ z; C3 R" z& t4 f% Y- F4 _. L; E. F+ y3 P  i# U
try{; J$ M+ g) h: J  Q2 {1 a/ B
String ts=pf.GetTextFromPdf("c:\\a.pdf");
" m. x8 @  l4 R. j9 aSystem.out.println(ts);
" J! C, f4 g: G, f! |( q}5 Y0 B/ T, t1 V6 j4 f
catch(Exception e)7 `  N2 o7 R- w  u/ A4 [
  {
+ A) ?2 f* ?2 U# r& O  e.printStackTrace();
6 s9 Y4 \2 ]+ P# L0 G  }
4 w6 j* G; ]1 n" Y. V6 Y5 }$ b2 O}
: v6 K2 L6 X9 G
8 ?; C4 `- e4 G}% A* s9 {# g# b+ S0 U4 P
</CODE></PRE></TD></TR></TABLE></P>2 ?+ j: T, O/ d: o0 n
<><A>4. 抽取支持中文的pdf文件-xpdf</A>- g% z& V; v: X) ^+ f, u4 q
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 </P>
  u5 d, g! O0 W: p7 e) ^<>下载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>
1 Q1 F1 a0 |& ^. Z) |( T. i" x<>同时需要下载支持中文的补丁包: <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>
4 I. Y, p- p' R3 A<>按照readme放好中文的patch,就可以开始写调用本地方法的java程序了 </P>
8 ^) R  I8 G  X! A7 f# p1 ~: ?<P>下面是一个如何调用的例子: </P>) P7 D  _, L! o8 ~
<P>2 B) B( E8 C* s( p; ]" t: Y& r8 M
<TABLE bgColor=#cccccc border=1 cellPadding=5 cellSpacing=0 width="100%">
1 k- X" h# J' o+ N4 |; Y' R" i& O" ?9 z! q1 @) L- n" _
<TR>, N3 m/ s4 Z8 V; Q
<TD><PRE><CODE>/ ~9 q, k6 g3 w% L
import java.io.*;4 O" ~5 b/ O5 ~( b
/**
& @. B, Z# x' R. z0 U * <P>Title: pdf extraction</P>1 W8 _$ x4 l, y5 L4 J) d
* <P>Description: email:chris@matrix.org.cn</P>6 X7 N( t) B7 r
* <P>Copyright: Matrix Copyright (c) 2003</P>  O3 h1 I) d7 T
* <P>Company: Matrix.org.cn</P>! D/ c' y' ^$ n7 ]  B6 I
* @author chris9 Z! M9 g8 @- t/ \% r
* @version 1.0,who use this example pls remain the declare* M: k' Q5 m7 b. G
*/9 W8 A" g# x* u' o$ }9 U; {9 G
# @& d9 \$ P, I6 ]

! i2 u( E5 P# Q/ Fpublic class PdfWin {7 G. o1 h1 d( ^8 M( Z5 z5 ]
  public PdfWin() {
, S9 _: z+ j# x/ Z  }
+ K" j; f. b* b) }& q- v& M  public static void main(String args[]) throws Exception
3 e  @/ B  W3 \0 Y7 u0 J  {9 ^  D1 ^. q' Q" M8 T
    String PATH_TO_XPDF="C:\\Program Files\\xpdf\\pdftotext.exe";
3 _) b  h, Z( ~; K; \& x6 A    String filename="c:\\a.pdf";
. k5 n9 F) t% C( u, P3 Z8 s    String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};1 \0 f5 E! T* N6 q. z) T+ n: e3 {. w
    Process p = Runtime.getRuntime().exec(cmd);
$ K  ?! y9 s  ?* e1 `9 ~, K' `    BufferedInputStream bis = new BufferedInputStream(p.getInputStream());  e( K+ G5 \  p# G: @
    InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
2 Y8 p2 G: A. P# M    StringWriter out = new StringWriter();
& d% V* e7 j6 [, @0 t    char [] buf = new char[10000];- j; W+ v3 J6 |2 D# h
    int len;
: s! i; v4 h* m/ O1 H    while((len = reader.read(buf))&gt;= 0) {6 E7 ?+ m& l" d3 G3 N! A
    //out.write(buf, 0, len);
: N  N, Y7 w% P) Y5 K    System.out.println("the length is"+len);+ s9 B+ a6 T9 H/ t3 [( J! T4 [$ e
    }5 a  x' m4 g+ p1 r2 ^7 J6 I
    reader.close();
' N0 K0 `$ M+ ~  G1 G" K    String ts=new String(buf);! R6 |( m; `9 w$ N" i
    System.out.println("the str is"+ts);
2 w, q4 [' Y4 V9 |, \: F) Y9 T  }
# }9 |3 H$ \, |/ z4 c- E5 x}/ b; i( d0 S0 Z$ k. h
</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-13 02:26 , Processed in 0.452530 second(s), 56 queries .

    回顶部