java accurately read word file pages

Transfer: https://blog.csdn.net/tiandixuanwuliang/article/details/71298406

As I was doing an online printing site, met a demand was "exactly the number of pages read the word, in order to achieve billing functions", through a very long period of learning, information search, finally solved the problem, and therefore the method written, people have the same need to facilitate detours.

Kookob first need to thank the bloggers blog post (http://blog.csdn.net/kookob/article/details/45038563?locationNum=3&fps=1) helped me a lot, but which introduced not perfect, it is in this more granular explain, we want to help.

First, I have experimented with using poi method, java2word method, itext methods are not well read pages word documents, most of the problems are not accurate reading. Because here it comes to money matters, it must be accurate, so only the paper.

Second, the idea of ​​this article is "pdf format directly read, word pdf format first turn and then read", due to take some time when the word transfer pdf, if only we developers use the progress bar when the word is large, will cause the turn too slow, so I'm on my website (already online), the user "choose file", the format began to turn back, then let the user see, try to let users do some configuration, such as the number of copies whether double-sided printing, user settings finished, displaying "read format" progress bar in the final settlement of the page, and then displays "Calculating the amount," so that the user experience will be better.

Third, following the step by step to achieve an accurate reading of word Pages:

3.1 Preparation:

3.1.1 Development Environment: windows. Be sure to carry out this operation in the windows operating system, because the word into pdf need on the server.

3.1.2 installed on your computer word, note the installation word, not installed wps.

3.1.3 Install the word transfer pdf plug-ins on your computer: "Software name: SaveAsPDFandXPS", download address: http: //download.csdn.net/download/tiandixuanwuliang/10006993

3.1.4 Please call the dll file system is placed in the bin directory of java jre environment, for example: E: \ software \ java \ jre7 \ bin \ jacob-1.18-x64.dll, (please pay attention to his computer 32 bit or 64-bit) Download: http: //download.csdn.net/download/tiandixuanwuliang/10007003

3.1.5 java environment jdk1.7

3.2 word transfer pdf:

3.2.1 New java project, add jacob.jar to the project, this jar wrapped in Section 3.1.4 downloaded file folder inside.

While the jacob.jar file copy into the C: windows / system32 down /

Figure:

3.2.2Word2PdfUtil.java code is as follows:

package testJavaReadWordpage;
 
import java.io.File;
import java.io.IOException;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
 
public class Word2PdfUtil {
 
    static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
    static final int wdFormatPDF = 17;// word转PDF 格式
 
    public static boolean word2pdf(String source, String target) {
        System.out.println("Word转PDF开始启动...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            System.out.println("打开文档:" + source);
            Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
            System.out.println("转换文档到PDF:" + target);
            File tofile = new File(target);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
            Dispatch.call(doc, "Close", false);
            long end = System.currentTimeMillis();
            System.out.println("转换完成,用时:" + (end - start) + "ms");
            return true;
        } catch (Exception e) {
            System.out.println ( "Word to PDF Error:" + e.getMessage ());
            return to false;
        } the finally {
            IF (! App = null) {
                app.invoke ( "the Quit", the wdDoNotSaveChanges);
            }
        }
    }
 
}
Note: a decentralized e test.doc word file in the disc, after the above code conversion is successful shown below:

3.3 start reading pdf:

3.3.1 to import the java read pdf file page number jar, download address: http: //download.csdn.net/download/tiandixuanwuliang/10007010

3.3.2 code is as follows:

package testJavaReadWordpage;
 
import java.io.IOException;
 
import com.itextpdf.text.pdf.PdfReader;
 
public class GetPdfpage {
    public static int getPdfPage(String filepath){
        int pagecount = 0;    
        PdfReader reader;
        try {
            reader = new PdfReader("e:\\test1.pdf");
            pagecount= reader.getNumberOfPages(); 
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(pagecount);
        return pagecount;
    }
}
注:读取成功如下:

3.4 Summary: word page using java accurate reading, by way of first word into pdf, pdf page and then read, tested, obtained by the method of the present p word accuracy was 100%

[Project]: http: //download.csdn.net/download/tiandixuanwuliang/10007015

Note: Although the code looks difficult, but I also studied half a month only to find out this method, which download a lot of useless resources, tried many methods, so in this article some tools, projects are set up to download scores, also I urge everyone to understand, I hope that we can support, I will do our best to write a better blog.

Fourth, if there is an error, please see the following:

4.1 If the error: java.lang.UnsatisfiedLinkError: no jacob-1.14-x86 in java.library.path description is jacob.dll file error, may be the median error, the 64 replaced 32; there may be jacob.dll put in the position in question, specific look: http: //blog.csdn.net/li346985170/article/details/38365889

4.2 If the error: Unsupported major.minor version 51.0, jdk version is wrong, please use jdk1.7 and above, the specific methods see: https: //jingyan.baidu.com/article/bea41d437363bdb4c51be6e3.html

4.3 If the program has been unable to successfully transfer, check, whether the word computer is installed, whether the transfer pdf plug-in installation. Also, you can put jacod.all and jacod.jar two bin files in the jre, jdk's bin and C: \ Windows \ System32 put down everywhere, but be aware that 32-bit or 64-bit (sometimes call may have question, I am not very familiar with the underlying java, anyway, after these operations, you can turn the success of the pdf).

4.4 If the error: Could not initialize class com.jacob.activeX.ActiveXComponent jacob.jar please copy the file to the C: / windows / System32 in
-------------------- - 
author: Hyun dance world 
source: CSDN 
original: https: //blog.csdn.net/tiandixuanwuliang/article/details/71298406 
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/zp357252539/article/details/91871384