JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗,分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請與我聯(lián)系。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          1. 用jacob
          其實jacob是一個bridage,連接java和com或者win32函數(shù)的一個中間件,jacob并不能直接抽取word,excel等文件,需要自己寫dll哦,不過已經(jīng)有為你寫好的了,就是jacob的作者一并提供了。
          jacob jar與dll文件下載: http://danadler.com/jacob/
          下載了jacob并放到指定的路徑之后(dll放到path,jar文件放到classpath),就可以寫你自己的抽取程序了,下面是一個簡單的例子:

           

          import java.io.File; 
          import com.jacob.com.*
          import com.jacob.activeX.*
          /** 
          * Title: pdf extraction 
          */
           
          public class FileExtracter {

           
          public static void main(String[] args) 
            ActiveXComponent component 
          = new ActiveXComponent("Word.Application"); 
            String inFile 
          = "c:\\test.doc"
            String tpFile 
          = "c:\\temp.htm"
            String otFile 
          = "c:\\temp.XML"
            
          boolean flag = false
            
          try {
             component.setProperty(
          "Visible"new Variant(false)); 
             Object wordacc 
          = component.getProperty("document.").toDispatch(); 
             Object wordfile 
          = Dispatch.invoke(wordacc,"Open", Dispatch.Method, 
             
          new Object[]{inFile,new Variant(false), new Variant(true)}
             
          new int[1] ).toDispatch(); 
             Dispatch.invoke(wordfile,
          "SaveAs", Dispatch.Method, new Object[]{tpFile,new  Variant(8)}new int[1]); 
             Variant f 
          = new Variant(false); 
             Dispatch.call(wordfile, 
          "Close", f); 
             flag 
          = true
            }
           catch (Exception e) 
             e.printStackTrace(); 
            }
           finally 
             component.invoke(
          "Quit"new Variant[] {}); 
            }
           
           }
           
          }
           


          2.用apache的poi來抽取word,excel。
          poi是apache的一個項目,不過就算用poi你可能都覺得很煩,不過不要緊,這里提供了更加簡單的一個接口給你:
          下載經(jīng)過封裝后的poi包: http://jakarta.apache.org/poi/
          下載之后,放到你的classpath就可以了,下面是如何使用它的一個例子:

          import java.io.*
          import org.textmining.text.extraction.WordExtractor; 
          /** 
          * Title: word extraction
          */
           

          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.pdfbox.org/
          下面是一個如何使用pdfbox抽取pdf文件的例子:

          import org.pdfbox.pdmodel.PDdocument. 
          import org.pdfbox.pdfparser.PDFParser; 
          import java.io.*
          import org.pdfbox.util.PDFTextStripper; 
          import java.util.Date; 
          /** 
          * Title: pdf extraction
          */
           

          public class PdfExtracter

           
          public PdfExtracter()
           }

           
           
          public String GetTextFromPdf(String filename) throws Exception 
            String temp
          =null
            PDdocument.nbsppdfdocument.
          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+"\n"); 
            
          return ts; 
           }

           
           
          public static void main(String args[]) {
            PdfExtracter pf
          =new PdfExtracter(); 
            PDdocument.nbsppdfdocument.nbsp
          = null
            
          try
             String ts
          =pf.GetTextFromPdf("c:\\a.pdf"); 
             System.out.println(ts); 
            }
           catch(Exception e) 
             e.printStackTrace(); 
            }
           
           }
           
          }


          4. 抽取支持中文的pdf文件-xpdf
          xpdf是一個開源項目,我們可以調(diào)用他的本地方法來實現(xiàn)抽取中文pdf文件。
          下載xpdf函數(shù)包: http://www.foolabs.com/xpdf/
          同時需要下載支持中文的補丁包,按照readme放好中文的patch,就可以開始寫調(diào)用本地方法的java程序了。
          下面是一個如何調(diào)用的例子:

          import java.io.*
          /** 
          * Title: pdf extraction
          */
           

          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); 
           }
           
          }

          posted on 2008-08-27 13:47 rogerfan 閱讀(450) 評論(0)  編輯  收藏 所屬分類: 【Java知識】
          主站蜘蛛池模板: 迁西县| 闻喜县| 永丰县| 宁陕县| 连南| 承德县| 赫章县| 通州市| 鹿邑县| 龙口市| 宣威市| 赤水市| 天津市| 马公市| 太原市| 宁都县| 苍山县| 郯城县| 巴彦县| 台南市| 农安县| 通辽市| 通渭县| 汉中市| 仁布县| 扶沟县| 临汾市| 毕节市| 金乡县| 广饶县| 鄯善县| 古丈县| 化德县| 新郑市| 尉犁县| 方山县| 鹤岗市| 枣庄市| 三原县| 兴海县| 拜泉县|