十一、后端:JAVA后台处理WORD转PDF支持WINDOWS以及LINUX系统项目运行

个人说明:
参照博客园前辈用户zsq_fengchen博客,她的原文我只改动了资源获取路径,EXCEL部分进行了注释,目前文章只谈WORD转PDF(需要用到aspose-words-15.8.0-jdk16.jar),我也是调研了很多方法,这位博主的博客解决了我的问题,非常感谢!
编译环境:
window/ssm项目,学到了就毫不吝啬,上代码
一、代码部分

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.aspose.words.Document;
import com.aspose.words.License;

public class WordtoPdf {
	private static boolean getLicense() {
		boolean result = false;
		try {
			Resource resource = new ClassPathResource("license.xml");
			InputStream is = resource.getInputStream();
			License aposeLic = new License();
			aposeLic.setLicense(is);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * @param wordPath
	 *            需要被转换的word全路径带文件名
	 * @param pdfPath
	 *            转换之后pdf的全路径带文件名
	 */
	public static void doc2pdf(String wordPath, String pdfPath) {
		if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
			return;
		}
		try {
			long old = System.currentTimeMillis();
			File file = new File(pdfPath);// 新建一个pdf文档
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(wordPath);// Address是将要被转化的word文档
			doc.save(os, com.aspose.words.SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB,
															// XPS, SWF 相互转换
			long now = System.currentTimeMillis();
			os.close();
			System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");// 转化用时
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void wodrz() {
		// word转为pdf
		String filePaths = "F://word.docx";
		String pdfPath = "F://word.pdf";
		doc2pdf(filePaths, pdfPath);// filePaths需要转换的文件位置 pdfPath为存储位置
	}
}

证书部分(xml文件,引入License.xml文件(备注:此License文件只对于Word版本),将其放入资源目录下)

尊重商业版权,大家还是私聊我吧,全部免费,留言我的csdn加我扣扣发给你
License.xml以及aspose-words-15.8.0-jdk16.jar

大家可以去下载新版的aspose-words-15.8.0-jdk16.jar,听说解决了不少转换问题,实则优化,大家可以去找一下,挺难找的

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值