Author : Chris
Source : Matrix
**************************************** **************************************** **************************************** *****************************
Many people ask how to extract files such as Word, Excel, and PDF. Here I will summarize several methods for extracting word and PDF files.
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
Download Jacob and put it in the specified path (DLL to path, JAR file to classpath), you can write your own ExtractionProgramThe 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
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
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 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 + "\ 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
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 login win () {
}< br>
Public static void main (string ARGs []) throws exception
{< br>
string path_to_xpdf = "C: \ Program Files \ xpdf \ Program totext.exe ";
string filename = "C :\\ a.pdf";
string [] cmd = new string [] {path_to_xpdf, "-ENC ", "UTF-8", "-Q", filename, "-" };< br>
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);
}< br>
reader. close ();
string Ts = new string (BUF);
system. out. println ("the STR is" + TS);
}< br>