Code examples of four open source systems for processing Word, Excel, and PDF documents in JavaMany 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.
1. Use Jacob to Extract files such as Word and Excel.
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.
File Download: http://www.matrix.org.cn/down_view.asp? Id = 13 download Jacob and put it to the specified path (DLL put in path, JAR file put in classpath), you can write your own extraction program, the following is a simple example:
Import java. Io. file;
Import com.jacb.com .*;
Import com. Jacob. ActiveX .*;
/**
* Title: PDF Extraction
* Description: Email: chris@matrix.org.cn
* Copyright: matrix Copyright 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}, 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 [] {});
}
}
}
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? After downloading id = 14, put it in your classpath. The following is an example of how to use it:
Import java. Io .*;
Import org. textmining. Text. extraction. wordextractor;
/**
* Title: word Extraction
* Description: Email: chris@matrix.org.cn
* Copyright: matrix Copyright 2003
* Company: matrix.org.cn
* @ 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. Export 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 use product_box to extract PDF files:
Import orgdomainbox. pdmodel. pddocument.
Import org.w.boxw.parser. extends parser;
Import java. Io .*;
Import orgdomainbox. util. extends textstripper;
Import java. util. date;
/**
* Title: PDF Extraction
* Description: Email: chris@matrix.org.cn
* Copyright: matrix Copyright 2003
* Company: matrix.org.cn
* @ 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 + "/N ");
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 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 configure the Chinese patch according to readme to start writing the Java program that calls the local method. The following is an example of how to call: Import java. Io .*;
/**
* Title: PDF Extraction
* Description: Email: chris@matrix.org.cn
* Copyright: matrix Copyright 2003
* Company: matrix.org.cn
* @ 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 );
}
}