Four java-based word and pdf extraction weapons

Source: Internet
Author: User

Level: elementary

Chris graduated from the Information College of Renmin University of China.

June 27, 2003

Many people often encounter a problem when using java for document operations, that is, how to obtain the content of documents such as word, excel, and pdf? I have studied and summarized several methods for extracting word and pdf.

Use jacob

In fact, jacob is a bridage, a middleware that connects java and com or win32 functions. jacob cannot directly Extract files such as word and excel. He needs to write the dll by himself, but it has already been written for you, and it is provided by jacob's author.

Jacob jar and dll file download: http://www.matrix.org.cn/down_view.asp? Id = 13

After you download jacob and put it in the specified path (dll and jar files are put into classpath), you can write your own extraction program. Below is a simple example:

            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:\\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[] {});            }            }            }            

Use apache poi to extract word and excel files.

Poi is a project of apache. However, even if you use poi, you may feel annoyed. However, it doesn't matter. Here we provide you with a simpler interface:

Download the encapsulated poi package: http://www.matrix.org.cn/down_view.asp? Id = 14

After the download, put it in your classpath. The following is an example of how to use it:

            import java.io.*;            import  org.textmining.text.extraction.WordExtractor;            /**            * <p>Title: word 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);            }            }            

Product_box-used to extract PDF files

But product_box for Chinese support is not good, first download product_box: http://www.matrix.org.cn/down_view.asp? Id = 12

The following is an example of how to extract a PDF file using product_box:

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

Extract PDF files that support Chinese characters-xpdf

Xpdf is an open-source project. We can call its local method to extract Chinese PDF files.

Download xpdf function package: http://www.matrix.org.cn/down_view.asp? Id = 15

At the same time need to download support Chinese patch package: http://www.matrix.org.cn/down_view.asp? Id = 16

By following the readme patch, you can start to write the java program that calls the local method.

The following is an example of how to call:

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.