Java实现doc转docx

jacob这个包可以实现此功能

jacob-1.19-x86.dll  
jacob-1.19-x86.dll
这两个文件放到system32下或者java的jre的bin目录下
public static String convertDocx(String filePath,String descPath) {
    ComThread.InitMTA();
    ActiveXcomponent app = new ActiveXcomponent("Word.Application");
    try{
    app.setProperty("Visible",new Variant(false));
    //实例化模板
    Dispatch document = app.getProperty("Documents").toDispatch();
    //打开document进行零存在操作
    Dispatch doc = Dispatch.invoke(document,"Open",Dispatch.Method,new Object[]{filePath,new Variant(true)},new int[1]).toDispatch();
    Dispatch.invoke(doc,"SaveAs",Dispatch.Method,new Object[]{descPath,new Variant(12)},new int[1]);
    Dispatch.call(doc,"Close",new Variant(false));
    return descPath;
    }catch(Exception e){
    throw new Exception();
    }finally{
    app.invoke("Quit",new Variant[]{});
    ComThread.Release();
    }
 }
package cn.xu.users.config.aspect;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

public class WordFileToPdf {
    private static final int wdFormatPDF = 17;
    private static final int xlTypePDF = 0;
    private static final int ppSaveAsPDF = 32;
    private static final String PATH = "D://";
 
    /***
     * 判断需要转化文件的类型(Excel、Word、ppt)
     *
     * @param inputFile
     */
    public static String convertToPDF(String inputFile) {
        //判断原文件是否存在
        File file = new File(PATH + inputFile);
        if (file.exists()) {
            String kind = getFileSufix(inputFile);
            if (kind.equals("pdf")) {
                return inputFile;//原文件就是PDF文件
            }
            String pdfFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";
            if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) {
                wordToPDF(PATH+inputFile, PATH+pdfFile);
            }else if (kind.equals("ppt")||kind.equals("pptx")||kind.equals("pptm")||kind.equals("ppsx")) {
                pptToPDF(PATH+inputFile, PATH+pdfFile);
            }else if(kind.equals("xls")||kind.equals("xlsx")){
                ExToPDF(PATH+inputFile, PATH+pdfFile);
            }else{
                return inputFile;//原文件是其它格式文件
            }
            //返回创建的pdf
            return pdfFile;
        } else {
            System.out.println("原文件不存在!");
            return inputFile;
        }
    }
 
    /***
     * 判断文件类型
     *
     * @param fileName
     * @return
     */
    public static String getFileSufix(String fileName) {
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }
 
    /***
     *
     * Word转PDF
     *
     * @param inputFile
     * @param pdfFile
     * @return
     */
    private static void wordToPDF(String inputFile, String pdfFile) {
        try {
            ComThread.InitSTA(true);
            // 打开Word应用程序
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            // 设置Word不可见
            app.setProperty("Visible", new Variant(false));
            // 禁用宏
            app.setProperty("AutomationSecurity", new Variant(3));
            // 获得Word中所有打开的文档,返回documents对象
            Dispatch docs = app.getProperty("Documents").toDispatch();
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true).toDispatch();
            // word保存为pdf格式宏,值为17
            Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);
            // 关闭文档
            Dispatch.call(doc, "Close", false);
            // 关闭Word应用程序
            app.invoke("Quit", 0);
            ComThread.Release();
            ComThread.quitMainSTA();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /***
     *
     * Excel转化成PDF
     *
     * @param inputFile
     * @param pdfFile
     * @return
     */
    private static void ExToPDF(String inputFile, String pdfFile) {
        try {
            ComThread.InitSTA(true);
            ActiveXComponent ax = new ActiveXComponent("Excel.Application");
            ax.setProperty("Visible", false);
            ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = ax.getProperty("Workbooks").toDispatch();
 
            Dispatch excel = Dispatch
                    .invoke(excels, "Open", Dispatch.Method,
                            new Object[] { inputFile, new Variant(false), new Variant(false) }, new int[9])
                    .toDispatch();
            // 转换格式
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[] { new Variant(0),pdfFile, new Variant(xlTypePDF)}, new int[1]);
 
            Dispatch.call(excel, "Close", new Variant(false));
            if (ax != null) {
                ax.invoke("Quit", new Variant[] {});
            }
            ComThread.Release();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /***
     * ppt转化成PDF
     *
     * @param inputFile
     * @param pdfFile
     * @return
     */
    private static void pptToPDF(String inputFile, String pdfFile) {
        try {
            ComThread.InitSTA(true);
            ActiveXComponent app = new ActiveXComponent("Ppt.Application");
            Dispatch ppts = app.getProperty("Presentations").toDispatch();
            Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,false).toDispatch();
            Dispatch.invoke(ppt, "SaveAs", Dispatch.Method, new Object[]{
                    pdfFile,new Variant(ppSaveAsPDF)},new int[1]);
            Dispatch.call(ppt, "Close");
            app.invoke("Quit");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String s = convertToPDF("ww.doc");
        System.out.println(s);
    }

}
链接:https://pan.baidu.com/s/1gBmc5L-PV7aQfaSDefVABg 
提取码:1111

你可能感兴趣的:(java,java,开发语言,后端)