Java抽取Word,PDF的四种武器 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: 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 * 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+"/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是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。 ????下载xpdf函数包: http://www.foolabs.com/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 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); } }
|
|
更多资源: 参与论坛讨论: http://www.java-cn.com/forum/index.jsp 更多技术文章: http://www.java-cn.com/technology/index.jsp 更多JAVA职位: http://www.e4u.cn/itjob/itjobSeach.jsp?classid=1&fatherid=13 JAVA中文站: 我们要做中国最好的JAVA门户网站,记住我们的域名: JAVA-CN.COM | JAVA-CN.NET | JAVA-CN.ORG |
本文网址: http://www.java-cn.com/technology/technology_detail.jsp?id=3445 声 明: 转载此文章,须在显著位置注明 JAVA中文站 的原文网址; 若是本站的转载文章,请标明原文出处,并保留作者信息 |