使用Jacob将Word转为PDF

本文介绍如何使用Java和Jacob库将Microsoft Word文档转换为PDF格式。需安装特定软件如Adobe Acrobat,并配置虚拟打印机。示例代码展示了从打开Word文档到最终生成PDF文件的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本博客 jacob 列文章导读
Java操作Microsoft Word之jacob
jacob使用入门及问题解析
使用Jacob将Word转为Html或txt
使用Jacob将Word转为PDF
java通过jacob调用word (根据Word模板生成动态内容)

准备工作:

1. 安装 "Adobe Acrobat 7.0 Professional" 并升级到"7.0.5"

2.安装"gs811w32.rar" (默认安装就可以了,它是一个PDF转换时所需要的脚本)

3.安装"postscript.rar" (默认安装就可以,它其实是个PDF虚拟打印机的驱动)

4.虚拟打印机配置,参考http://www.matrix.org.cn/thread.shtml;jsessionid=B1E4B57897D51B59802D353CB6B32ACC?topicId=29594&forumId=17

一点需要稍微留意:wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));
这行代码中的"MS Publisher Color Printer"对应安装的虚拟打印机名称,请用以下代码测试。

1》要用到的软件:
(1)Adobe Acrobat 8 Professional (最低版本7.03)
(个人非商业使用)8.0破解版下载地址:http://green.crsky.com/soft/2205.html (记得下载补丁)
安装文件 http://down1.greendown.cn//200611/AcroPro80_efg.rar
破解 http://soft.greendown.cn//200611/AcroPro80_Crack.rar
(2)gs811w32.rar (PDF转换时所需要的脚本ps)
http://www.allmail.com.cn/gs811w32.rar
(3)postscript.rar (PDF虚拟打印机的驱动)
http://www.pdfhome.com.cn/Resource/DownLoad/postscript.rar
(4)jacob.jar
jacob_1.9.zip
(5)office 2003

2》原理:
jacob.jar
doc --> ps --> pdf --> office 2003 --> gs811w32 -->Adobe Acrobat 8 --> postscript --> 打印机
(其中关于jacob,jar的安装请看:jacob使用入门及问题解析)

3》安装运行:
(1)安装 Adobe Acrobat 8 Professional

(2)安装 gs811w32.rar

(3)配置打印机(这里不需要真实的打印机)
控制面板》 打印机及其他硬件》打印机和传真》添加打印机
(如果添加时显示“操作无法完成。打印后台程序服务没有运行。”
请打开控制面板》性能和维护》管理工具》服务》找到“Print Spooler”
》右击属性》启动)》选择本地打印机(如果没有打印机请将“检测并安装
即插打印机”的钩去掉)》下一步》选择“使用以下端口”
(My Document/*.pdf (Adobe PDF Port))》下一步 选择打印机》我选择
的是Apple的 Color LaserWriter 12/600(工作后,有钱一定要买个Apple hp)
》下一步(记住打印机的名字:Apple Color LaserWriter 12/600)
》下一步(没有打印机的朋友请选择:不测试)

(4)安装 postscript.rar (安装时,注意每一步,选择与前面设置相关的选项)

(5)设置Adobe Acrobat 8 Professional:选择一个pdf文件,右击打开方式选择
使用 打开Adobe Acrobat 8 Professional》选择file菜单》Print Setup...》打印选项
属性“Apple Color LaserWriter 12/600”》确定

(6)运行下面的代码:

/** * @author XuMing Li * * @version 1.00, 2007-4-9 * */ import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class D2P { private ActiveXComponent wordCom = null; private Object wordDoc = null; private final Variant False = new Variant(false); private final Variant True = new Variant(true); /** * 打开word文档 * @param filePath * word文档 * @return 返回word文档对象 */ public boolean openWord(String filePath) { // 建立ActiveX部件 wordCom = new ActiveXComponent("Word.Application"); try { // 返回wrdCom.Documents的Dispatch Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch(); // 调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method, new Object[] { filePath }, new int[1]).toDispatch(); return true; } catch (Exception ex) { ex.printStackTrace(); } return false; } /** * 关闭word文档 */ public void closeWord() { // 关闭word文件 wordCom.invoke("Quit", new Variant[] {}); } /** * * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 * * * @param sourceFilePath * 源文件路径 * * @param destinPSFilePath * 首先生成的PS文件路径 * * @param destinPDFFilePath * 生成PDF文件路径 */ public void docToPDF(String sourceFilePath, String destinPSFilePath, String destinPDFFilePath) { if (!openWord(sourceFilePath)) { closeWord(); return; } // 建立Adobe Distiller的com对象 ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1"); try { // 设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF" wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer")); // 设置printout的参数,将word文档打印为postscript文档。目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api // 是否在后台运行 Variant Background = False; // 是否追加打印 Variant Append = False; // 打印所有文档 int wdPrintAllDocument = 0; Variant Range = new Variant(wdPrintAllDocument); // 输出的postscript文件的路径 Variant OutputFileName = new Variant(destinPSFilePath); Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] {Background, Append, Range, OutputFileName }); System.out.println("由word文档打印为ps文档成功!"); // 调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册作为输入的ps文档路径 Variant inputPostScriptFilePath = new Variant(destinPSFilePath); // 作为输出的pdf文档的路径 Variant outputPDFFilePath = new Variant(destinPDFFilePath); // 定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件 Variant PDFOption = new Variant(""); // 调用FileToPDF方法将ps文档转换为pdf文档 Dispatch.callN(distiller, "FileToPDF", new Variant[] {inputPostScriptFilePath, outputPDFFilePath, PDFOption }); System.out.println("由ps文档转换为pdf文档成功!"); } catch (Exception ex) { ex.printStackTrace(); } finally { closeWord(); wordCom = null; // 释放在程序线程中引用的其它com,比如Adobe PDFDistiller ComThread.Release(); } } public static void main(String[] argv) { D2P d2p = new D2P(); d2p.docToPDF("d:/12.doc", "d:/1p.ps", "d:/1p.pdf"); // 这里是你建一个叫12.doc的word文档,生成的文档将在D盘下 // 1p.ps和1p.pdf(这是我们要的) } }

博文来源:http://blog.youkuaiyun.com/gavin_sw/archive/2007/04/11/1561254.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值