Many people asked how to extract Word, Excel, PDF. Here I summarize several ways to extract Word, PDF. 1. Using Jacob. In fact, Jacob is a middleware that connects Java and COM or Win32 functions. Jacob does not directly extract Word, Excel and other files, you need to write DLLs yourself, but already write it for you, Jacob Author one is available. Jacob Download:
http://www.matrix.org.cn/down_view.asp?id=13
After downloading Jacob and put it in the specified path (DLL is placed in the path, the JAR file is placed in the classpath), you can write your own extraction program, below is an example: Import java.io.file; import com.jacob. . com *; import com.jacob.activeX *; public class FileExtracter {public static void main (String [] args) {ActiveXComponent app = new ActiveXComponent ( "Word.Application"); String inFile = "c:. // test .doc "; string tpfile =" c: //Temp.htm "; string OtFile =" c: //temp.xml "; boolean flag = false; try {app.setproperty (" visible ", new variant (false) ); Object Docs = app.getProperty ("document."). Todispatch (); object doc = dispatch.invoke (DOCS, "Open", Dispatch.Method, New Object [] {Infile, New Variant (false), New Variant (true)}, new int [1]). Todispatch (); Dispatch.Invoke (DOC, "Saveas", Dispatch.method, New Object [] {TPFILE, New Variant (8)}, new int rt [1] ); Variant f = new variant (false); Dispatch.call (DOC, "Close", F); Flag = true;} catch (exception e) {E.PrintStackTrace ();} finally {app.invoke ("quit ", New variant [] {});}}} 2. Use apache's Poi to extract Word, Excel. POI is a project of Apache, but it may be very annoying, but don't matter, it provides a simpler interface to you: download the packaged POI package:
http://www.matrix.org.cn/down_view.asp?id=14
After downloading, you can put it in your classpath. How do you use it with it: import java.io. *; Import org.textmining.text.extraction.Wordextractor; / ** *
Title: PDF extrantion p> *
description: email: chris@matrix.org.cn p> *
Copyright: Matrix Copyright (c) 2003 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);}} 3. PDFBOX- is used to extract PDF files but PDFBOX is not good to support Chinese, first download pdfbox: http://www.matrix.org.cn/down_view.asp? id = 12
Below is an example of how to use PDFBOX to extract PDF files: 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 P> *
Description: email: chris@matrix.org.cn p> *
Copyright: Matrix Copyright (C) 2003 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 (); Parser.Parse (); pdfdocument. NBSP = parse.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 (content); system.out.println ("THE STRINGTH 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 files for supporting Chinese - xpdfxpdf are an open source project, we can call his local approach to extract a Chinese PDF file. Download XPDF function package: http://www.matrix.org.cn/down_view.asp? Id = 15
At the same time, you need to download a patch package that supports Chinese:
http://www.matrix.org.cn/down_view.asp?id=16 Put the Chinese Patch according to Readme, you can start writing a Java program that calls the local method. The following is an example of how to call: Import Java.io * description: email: chris@matrix.org.cn p> * Copyright: Matrix CopyRight (C) 2003 < / p> * Company: Matrix.org.cn p> * @Author chris * @version 1.0, who use this example pls remain the declare * / public class pdfwin {public packet () {} public static void Main (string args []) throws exception {string path_to_xpdf = "c: // 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.pr INTLN ("The length is" len);} reader.close (); string ts = new string (buf); system.out.println ("The Str is" TS);}}