Java extracts office, pdf four pieces of weapons

Source: Internet
Author: User

Many people asked how to extract Word, Excel, PDF and other files, here I summarize the extraction of Word, PDF, several methods.

1, with Jacob.

In fact, Jacob is a bridage, connecting Java and COM or Win32 functions of a middleware, Jacob can not directly extract files such as word,excel, need to write their own DLL Oh, but has been written for you, is the author of Jacob together.

Jacob Download: http://www.matrix.org.cn/down_view.asp?id=13

After you have downloaded Jacob and placed it on the specified path (the DLL is placed in the Path,jar file to Classpath), you can write your own extractor, and here is an example:

import java.io.File;
import com.jacob.com.*;
import com.jacob.activex.*;
public class fileextracter{
public static void Main (string[] args) {
Activexcomponent app = new Activexcomponent ("Word.applica tion ");
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, with the Apache POI to extract Word,excel

POI is an Apache project, but even with poi you may find it annoying, but it doesn't matter, it provides a simpler interface for you:

Download the encapsulated POI package: http://www.matrix.org.cn/down_view.asp?id=14

After downloading, put it on your classpath, and here's 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 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);
}
}

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.