Four weapons for extracting office and PDF in Java

Source: Internet
Author: User
Many people ask how to extract files such as Word, Excel, and PDF. Here I will summarize several methods for extracting word and PDF files.

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 download: http://www.matrix.org.cn/down_view.asp? Id = 13

After you download Jacob and put it in the specified path (place DLL in path and JAR file in classpath), you can write your own extraction program. The following is an example:

Import java. Io. file;
Import com.jacb.com .*;
Import com. Jacob. ActiveX .*;
Public class fileextracter {

Public static void main (string [] ARGs ){

Activexcomponent APP = new activexcomponent ("word. application ");
String infile = "C:/test.doc ";
String tpfile = "C:/temp.htm ";
String otfile = "C:/temp. xml ";
Boolean flag = false;
Try {
App. setproperty ("visible", new variant (false ));
Object docs = app. getproperty ("document."). todispatch ();
Object Doc = Dispatch. Invoke (Docs, "open", dispatch. method, new object [] {
Infile, new variant (false), new variant (true)}, new int [1]). todispatch ();
Dispatch. Invoke (Doc, "saveas", dispatch. method, new object [] {
Tpfile, new variant (8)}, new int [1]);
Variant F = new variant (false );
Dispatch. Call (Doc, "close", F );
Flag = true;
} Catch (exception e ){
E. printstacktrace ();
} Finally {
App. Invoke ("quit", new variant [] {});
}

}
}

2. Use Apache poi to extract Word and Excel

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:

Import java. Io .*;
Import org. textmining. Text. extraction. wordextractor;
/**
* <P> title: PDF extraction </P>
* <P> Description: Email: chris@matrix.org.cn </P>
* <P> copyright: matrix copyright (c) 2003 </P>
* <P> company: matrix.org.cn </P>
* @ 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. product_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:

Import orgdomainbox. pdmodel. pddocument.
Import org.w.boxw.parser. extends parser;
Import java. Io .*;
Import orgdomainbox. util. extends textstripper;
Import java. util. date;
/**
* <P> title: PDF extraction </P>
* <P> Description: Email: chris@matrix.org.cn </P>
* <P> copyright: matrix copyright (c) 2003 </P>
* <P> company: matrix.org.cn </P>
* @ 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 +"
");
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 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 .*;
/**
* <P> title: PDF extraction </P>
* <P> Description: Email: chris@matrix.org.cn </P>
* <P> copyright: matrix copyright (c) 2003 </P>
* <P> company: matrix.org.cn </P>
* @ 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/pdftotext.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.