日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java抽取pdf_java 抽取 word,pdf 的四种武器

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java抽取pdf_java 抽取 word,pdf 的四种武器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:https://www.ibm.com/developerworks/cn/java/l-java-tips/ ? ? 感謝作者發布的文章

用 jacob

其實 jacob 是一個 bridage,連接 java 和 com 或者 win32 函數的一個中間件,jacob 并不能直接抽取 word,excel 等文件,需要自己寫 dll 哦,不過已經有為你寫好的了,就是 jacob 的作者一并提供了。

下載了 jacob 并放到指定的路徑之后 (dll 放到 path,jar 文件放到 classpath),就可以寫你自己的抽取程序了,下面是一個簡單的例子:

importjava.io.File;import com.jacob.com.*;import com.jacob.activeX.*;/*** Title: pdf extraction

* Description: email:chris@matrix.org.cn

* Copyright: Matrix Copyright (c) 2003

* Company: Matrix.org.cn

*@authorchris

*@version1.0,who use this example pls remain the declare*/

public classFileExtracter{public static voidmain(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", newVariant[] {});

}

}

}

用 apache 的 poi 來抽取 word,excel。

poi 是 apache 的一個項目,不過就算用 poi 你可能都覺得很煩,不過不要緊,這里提供了更加簡單的一個接口給你:

下載之后,放到你的 classpath 就可以了,下面是如何使用它的一個例子:

import java.io.*;

import org.textmining.text.extraction.WordExtractor;

/**

*

Title: word extraction

*

Description: email:chris@matrix.org.cn

*

Copyright: Matrix Copyright (c) 2003

*

Company: Matrix.org.cn

* @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);

}

}

pdfbox- 用來抽取 pdf 文件

下面是一個如何使用 pdfbox 抽取 pdf 文件的例子:

importorg.pdfbox.pdmodel.PDdocument .importorg.pdfbox.pdfparser.PDFParser;import java.io.*;importorg.pdfbox.util.PDFTextStripper;importjava.util.Date;/***

Title: pdf extraction

*

Description: email:chris@matrix.org.cn

*

Copyright: Matrix Copyright (c) 2003

*

Company: Matrix.org.cn

*@authorchris

*@version1.0,who use this example pls remain the declare*/

public classPdfExtracter{publicPdfExtracter(){

}public String GetTextFromPdf(String filename) throwsException

{

String temp=null;

PDdocument . nbsppdfdocument .null;

FileInputStream is=newFileInputStream(filename);

PDFParser parser= newPDFParser( is );

parser.parse();

pdfdocument . nbsp=parser.getPDdocument . );

ByteArrayOutputStream out= newByteArrayOutputStream();

OutputStreamWriter writer= newOutputStreamWriter( out );

PDFTextStripper stripper= newPDFTextStripper();

stripper.writeText(pdfdocument . getdocument . ), writer );

writer.close();byte[] contents =out.toByteArray();

String ts=newString(contents);

System.out.println("the string length is"+contents.length+"\n");returnts;

}public static voidmain(String args[])

{

PdfExtracter pf=newPdfExtracter();

PDdocument . nbsppdfdocument . nbsp= null;try{

String ts=pf.GetTextFromPdf("c:\\a.pdf");

System.out.println(ts);

}catch(Exception e)

{

e.printStackTrace();

}

}

}

抽取支持中文的 pdf 文件-xpdf

xpdf 是一個開源項目,我們可以調用他的本地方法來實現抽取中文 pdf 文件。

按照 readme 放好中文的 patch,就可以開始寫調用本地方法的 java 程序了

下面是一個如何調用的例子:

import java.io.*;/***

Title: pdf extraction

*

Description: email:chris@matrix.org.cn

*

Copyright: Matrix Copyright (c) 2003

*

Company: Matrix.org.cn

*@authorchris

*@version1.0,who use this example pls remain the declare*/

public classPdfWin {publicPdfWin() {

}public static void main(String args[]) throwsException

{

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= newBufferedInputStream(p.getInputStream());

InputStreamReader reader= new InputStreamReader(bis, "UTF-8");

StringWriter out= newStringWriter();char [] buf = new char[10000];intlen;while((len = reader.read(buf))>= 0) {//out.write(buf, 0, len);

System.out.println("the length is"+len);

}

reader.close();

String ts=newString(buf);

System.out.println("the str is"+ts);

}

}

總結

以上是生活随笔為你收集整理的java抽取pdf_java 抽取 word,pdf 的四种武器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。