Programming: four weapons for Java to extract Word and PDF

Source: Internet
Author: User
Programming: four weapons for Java to extract Word and PDF-general Linux technology-Linux programming and kernel information. For more information, see the following. 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://danadler.com/jacob/

?? 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 (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 (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 (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 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://jakarta.apache.org/poi/

?? 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;
/**
*

Title: word 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 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

?? However, the Consumer box is not good for Chinese support, first download consumer box: http://www.pdfbox.org/

?? 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;
/**
*

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 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 the xpdf function Pack: http://www.foolabs.com/xpdf/

?? At the same time, you need to download the patch packages that support Chinese characters, and set 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 (c) 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 );
}
}
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.