Java extracts Office, Four weapons of PDF

zhaozj2021-02-16  96

Many people asked how to extract words such as Word, Excel, PDF, here I summarize several ways to extract Word, PDF.

1, use Jacob.

In fact, Jacob is a middleware that connects Java and COM or Win32 functions. Jacob does not directly extract Word, Excel and other files, you need to write DLLs yourself, but you have written it for you, it is the author of Jacob. And provided.

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

After downloading Jacob and put it in the specified path (DLL is placed in the path, the JAR file is placed in the classpath), you can write your own extraction program, below 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, use Apache's Poi to extract Word, Excel

POI is a project of Apache, but it may be very annoying, but don't matter, it provides you with a simpler interface to you:

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

After downloading, you can put it in your classpath, how is the following example of using it:

Import java.io. *; import org.textmining.text.extraction.Wordextractor; / ***

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 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 is not good, first download pdfbox: http://www.matrix.org.cn/down_view.asp? Id = 12 Next is an example of how to extract PDF files using PDFBOX:

Import org.pdfbox.pdmodel.pddocument. Import org.pdfbox.pdfparser.pdfparser; import java.io. *; import org.pdfbox.util.pdftextstripper; 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 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 (); Parser.Parse (); pdfdocument. NBSP = parse.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 (content); system.out.println ("THE STRINGTH IS" CONTENTS.LENGTH "/ N"); 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 , Extract PDF files support Chinese - XPDF

XPDF is an open source project, we can call his local approach to achieve a Chinese PDF file.

Download XPDF function package: http://www.matrix.org.cn/down_view.asp? Id = 15

At the same time, it is necessary to download support Chinese patch package: http://www.matrix.org.cn/down_view.asp? Id = 16 Putmed the Chinese PATCH according to Readme, you can start writing a Java program that calls the local method.

Here 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 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, "-ent", "utf-8", "-q", filename, "-"}; process p = runtime.getime (). 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);}}

Note: The original author: chris ■ Source: matrix

转载请注明原文地址:https://www.9cbs.com/read-11778.html

New Post(0)