java doc转pdf又来了,这一次超简单

java doc转pdf又来了,这一次超简单,不用安装,一个jar包,一个文件搞定。
这里用到了aspose.words.jar



import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

public class Doc2Pdf{
	public static Logger logger = LoggerFactory.getLogger(Doc2Pdf.class);

	public static void main(String[] args) throws Exception {
		doc2pdf("D:\\download\\rent\\test\\R-283.docx", "D:\\download\\rent\\test\\R-284.pdf");
	}

	public static boolean getLicense() throws Exception {
		boolean result = false;
		try {
			InputStream is = com.aspose.words.Document.class.getResourceAsStream("/com.aspose.words.lic_2999.xml");
			License aposeLic = new License();
			aposeLic.setLicense(is);
			result = true;
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	}

	public static void doc2pdf(String inPath, String outPath) throws Exception {
		if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
			throw new Exception("com.aspose.words lic ERROR!");
		}
		logger.info(inPath + " -> " + outPath);
		try {
			long old = System.currentTimeMillis();
			File file = new File(outPath);
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(inPath); // word文档
			// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
			doc.save(os, SaveFormat.PDF);
			long now = System.currentTimeMillis();
			logger.info("convert OK! " + ((now - old) / 1000.0) + "秒");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

jar包下载地址:https://download.csdn.net/download/u011855773/11966221

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值