使用jacob将word转成PDF

整体思路参考http://www.iteye.com/topic/588050

 

上面的这篇文章使用jacob将word转换成HTML的,利用的是Word的另存为功能,在Office 2007 SP2之后,Office就可以另存为PDF了,可以使用这个方法将office另存为PDF文档。

 

具体代码可以参考上文里面的,另存为哪种类型是由new variant()里面的参数决定的。

 

            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]);  

new Variant(),这里面的根据传入的参数不同,可以另存为不同的类型,但是在网上搜索了一个并没有找到有关这个参数类型的一个说明,自己尝试了一下,结果如下:

 

 

0

Doc

1

Dot

2-5

Txt

6

Rtf

7

Txt

810

htm

11

Xml

1216

Docx

13

Docm

14

Dotx

15

Dotm

17

Pdf

 

我使用的是office 2010,不同版本的对应的应该不一样,我是写了这一小段程序来测试另存为的类型的。

 

public class JacobTest {
	public static void wordToPDF(String docfile, String toFile,int type) {  
        ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word  
        try {  
            app.setProperty("Visible", new Variant(false));  
            Dispatch docs = app.getProperty("Documents").toDispatch();  
            Dispatch doc = Dispatch.invoke(  
                    docs,  
                    "Open",  
                    Dispatch.Method,  
                    new Object[] { docfile, new Variant(false),  
                            new Variant(true) }, new int[1]).toDispatch();  
            //new Variant(type),这里面的type的决定另存为什么类型的文件
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {  
                    toFile, new Variant(type) }, new int[1]);  
            Variant f = new Variant(false);  
            Dispatch.call(doc, "Close", f);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            app.invoke("Quit", new Variant[] {});  
        }  
    }  
	
	public static void main(String[] args) {
		//源文件全路径
		String docfile ="D:\\服务实施描述报告(企业门户).docx";
		for (int i = 0; i < 18; i++) {	
			//些路径test为实际存在的目录,s后面为要另存为的文件名
			String toFile="d:\\test\\s"+i;
			wordToPDF(docfile, toFile,i);
		}		
	}
}
<think>好的,我现在需要帮助用户找到使用Jacob API将Word换为PDF的方法。首先,用户明确提到了Jacob,所以我要先确认Jacob是什么以及它如何工作。根据引用中的信息,Jacob是一个Java与COM组件之间的桥梁,允许Java程序调用微软的COM组件,比如Office应用程序。 接下来,我需要查看用户提供的引用内容。引用[2]详细介绍了如何使用JacobWord换为PDF,包括环境配置和代码示例。引用[3]也提到了JCom的实现,但用户特别问到了Jacob,所以应重点关注引用[2]和可能的引用[3]中的Jacob部分。 用户需要的是具体的API使用方法和步骤。首先,环境配置是关键。根据引用[2],必须安装Microsoft Word和Adobe Acrobat,并且配置Jacob的DLL文件到系统路径中。此外,还需要在代码中引入Jacob的库。 然后,代码实现部分。引用[2]中的示例代码可能已经过时,但引用[3]中的代码片段展示了如何使用IDispatch调用Acrobat的API。不过用户更可能需要的是直接操作Word的COM接口来换文档为PDF,而不是通过Acrobat。引用[4]提到了另一种方法使用Apache POI和PDFConverter,但用户明确要求Jacob,所以需要排除这个方法。 需要确保代码示例的正确性。Jacob通常通过调用Word的SaveAs方法,将文档保存为PDF格式。步骤包括启动Word应用程序,打开文档,然后使用SaveAs2方法指定文件格式为PDF。最后要正确释放COM对象以避免内存泄漏。 此外,用户可能需要处理不同版本的Office和Acrobat的兼容性问题,如引用[2]中提到的Adobe Acrobat 7.0.5的特定设置。需要提醒用户注意这些环境依赖,否则换可能会失败。 最后,总结步骤:配置环境、引入Jacob库、编写Java代码调用Word的COM组件、处理异常和资源释放。同时,要提供相关的问题供用户进一步了解,比如性能优化或错误处理。</think>### 使用Jacob实现WordPDF的API方法 通过Jacob调用Microsoft Word的COM接口实现文档PDF,需配置环境并遵循以下步骤: #### 一、环境准备 1. **安装依赖软件** - Microsoft Office(需包含Word组件) - Adobe Acrobat Professional(部分版本需关闭“不发送字体到PDF”选项[^2]) 2. **配置Jacob库** - 下载`jacob.jar`和`jacob-1.xx-x64.dll`(根据系统位数选择) - 将DLL文件放入系统PATH路径(如`C:\Windows\System32`) #### 二、核心代码实现 ```java import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordToPdfConverter { public static void convert(String inputPath, String outputPath) { ActiveXComponent wordApp = null; try { // 启动Word应用程序 wordApp = new ActiveXComponent("Word.Application"); wordApp.setProperty("Visible", new Variant(false)); // 后台运行 // 打开文档 Dispatch documents = wordApp.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Open", inputPath).toDispatch(); // 另存为PDF(文件格式代码17代表PDF) Dispatch.call(document, "SaveAs2", outputPath, new Variant(17)); // 关闭文档和Word应用 Dispatch.call(document, "Close", false); wordApp.invoke("Quit", 0); } catch (Exception e) { e.printStackTrace(); } finally { if (wordApp != null) { wordApp.safeRelease(); } } } } ``` #### 三、关键参数说明 - **SaveAs2格式代码**: `17`对应PDF格式,其他格式可通过MSDN查询[^3] - **错误处理**: 若出现字体缺失问题,需检查Acrobat的字体嵌入设置[^2] #### 四、典型调用方式 ```java WordToPdfConverter.convert("D:/test.doc", "D:/output.pdf"); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值