java中word转pdf实现

基于Jacob+SaveAsPDFandXPS+MicrosoftOfficeWord实现word转pdf
优点:功能强大,转换效果好,支持格式全
缺点:转换速度慢,只支持windows环境,需要有office软件,以及需要加装插件
Jacob:JACOB一个Java-COM中间件,通过这个组件你可以在Java应用程序中调用COM组件和Win32程序库。
SaveAsPDFandXPS:SaveAsPDFandXPS是微软官方出品,免费且小巧,可直接将word文档(docx/doc)保存为pdf或者xps。

本套方案中,所有匹配文件的下载地址:Jacob+SaveAsPdfAndXps
包含内容:​​在这里插入图片描述

第一步:安装SaveAsPDFandXPS插件

在这里插入图片描述

第二步:把dll文件放到jre的bin目录下

在这里插入图片描述
电脑是64位的就用x64的,32位的就用x86的
在这里插入图片描述

第三步:引入jacob.jar包

方法一:放到jre的lib路径下
在这里插入图片描述
方法二:直接引入项目
Java项目直接build进去;
maven项目引入代码(jacob包在阿里maven仓库中没有,要用中央仓库的地址:https://mvnrepository.com/)

<dependency>
    <groupId>com.jacob</groupId>
    <artifactId>jacob</artifactId>
    <version>1.10</version>
</dependency>

第四步:书写代码

package com.jacob.word2pdf;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
 
public class WordToPdf {

    private static final int wdFormatPDF = 17;// PDF 格式    
    /**
     * word转pdf 支持doc, docx
     * @param inputFile 转换前的文件路径
     * @param pdfFile 转换后的文件路劲
     */
    public static void wordToPDF(String startFile, String overFile){    
        ActiveXComponent app = null;  
        Dispatch doc = null;  
        try {      
            app = new ActiveXComponent("Word.Application");      
            app.setProperty("Visible", new Variant(false));  
            Dispatch docs = app.getProperty("Documents").toDispatch();    
            doc = Dispatch.call(docs,  "Open" , startFile).toDispatch();  
            File tofile = new File(overFile);      
            if (tofile.exists()) {      
                    tofile.delete();      
            }      
            Dispatch.call(doc,"SaveAs", overFile, wdFormatPDF);  
        } catch (Exception e) {      
                System.out.println(e.getMessage());      
        } finally {  
                Dispatch.call(doc,"Close",false);  
                if (app != null)      
                        app.invoke("Quit", new Variant[] {});      
        }  
        //结束后关闭进程   
        ComThread.Release();
    }
        
    /**
     * 根据传入文件将word转为pdf    
     * @param wordFile
     * @return
     */
    public static File wordToPDFByFile(File wordFile){   
        File returnFile = null;
        try {
            String inputFilePath = wordFile.getAbsolutePath();
            System.out.println("生成传入临时文件路径: "+inputFilePath);
            String pdfFilePath = inputFilePath.substring(0,inputFilePath.lastIndexOf("."))+1 + "pdf";
            System.out.println("生成pdf文件路径: "+pdfFilePath);
            wordToPDF(inputFilePath, pdfFilePath);
            returnFile = new File(pdfFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnFile;
    }  
    
    public static void main(String[] args) {
      String path = "C://Users//zrc//Desktop//新建 DOCX 文档.docx";
      String newPath = "C://Users//zrc//Desktop//新建 DOCX 文档.pdf";
      wordToPDF(path,newPath);
//      String path = "C://Users//zrc//Desktop//新建 DOCX 文档.docx";
//      File file = new File(path);
//      File pdf = wordToPDFByFile(file);
    }
}


亲测有效!!!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值