1. 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 download:
Http://www.matrix.org.cn/down_view.asp? Id = 13
After you download Jacob and put it in the specified path (place DLL in path and JAR file in classpath), you can write your own extraction program. The following is an example:
Import java. Io. file;
Import com.jacb.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 [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 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: 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 extends extractor {
Public extends Extractor (){
}
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. Product_box-used to extract PDF files
However, product_box does not provide good support for Chinese characters. First download javasbox: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 orgdomainbox. pdmodel. pddocument.
Import org.w.boxw.parser. extends parser;
Import java. Io .*;
Import orgdomainbox. util. extends textstripper;
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 extends extracter {
Public writable extracter (){
}
Public String gettextfrompdf (string filename) throws exception
{
String temp = NULL;
Pddocument. nbsp;document. null;
Fileinputstream is = new fileinputstream (filename );
Partition parser = new partition Parser (is );
Parser. parse ();
Optional document. nbsp = parser. getpddocument .);
Bytearrayoutputstream out = new bytearrayoutputstream ();
Outputstreamwriter writer = new outputstreamwriter (out );
Required textstripper stripper = new required textstripper ();
Stripper. writetext (invalid document. 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 [])
{
Extends extracter pF = new extends extracter ();
Pddocument. nbsp1_document. nbsp = NULL;
Try {
String Ts = PF. gettextfrompdf ("C: // a.pdf ");
System. Out. println (TS );
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}
4. 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 the xpdf function package:Http://www.matrix.org.cn/down_view.asp? Id = 15
You also need to download a patch that supports Chinese characters: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 login win {
Public writable win (){
}
Public static void main (string ARGs []) throws exception
{
String path_to_xpdf = "C: // program files // xpdf // export totext.exe ";
String filename = "C: // a.pdf ";
String [] cmd = new string [] {path_to_xpdf, "-ENC", "UTF-8", "-Q", filename ,"-"};
PROCESS p = runtime.getruntime(cmd.exe C (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 );
}
}