使用jacob将word转换为pdf(完美保持原格式,仅支持windows环境)

需要先将以下两个文件放在jdk的bin路径下面,如:java1.8\jre\bin
jacob-1.19-x64.dll
jacob-1.19-x86.dll
jacob相关文件下载

package com.qlyl.util;

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 格式

	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();
	}

	public static void main(String[] args) {
		wordToPDF("D:\\word报告模板\\testXml2Word.docx", "D:\\word报告模板\\testWord2Pdf.pdf");
	}
}


发布了6 篇原创文章 · 获赞 0 · 访问量 64
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览