Java technical documents use Java to obtain documents such as Word, Excel, and PDF

Source: Internet
Author: User

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 // export 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 );
}
}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1293505

 

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.