jar:

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

在tomcat上使用时要在tomcat使用的jdk的jdk/jre/bin目录下放置配套的jacob.dll文件

import java.io.File;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.ComThread;

import com.jacob.com.Dispatch;

public class WordtoPdfUtil {

static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。

static final int wdFormatPDF = 17;// PDF 格式

/**

* word转pdf

* @param wordSrc    word路径

* @param pdfSrc     另存为pdf后的路径

*/

public static void  wordToPdf(String wordSrc,String pdfSrc){

long start = System.currentTimeMillis();

ActiveXComponent app = null;

Dispatch docs=null;

try {

System.runFinalizersOnExit(true);

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

app.setProperty("Visible", false);

docs = app.getProperty("Documents").toDispatch();

System.out.println("打开文档" + wordSrc);

Dispatch doc = Dispatch.call(docs,//

"Open", //

wordSrc,// FileName

false,// ConfirmConversions

true // ReadOnly

).toDispatch();

System.out.println("转换文档到PDF" + pdfSrc);

File tofile = new File(pdfSrc);

//如果输出目标文件夹不存在,则创建

if (!tofile.getParentFile().exists()){

tofile.getParentFile().mkdirs();

}

Dispatch.call(doc,//

"SaveAs", //

pdfSrc, // FileName

wdFormatPDF);

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

long end = System.currentTimeMillis();

System.out.println("转换完成..用时:" + (end - start) + "ms.");

} catch (Exception e) {

e.printStackTrace();

System.out.println("========Error:文档转换失败:" + e.getMessage());

} finally {

if (app != null){

app.invoke("Quit", wdDoNotSaveChanges);

}

if(docs != null){

ComThread.Release();

ComThread.RemoveObject(docs);

}

}

}

public static void main(String[] args) {

WordtoPdfUtil.wordToPdf("C:\\Users\\Administrator\\Desktop\\qcs.docx","C:\\Users\\Administrator\\Desktop\\qcs.pdf");

}

}

WordtoPdfUtil word转pdf的更多相关文章

  1. 【源码】Word转PDF V1.0.1 小软件,供新手参考

    昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~ 开源地址:https://github.com/dunitian/WordConvertPDF 软 ...

  2. java word 转 pdf

    这里使用jacob将word转pdf,使用的是jacob.jar import java.io.File;import com.jacob.activeX.ActiveXComponent;impor ...

  3. C#实现 word、pdf、ppt 转为图片

    office word文档.pdf文档.powerpoint幻灯片是非常常用的文档类型,在现实中经常有需求需要将它们转换成图片 -- 即将word.pdf.ppt文档的每一页转换成一张对应的图片,就像 ...

  4. Aspose 强大的服务器端 excel word ppt pdf 处理工具

    Aspose 强大的服务器端 excel word ppt pdf 处理工具 http://www.aspose.com/java/word-component.aspx

  5. word转pdf字体格式变乱的问题

    完成word转pdf的功能之后,本地测试没问题,然后发布到服务器上,就遇到了字体变乱的问题,如下: 由于我本地发布后导出没有出现同样情况,而服务器和本地的最大区别在于字体库,于是,把服务器上关于需要用 ...

  6. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  7. winform实现word转换为PDF(.doc)

    注意:实现word转换为PDF文件,本人安装Office为2013; word以后缀为.doc为例实现文件类型转换,具体方式如下所示: 实现步骤: 1.添加命名空间引用——using Microsof ...

  8. 使用aspose.word两句代码将word转换为pdf

    //Load Document Document document = new Document(@"C:\Users\Administrator\Desktop\人事---新员工转正总结( ...

  9. jacob 操作word转pdf

    项目需要对上传的word及pdf进行在线预览,因基于jquery的pdf插件,很方面实现在线预览,而word实现在线预览费劲不少,于是想到在进行上传处理时,直接将word转成pdf,在预览时直接预览p ...

随机推荐

  1. redis实现消息队列-java代码实现

    转:https://blog.csdn.net/qq_42175986/article/details/88576849 pom.xml <!-- redis依赖 --> <depe ...

  2. Python决策树可视化:GraphViz's executables not found的解决方法

    参考文献: [1]Python决策树可视化:GraphViz's executables not found的解决方法

  3. 拖拽插件SortableJS

    在项目中,经常会遇到一些涉及到拖拽的需求,github上面有一个开源的SortableJS的插件,支持Vue,React,Angular等多种框架,实现效果很好,基本可以满足大部分的需求,下面就第一次 ...

  4. DT企业新闻也叫公司新闻简介调取方案

    今天我们讲的是企业新闻简介的事,由于destoon官方比较懒,企业新闻没有开发这个截字功能,我们就变通思维直接调取内容前100字,但是由于企业新闻是2个不同的 表,所以我们必须做点小事,  就是写点p ...

  5. egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; consider upgrading MySQL client

    egg 连接 mysql 的 docker 容器,报错:Client does not support authentication protocol requested by server; con ...

  6. woocommerce调用产品相册gallery图片如何操作?wordpress技巧

    wordpress官网有很多woocommerce模板,但有些客户要求定制模板,这时可能会碰到产品相册图片调用的问题,如果根据自带的Storefront主题来改很麻烦,那我们就自己定义吧!下来就随yt ...

  7. GridControl 使用方法篇 --- 隐藏表头、隐藏Group by Box

  8. LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...

  9. STL next_permutation 全排列

    调用方法: ]={,,,}; )){ ;i<;i++) printf("%d ",arr[i]); puts(""); } 测试效果: 注:可以看到1 2 ...

  10. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...