The way Jacob handles Word documents

Source: Internet
Author: User
7.4  uses Jacob to work with Word documents. Word or Excel programs exist in a COM component form. If you can call the COM component of Word in Java, you can use its methods to get textual information from a Word document. There are many tools available on the web today. 7.4.1  Jacob's Download, Jacob, is the acronym for Java-com Bridge, which builds bridges between Java and Microsoft's COM components. Using a DLL dynamic link library with the Jacob's, and using JNI to implement the call to COM programs on the Java platform. Jacob's Download address is: http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368. This book uses the Jacob_1.11_zip. After extracting the downloaded Jacob_1.11_zip file, as shown in Figure 7-17. Figure 7-17  The unpacked content of the Jacob package 7.4.2  in Eclipse (1) will jacob.jar import the project's build Path, then confirm the CPU type (X86 or AMD64) of your machine, and select the Jacob.dll files in different directories. (2) Place the Jacob.dll in the%java_home%/jre/bin directory, where%java_home% is the installation directory of the JDK. Note that this JRE directory must be the directory that eclipse is currently using, select the "Window->preferences" menu in Eclipse, and select the "java->installed JREs" item in the pop-up dialog box. As shown in Figure 7-18. Figure 7-18  dialog box for JRE settings in Eclipse (3) The currently selected JRE is under the "C:/Program files/java/jdk1.5.0_07/jre" directory, so Jacob.dll should be copied to c:/ Program Files/java/jdk1.5.0_07/jre/bin "directory below. (4) Create a new Ch7.jacob package in the project and Wordreader class in the package. This class will provide a static Extractdoc () method. It receives two parameters, one is the doc file name to process, the other is the file name of the output, and then the Word's API transformation content is called through JNI, and the code for the function is as follows. Code 7.10 public static void Extractdoc (String inputfile, String outputfile) {   Boolean flag = false;      //open Word should Program    activexcomponent app = new Activexcomponent ("Word.Application");    try {     //Set Word not visible       app.setproperty (" Visible ", new Variant (false));      //Open Word file       Dispatch Doc1 = App.getproperty ("Documents" ). Todispatch ();       Dispatch doc2 = Dispatch.invoke (             Doc1,             "Open",              Dispatch.method,              New object[] {inputfile, new Variant (false),                    New VariaNT (TRUE)}, new Int[1]). Todispatch ();      //Save to temporary file as txt format       dispatch.invoke (doc2, "SaveAs", Dispatch.method, new object[] {            outputfile, new Variant (7)}, new int[1]);      //Turn off Word       variant F = new Variant (FALSE);       Dispatch.call (DOC2, "close", f);       flag = true;    catch (Exception e) {      e.printstacktrace ();   } finally { &N bsp;    App.invoke ("Quit", new variant[] {});   }    if (flag = = True) {      System.out.println ("transformed Successfully ");   } else {      System.out.println ("Transform Failed");   }} (5) Create a main function to test the Wordreader class, the main function code is as follows. public static void Main (string[] ArGS) {        wordreader.extractdoc ("C:/test.doc", "C:/jacob.txt");} (6) The newly generated TXT file is saved to C:/jacob.txt, as shown in Figure 7-19. Figure 7-19  the effect of using Jacob when using Jacob, it is important that you have a Word application installed on the user's local system. Otherwise can not establish java-com bridge, and then can not be resolved.

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.