Java word extraction, four weapons of pdf @ -- general Linux technology-Linux programming and kernel information, the following is a detailed reading. Four java-based word and pdf extraction weapons
Chris
19:04:00
542 views
Chris (chris@matrix.org.cn)
Graduated from the Information School of Renmin University of China
June 2003
Many 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
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 jar and dll file download: http://www.matrix.org.cn/down_view.asp? Id = 13
After you download jacob and put it in the specified path (dll and jar files are put into classpath), you can write your own extraction program. Below 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? Id = 14
After the download, put it in your classpath. The following is an example of how to use it:
* @ 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 extract a PDF file using product_box:
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 .*;
/**
*
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 \ Program 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 );
}
}
About the author
About the author: chris graduated from the Information College of Renmin University of China. Currently, I am engaged in financial analysis software development in Hong Kong. I am also active in the development community of jxta p2p open source software. I am also keen on network security, AI search engine technology and java-based game engine technology.
If anyone has a better way, please tell the author: chris@matrix.org.cn
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.