2019-06-27 POI java代码实现word转PDF

jar包下载路径:http://www.onlinedown.net/soft/988800.htm

实现代码如下:

package com.voucher.manage2.utils;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import java.io.File;

public class Word2pdf02 {

public static void wToPdfChange(String wordFile,String pdfFile){

//wordFile word 的路径  //pdfFile pdf 的路径

        ActiveXComponent app =null;

        System.out.println("开始转换...");

        // 开始时间

// long start = System.currentTimeMillis();

        try {

// 打开word

            app =new ActiveXComponent("Word.Application");

            // 获得word中所有打开的文档

            Dispatch documents = app.getProperty("Documents").toDispatch();

            System.out.println("打开文件: " + wordFile);

            // 打开文档

            Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();

            // 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在

            File target =new File(pdfFile);

            if (target.exists()) {

target.delete();

            }

System.out.println("另存为: " + pdfFile);

            Dispatch.call(document, "SaveAs", pdfFile, 17);

            // 关闭文档

            Dispatch.call(document, "Close", false);

        }catch(Exception e) {

System.out.println("转换失败"+e.getMessage());

        }finally {

// 关闭office

            app.invoke("Quit", 0);

        }

}

public static void main(String[] args) {

String word ="src\\main\\java\\com\\voucher\\docx\\01.docx";

        String name ="02".concat(".pdf");

        String pdf ="src\\main\\java\\com\\voucher\\docx\\"+name;

    wToPdfChange(word, pdf);

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容