关闭

使用Jacob实现word转PDF

471人阅读 评论(0) 收藏 举报
分类:

1.  引入jacob.jar


2.  将jacob-1.18-M2-x64.dll   jacob-1.18-M2-x86.dll 拷贝到C:\Windows\System32下


3. 编码

package com.base.pf.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * PDF word转换PDF;PDF下载
 * 
 * @author ZHEN.L
 * @date 2014.11.17
 * 
 */
public class PDFUtils {

	static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
	static final int wdFormatPDF = 17;// PDF 格式

	/**
	 * WORD 转换成 PDF
	 * 
	 * @param sourcePath
	 * @param targetPath
	 */
	public static void Word2PDF(String sourcePath, String targetPath) {
		// String filename = "F:/22.doc";
		// String toFilename = "F:/a.pdf";
		long start = System.currentTimeMillis();
		ActiveXComponent app = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", false);
			Dispatch docs = app.getProperty("Documents").toDispatch();
			System.out.println("打开文档..." + sourcePath);
			Dispatch doc = Dispatch.call(docs,//
					"Open", sourcePath, false, true).toDispatch();
			System.out.println("转换文档到PDF..." + targetPath);
			File tofile = new File(targetPath);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc, "SaveAs", targetPath, wdFormatPDF);
			Dispatch.call(doc, "Close", false);
			long end = System.currentTimeMillis();
			System.out.println("转换完成..用时:" + (end - start) + "ms.");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (app != null)
				app.invoke("Quit", wdDoNotSaveChanges);
		}
	}

	/**
	 * 下载PDF
	 * 
	 * @param request
	 * @param response
	 * @param path
	 */
	public static void download(HttpServletRequest request,
			HttpServletResponse response, String path) {
		String pdfPath = path.replace(".doc", ".pdf");
		Word2PDF(path, pdfPath);
//		path = "f:/1.pdf";
		File file = new File(pdfPath);
		String fileName = file.getName();
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(file);
			response.setContentType("application/pdf,charset=UTF-8");
			response.setHeader("Content-disposition", "attachment;filename="
					+ URLEncoder.encode(fileName, "UTF-8"));
			os = response.getOutputStream();
			byte[] bytes = new byte[1024];
			while (is.read(bytes) != -1) {
				os.write(bytes, 0, bytes.length);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (os != null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:90927次
    • 积分:1629
    • 等级:
    • 排名:千里之外
    • 原创:88篇
    • 转载:0篇
    • 译文:0篇
    • 评论:11条
    最新评论