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

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

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

    posts - 56, comments - 54, trackbacks - 0, articles - 4
       ::  ::  :: 聯系 :: 聚合  :: 管理

    java抽取word,pdf的四種武器(轉載)

    Posted on 2005-12-07 14:40 Terry的Blog 閱讀(578) 評論(0)  編輯  收藏 所屬分類: java語言轉載
    摘要:

    轉載:轉載請保留本信息,本文來自http://www.matrix.org.cn/resource/article/0/120.html
    很多人問到如何抽取word,excel,pdf阿。這里我總結一下抽取word,pdf的
    幾種方法。
    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("Documents").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;
    /**
    * <p>Title: pdf extraction</p>
    * <p>Description: email:chris@matrix.org.cn</p>
    * <p>Copyright: Matrix Copyright (c) 2003</p>
    * <p>Company: Matrix.org.cn</p>
    * @author chris
    * @version 1.0,who use this example pls remain the declare
    */

    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;
    /**
    * <p>Title: pdf extraction</p>
    * <p>Description: email:chris@matrix.org.cn</p>
    * <p>Copyright: Matrix Copyright (c) 2003</p>
    * <p>Company: Matrix.org.cn</p>
    * @author chris
    * @version 1.0,who use this example pls remain the declare
    */

    public class PdfExtracter{

    public PdfExtracter(){
    }
    public String GetTextFromPdf(String filename) throws Exception
    {
    String temp=null;
    PDDocument pdfdocument=null;
    FileInputStream is=new FileInputStream(filename);
    PDFParser parser = new PDFParser( is );
    parser.parse();
    pdfdocument = 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+"\n");
    return ts;
    }
    public static void main(String args[])
    {
    PdfExtracter pf=new PdfExtracter();
    PDDocument pdfDocument = 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.*;
    /**
    * <p>Title: pdf extraction</p>
    * <p>Description: email:chris@matrix.org.cn</p>
    * <p>Copyright: Matrix Copyright (c) 2003</p>
    * <p>Company: Matrix.org.cn</p>
    * @author chris
    * @version 1.0,who use this example pls remain the declare
    */


    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);
    }
    }
    主站蜘蛛池模板: 国产亚洲精品精品精品| 国产成人精品免费久久久久| 亚洲国产精品尤物YW在线观看 | 国产成人综合久久精品亚洲| 国产亚洲人成无码网在线观看 | 国产成人免费a在线视频色戒| 中文字幕免费观看视频| 亚洲熟妇av一区二区三区下载| 永久免费看mv网站入口| 青青操免费在线观看| 中文日韩亚洲欧美制服| 亚洲色成人中文字幕网站| 69堂人成无码免费视频果冻传媒| 免费国产污网站在线观看不要卡| 亚洲AV无码成人网站久久精品大| 成人性生免费视频| 无码人妻丰满熟妇区免费| mm1313亚洲国产精品无码试看| 亚洲国产精品SSS在线观看AV| 日本特黄特黄刺激大片免费| 久久永久免费人妻精品| 免费一级毛suv好看的国产网站| 亚洲精品美女久久久久9999| 国产亚洲精品无码拍拍拍色欲| 成人特黄a级毛片免费视频| 久久九九全国免费| 男女污污污超污视频免费在线看| 亚洲中文久久精品无码1 | 亚洲av无码成h人动漫无遮挡| 亚洲 自拍 另类小说综合图区| 国产电影午夜成年免费视频| a毛片全部免费播放| 大片免费观看92在线视频线视频| 亚洲成a人片在线看| 久久精品国产亚洲av高清漫画 | 国产∨亚洲V天堂无码久久久| 免费一级做a爰片性色毛片| 成人影片麻豆国产影片免费观看| 天天影视色香欲综合免费| 日本亚洲欧洲免费天堂午夜看片女人员| 国产亚洲精品美女2020久久 |