http://www-128.ibm.com/developerWorks/cn/java/l-java-tips/
Java extracts four weapons of Word, PDF
script>
script>
Level: Primary Chris (chris@matrix.org.cn), graduated from China University of Information Science, June 27, 2003
Many people often encounter a problem with Java, how do you get the contents of Word, Excel, PDF, and other documents? I have studied it here, summarize several ways to extract Word, PDF.
Using Jacob, Jacob is a bridage, a middleware that connects to Java and COM or Win32 functions. Jacob does not directly extract Word, Excel and other files, you need to write DLLs yourself, but you have written it for you, just Jacob The author is offering. Jacob Jar and DLL file Download: http://www.matrix.org.cn/down_view.asp?id=13 Download Jacob and place it after the specified path (DLL is placed in the path, the JAR file is placed in the classpath), You can write your own extract, the following 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 [] {});
}
}
}
Use apache's Poi to extract Word, Excel. POI is a project of Apache, but you may feel very annoying, but don't matter, it provides a simpler interface to you: download the packaged POI package: http://www.matrix.org. CN / DOWN_VIEW.ASP? ID = 14 After downloading, you can put it in 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);
}
}
PDFBOX- is used to extract PDF files but pdfbox is still not good, first download pdfbox: http://www.matrix.org.cn/down_view.asp?id=12 Next is an example of how to extract PDF files using PDFBOX :
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 ();
}
}
}
PDF files to support Chinese-XPDF XPDF are 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 Put the Chinese in the readme, you can start writing a Java program that calls the local method. The following 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, "-enc", "utf-8", "-q", filename, "-"
Process p = runtime.getRuntime (). 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);
}
}
About author
About the author: Chris, graduated from the University of Renmin University of China. In terms of Hong Kong, financial analysis software research and development, the author is also active in the development of the JXTA P2P open source software, and is keen on network security, AI search engine technology and Java-based game engine technology. If you have a better way, please tell the author: chris@matrix.org.cn