1. Using Jacob, Jacob is a bridage, connecting the Java and COM or Win32 functions, Jacob can't take directly to Word, Excel and other files, you need to write DLLs yourself, but you have already written for you, just Jacob's author is offering.
Jacob Jar and DLL File 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 a simple example:
Import java.io.file;
Import com.jacob.com. *;
Import com.jacob.activex. *;
/ **
* Title: PDF EXTRACTION
* Description: email: chris@matrix.org.cn
* Copyright: Matrix CopyRight (C) 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 (WordAc, "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 (8)}, 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's POI to extract Word, Excel. POI is a project of Apache, but it may be very annoying, but don't matter, it provides you with a simpler interface to you:
Download the packaged POI package: http://www.matrix.org.cn/down_view.asp?id=14 download, you can put your classpath, how is the following example:
Import java.io. *;
Import org.textmining.text.extraction.wordextractor;
/ **
*
Title: Word 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 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
Here 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 (IS);
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 (Contents);
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. Extracting the PDF file -Xpdfxpdf to support Chinese is 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 support Chinese patch pack: http://www.matrix.org.cn/down_view.asp?id=16
Putme's PATCH according to Readme, you can start writing a Java program that calls the local method.
Here is an example of how to call:
Import java.io. *;
/ **
*
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 PDFWIN {
Public pdfwin () {
}
Public static void main (string args []) THROWS EXCEPTION
{
String path_to_xpdf = "c: // program files // xpdf // pdftotext.exe";
String filename = "c: //a.pdf";
String [] cmd = new string [] {path_to_xpdf, "-ent", "UTF-8", "-q", filename, "-"}; process p = runtime.getime (). 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.println ("The Length Is" LEN);
}
Reader.Close ();
String TS = New String (buf);
System.out.println ("THE STR IS" TS);
}
}