程式設計:Java抽取Word,PDF的四種武器(轉)
程式設計:Java抽取Word,PDF的四種武器(轉)[@more@] 很多人用java進行文件操作時經常會遇到一個問題,就是如何獲得word,excel,pdf等文件的內容?我研究了一下,在這裡總結一下抽取word,pdf的幾種方法。
??1. 用jacob
??其實jacob是一個bridage,連線java和com或者win32函式的一箇中介軟體,jacob並不能直接抽取word,excel等檔案,需要自己寫dll哦,不過已經有為你寫好的了,就是jacob的作者一併提供了。
??jacob jar與dll檔案下載:
??下載了jacob並放到指定的路徑之後(dll放到path,jar檔案放到classpath),就可以寫你自己的抽取程式了,下面是一個簡單的例子:
import java.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
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class FileExtracter{
public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c: est.doc";
String tpFile = "c: emp.htm";
String otFile = "c: emp.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你可能都覺得很煩,不過不要緊,這裡提供了更加簡單的一個介面給你:
??下載經過封裝後的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);
}
}
??3. pdfbox-用來抽取pdf檔案
??但是pdfbox對中文支援還不好,先下載pdfbox:
??下面是一個如何使用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
*
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 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+" ");
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是一個開源專案,我們可以呼叫他的本地方法來實現抽取中文pdf檔案。
??下載xpdf函式包:
??同時需要下載支援中文的補丁包,按照readme放好中文的patch,就可以開始寫呼叫本地方法的java程式了。
??下面是一個如何呼叫的例子:
import java.io.*;
/**
*
Title: pdf 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 PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:Program Filesxpdfpdftotext.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);
}
}
??1. 用jacob
??其實jacob是一個bridage,連線java和com或者win32函式的一箇中介軟體,jacob並不能直接抽取word,excel等檔案,需要自己寫dll哦,不過已經有為你寫好的了,就是jacob的作者一併提供了。
??jacob jar與dll檔案下載:
??下載了jacob並放到指定的路徑之後(dll放到path,jar檔案放到classpath),就可以寫你自己的抽取程式了,下面是一個簡單的例子:
import java.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
* @author chris
* @version 1.0,who use this example pls remain the declare
*/
public class FileExtracter{
public static void main(String[] args) {
ActiveXComponent component = new ActiveXComponent("Word.Application");
String inFile = "c: est.doc";
String tpFile = "c: emp.htm";
String otFile = "c: emp.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你可能都覺得很煩,不過不要緊,這裡提供了更加簡單的一個介面給你:
??下載經過封裝後的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);
}
}
??3. pdfbox-用來抽取pdf檔案
??但是pdfbox對中文支援還不好,先下載pdfbox:
??下面是一個如何使用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
*
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 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+" ");
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是一個開源專案,我們可以呼叫他的本地方法來實現抽取中文pdf檔案。
??下載xpdf函式包:
??同時需要下載支援中文的補丁包,按照readme放好中文的patch,就可以開始寫呼叫本地方法的java程式了。
??下面是一個如何呼叫的例子:
import java.io.*;
/**
*
Title: pdf 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 PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:Program Filesxpdfpdftotext.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);
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-959032/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java PDF 轉 Word 教程Java
- java Word 轉 PDF格式Java
- pdf轉word格式PDF to word for MacMac
- pdf 轉 word
- PDF轉Word?PDF轉換工具推薦:PDF to Word Document Converter for MacMac
- 【PY】Word 轉 PDF
- PDF to Word Document Converter Mac(PDF轉Word檔案轉換器)Mac
- pdf轉word工具推薦 PDF to word 最新啟用版
- PDF批次轉換器,批次轉word為pdf,批次轉ppt為pdf
- pdf轉word工具:PDF to word for Mac v8.5.6啟用版Mac
- 四種將Word轉換為HTML的線上工具HTML
- word與excel轉pdf方法Excel
- pdf轉換成word文件
- stirlingpdf PDF 工具將PDF轉換成Word
- 好程式設計師Java教程分享Java的兩種跳轉語句程式設計師Java
- pdf轉word如何線上轉換?
- Java 將Markdown檔案轉換為Word和PDF文件Java
- 用Java寫一個PDF,Word檔案轉換工具Java
- Flyingbee PDF to word for Mac(飛蜂pdf轉word轉換器) v2.8啟用版Mac
- PDF文件轉換器PDF to Word Document Converter MacMac
- pdf的表格怎麼轉換成word?
- 命令列下將 word 轉 pdf命令列
- pdf轉換word,超簡單!
- pdf轉換成word,免費轉換
- 五種Java程式設計高效程式設計方法 - BablaJava程式設計
- [PY] Word 處理, 技術選型, Word 轉 PDF
- Java 將PDF/XPS轉為Word/html /SVG/PS/PCL/PNG、PDF和XPS互轉(基於Spire.Cloud.SDK for Java)JavaHTMLSVGCloud
- pdf轉word工具,轉易俠很優秀!
- pdf轉換成word,非常實用
- win10 pdf怎麼轉換成word_win10如何把pdf轉換成word文件Win10
- 【java】int轉換成String的四種方法Java
- java23種設計模式——四、原型模式Java設計模式原型
- Java 將PDF轉為PDF/AJava
- Java中將XML轉換為PDF的兩種辦法JavaXML
- go用unioffice轉換word文件為pdfGo
- 線上免費工具——pdf轉word文件
- 怎麼樣轉換pdf格式為Word?
- 如何用Python把pdf轉換成wordPython
- 《JAVA語言程式設計》pdf 附下載連結Java程式設計