<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    [摘錄]java抽取word,pdf的四種武器

    摘錄地址:http://wendy.reallydo.com/article.asp?id=64

    1。用jacob.
    其實jacob是一個bridage,連接java和com或者win32函數的一個中間件,jacob并不能直接抽取word,excel等文件,需要自己寫dll哦,不過已經有為你寫好的了,就是jacob的作者一并提供了。
    jacob下載:http://www.matrix.org.cn/down_view.asp?id=13
    下載了jacob并放到指定的路徑之后(dll放到path,jar文件放到classpath),就可以寫你自己的抽取程序了,下面是一個例子:
    import java.io.File;
    import com.jacob.com.*;
    import com.jacob.activeX.*;
    public class FileExtracter{
    public static void main(String[] args) {
      ActiveXComponent app = new ActiveXComponent("Word.Application");
      String inFile = "c:\test.doc";
    String tpFile = "c:\temp.htm";
      String otFile = "c:\temp.xml";
      boolean flag = false;
      try {
       app.setProperty("Visible", new Variant(false));
       Object docs = app.getProperty("document.").toDispatch();
       Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
       Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
       Variant f = new Variant(false);
       Dispatch.call(doc, "Close", f);
       flag = true;
      } catch (Exception e) {
       e.printStackTrace();
      } finally {
       app.invoke("Quit", new Variant[] {});
      }
    }
    }
    2。用apache的poi來抽取word,excel。
    poi是apache的一個項目,不過就算用poi你可能都覺得很煩,不過不要緊,這里提供了更加簡單的一個接口給你:下載經過封裝后的poi包:http://www.matrix.org.cn/down_view.asp?id=14
    下載之后,放到你的classpath就可以了,下面是如何使用它的一個例子:
    import java.io.*;
    import  org.textmining.text.extraction.WordExtractor;
    public class PdfExtractor {
      public PdfExtractor() {
      }
      public static void main(String args[]) throws Exception
      {
      FileInputStream in = new FileInputStream ("c:\a.doc");
      WordExtractor extractor = new WordExtractor();
      String str = extractor.extractText(in);
      System.out.println("the result length is"+str.length());
       System.out.println("the result is"+str);
    }
    }
    3。pdfbox-用來抽取pdf文件
    但是pdfbox對中文支持還不好,先下載pdfbox:http://www.matrix.org.cn/down_view.asp?id=12
    下面是一個如何使用pdfbox抽取pdf文件的例子:
    import org.pdfbox.pdmodel.PDdocument.
    import org.pdfbox.pdfparser.PDFParser;
    import java.io.*;
    import org.pdfbox.util.PDFTextStripper;
    import java.util.Date;
    public class PdfExtracter{
    public PdfExtracter(){
      }
    public String GetTextFromPdf(String filename) throws Exception
      {
      String temp=null;
      PDdocument.nbsp;pdfdocument.null;
      FileInputStream is=new FileInputStream(filename);
      PDFParser parser = new PDFParser( is );
      parser.parse();
      pdfdocument.nbsp;= parser.getPDdocument.);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      OutputStreamWriter writer = new OutputStreamWriter( out );
      PDFTextStripper stripper = new PDFTextStripper();
      stripper.writeText(pdfdocument.getdocument.), writer );
      writer.close();
      byte[] contents = out.toByteArray();
      String ts=new String(contents);
      System.out.println("the string length is"+contents.length+" ");
      return ts;
    }
    public static void main(String args[])
    {
    PdfExtracter pf=new PdfExtracter();
    PDdocument.nbsp;pdfdocument.nbsp;= null;
    try{
    String ts=pf.GetTextFromPdf("c:\a.pdf");
    System.out.println(ts);
    }
    catch(Exception e)
      {
      e.printStackTrace();
      }
    }
    }
    4.抽取支持中文的pdf文件-xpdf
    xpdf是一個開源項目,我們可以調用他的本地方法來實現抽取中文pdf文件。
    下載xpdf函數包:http://www.matrix.org.cn/down_view.asp?id=15
    同時需要下載支持中文的補丁包:http://www.matrix.org.cn/down_view.asp?id=16
    按照readme放好中文的patch,就可以開始寫調用本地方法的java程序了
    下面是一個如何調用的例子:
    import java.io.*;
    public class PdfWin {
      public PdfWin() {
      }
      public static void main(String args[]) throws Exception
      {
        String PATH_TO_XPDF="C:\Program Files\xpdf\pdftotext.exe";
        String filename="c:\a.pdf";
        String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
        InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
        StringWriter out = new StringWriter();
        char [] buf = new char[10000];
        int len;
        while((len = reader.read(buf))>= 0) {
        //out.write(buf, 0, len);
        System.out.println("the length is"+len);
        }
        reader.close();
        String ts=new String(buf);
        System.out.println("the str is"+ts);
      }
    }



    歡迎大家訪問我的個人網站 萌萌的IT人

    posted on 2008-03-06 10:18 見酒就暈 閱讀(177) 評論(0)  編輯  收藏 所屬分類: J2EE文章

    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(3)

    我參與的團隊

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    BLOG

    FRIENDS

    LIFE

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日韩免费高清一级毛片在线| 中字幕视频在线永久在线观看免费| 麻豆安全免费网址入口| 牛牛在线精品观看免费正| 两个人看的www免费视频| 亚洲精品国精品久久99热| 粉色视频在线观看www免费| 国产成人在线观看免费网站| 亚洲国产成人精品无码区花野真一 | 男人免费视频一区二区在线观看| 国产精品二区三区免费播放心| 亚洲精品伦理熟女国产一区二区| 日本二区免费一片黄2019| 老湿机一区午夜精品免费福利| 四虎精品亚洲一区二区三区| 无码日韩人妻AV一区免费l| 亚洲一级毛片免费看| 亚洲av区一区二区三| free哆拍拍免费永久视频| 亚洲色精品vr一区二区三区 | 本道天堂成在人线av无码免费| 国产亚洲情侣一区二区无码AV| 亚洲高清无在码在线电影不卡| 84pao国产成视频免费播放| 亚洲av无码国产综合专区 | 亚洲精品无码永久在线观看男男 | 国产精品亚洲精品日韩电影| 亚洲偷自拍拍综合网| 亚洲日本VA午夜在线电影| 亚洲M码 欧洲S码SSS222| 亚洲日韩AV一区二区三区中文| 亚洲?v无码国产在丝袜线观看| 国产成人免费ā片在线观看老同学 | 亚洲乱色熟女一区二区三区丝袜| 免费无码又爽又刺激高潮视频 | 成年女人免费视频播放77777| 久久久久亚洲AV无码永不| 午夜时刻免费入口| 99视频免费在线观看| 亚洲一区在线视频观看| 免费a级毛片无码av|