Four weapons __java in the Java language for office and PDF

Source: Internet
Author: User
Tags pack

Four weapons in the Java language that extract office, pdf


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.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, 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);
}
}


3, pdfbox-used to extract PDF files

But PDFBox support for Chinese is still not good, first download pdfbox:http://www.matrix.org.cn/down_view.asp?id=12

Here is an example of how to use PDFBox to extract PDF files:


Import Org.pdfbox.pdmodel.PDdocument.
Import Org.pdfbox.pdfparser.PDFParser;
Import java.io.*;
Import Org.pdfbox.util.PDFTextStripper;
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 pdfextracter{

Public Pdfextracter () {
}
public string gettextfrompdf (string filename) throws Exception
{
String Temp=null;
PDDocument. Nbsppdfdocument. Null
FileInputStream is=new fileinputstream (filename);
Pdfparser parser = new Pdfparser (IS);
Parser.parse ();
Pdfdocument. Nbsp= parser.getpddocument. );
Bytearrayoutputstream out = new Bytearrayoutputstream ();
OutputStreamWriter writer = new OutputStreamWriter (out);
Pdftextstripper stripper = new Pdftextstripper ();
Stripper.writetext (pdfdocument. 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[])
{
Pdfextracter pf=new Pdfextracter ();
PDDocument. Nbsppdfdocument. nbsp= null;

try{
String TS=PF. Gettextfrompdf ("C:/a.pdf");
SYSTEM.OUT.PRINTLN (TS);
}
catch (Exception e)
{
E.printstacktrace ();
}
}

}


4, the extraction of supporting Chinese PDF file-xpdf

Xpdf is an open source project and we can call his local method to extract the Chinese PDF file.

Download Xpdf function Pack: http://www.matrix.org.cn/down_view.asp?id=15

Also need to download support Chinese patch pack: http://www.matrix.org.cn/down_view.asp?id=16

With the patch of Chinese in the Readme, you can start writing Java programs that call local methods.

Here 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 Pdfwin {
Public Pdfwin () {
}
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 (). exec (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);
}
}

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.