Many people asked how to extract Word, Excel, PDF and other files, here I summarize the extraction of Word, PDF, several methods.
1, with Jacob.
In fact, Jacob is a bridage, connecting Java and COM or Win32 functions of a middleware, Jacob can not directly extract files such as word,excel, need to write their own DLL Oh, but has been written for you, is the author of Jacob together.
Jacob Download: http://www.matrix.org.cn/down_view.asp?id=13
After you have downloaded Jacob and placed it on the specified path (the DLL is placed in the Path,jar file to Classpath), you can write your own extractor, and here 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.applica tion ");
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[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, with the Apache POI to extract Word,excel
POI is an Apache project, but even with poi you may find it annoying, but it doesn't matter, it provides a simpler interface for you:
Download the encapsulated POI package: http://www.matrix.org.cn/down_view.asp?id=14
After downloading, put it on your classpath, and here's an example of how to use it:
import java.io.*;
import org.textmining.text.extraction.WordExtractor;
/**
* <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 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);
}
}