博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用aspose.words 18.6实现pdf文档转换
阅读量:2168 次
发布时间:2019-05-01

本文共 5627 字,大约阅读时间需要 18 分钟。

使用aspose试问pdf文档转换

jar包引入

AsposeJavaAPI
Aspose Java API
https://repository.aspose.com/repo/
com.aspose
aspose-words
18.6

水印处理

因为aspose是收费的,如果大家经济实力允许的话建议在官网购买授权的版本。

水印破解jar包下载链接

代码demo

/** * doc文档转换问pdf */public class Word2PdfUtil {
public static void main(String[] args) {
doc2pdf("d:\\mnt\\uploads\\test.docx","d:\\mnt\\uploads\\test.pdf");// docx2doc("d:\\mnt\\uploads\\test.docx","d:\\mnt\\uploads\\test.doc");// System.out.println(getWordPages("d:\\mnt\\uploads\\test.docx")); } /** * check license * * @return */ public static boolean getLicense() {
boolean result = false; try {
/** * license.xml应放在..\WebRoot\WEB-INF\classes路径下 */ InputStream is = Test.class.getClassLoader().getResourceAsStream("com.aspose.words.lic_2999.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) {
e.printStackTrace(); } return result; } /** * doc to pdf * * @param inPath * @param outPath */ public static void doc2pdf(String inPath, String outPath) {
Logs.info("开始进行doc转pdf文件操作"); Logs.info("inPath >> " + inPath); Logs.info("outPath >> " + outPath); // 验证License 若不验证则转化出的pdf文档会有水印产生 if (!Word2PdfUtil.getLicense()) {
return; } try {
long old = System.currentTimeMillis(); // 新建一个空白pdf文档 File file = new File(outPath); if (!file.canRead()) {
file.setReadable(true); } if (!file.canWrite()) {
file.setWritable(true); } FileOutputStream os = new FileOutputStream(file); // Address是将要被转化的word文档 Document doc = new Document(inPath); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, doc.save(os, SaveFormat.PDF); // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); // 转化用时 Logs.info("共耗时:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) {
Logs.error("文件转换异常:" + e.getMessage(), e); } } public static void docx2doc(String docxPath, String docPath) {
Logs.info("开始进行docx转doc文件操作"); Logs.info("docxPath >> " + docxPath); Logs.info("docPath >> " + docPath); // 验证License 若不验证则转化出的pdf文档会有水印产生 if (!Word2PdfUtil.getLicense()) {
return; } try {
long old = System.currentTimeMillis(); // 新建一个空白pdf文档 File file = new File(docPath); if (!file.canRead()) {
file.setReadable(true); } if (!file.canWrite()) {
file.setWritable(true); } FileOutputStream os = new FileOutputStream(file); // Address是将要被转化的word文档 Document doc = new Document(docxPath); // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, doc.save(os, SaveFormat.DOC); // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); // 转化用时 Logs.info("共耗时:" + ((now - old) / 1000.0) + "秒"); } catch (Exception e) {
Logs.error("文件转换异常:" + e.getMessage(), e); } } /** * 获取word文件页码数 * * @param filePath 文件路径 * @return */ public static Integer getWordPages(String filePath) {
try {
//需要将文档转成pdf再读取。当我们的文档较大时使用这种方式会效率会比较慢,需要优化处理 //但目前我们平台的文章不会太大,所以暂时不考虑 String pdfPath = filePath.replace(".doc", ".pdf").replace(".docx", ".pdf"); Word2PdfUtil.doc2pdf(filePath, pdfPath); int pageSize = 0; Document dc = new Document(pdfPath); Logs.info(">>>>" + dc.getPageCount());// new File(pdfPath).delete();//读取完成后将文件删除 return pageSize; } catch (Exception e) {
Logs.error("获取文件页数异常:" + e.getMessage(), e); } return 0; } /** * 获取分页数 * * @param file * @return */ public static Integer getWordPages(File file) {
return Word2PdfUtil.getWordPages(file.getPath()); } /** * 将文件转换成pdf,在原路径保留一份pdf文件 * * @param file */ public static void doc2pdf(File file) {
if (file == null) {
return; } String docPath = file.getPath(); String pdfPath = docPath.replace(".doc", ".pdf").replace(".docx", ".pdf"); Word2PdfUtil.doc2pdf(docPath, pdfPath); } /** * doc 转 pdf * * @param docFile * @param pdfFile */ public static void doc2pdf(File docFile, File pdfFile) {
String docPath = docFile.getPath(); String pdfPath = pdfFile.getPath(); Word2PdfUtil.doc2pdf(docPath, pdfPath); } /** * doc 转 pdf * * @param docxFile * @param docFile */ public static void docx2doc(File docxFile, File docFile) {
String docxPath = docxFile.getPath(); String pdfPath = docFile.getPath(); Word2PdfUtil.docx2doc(docxPath, pdfPath); } /** * doc 转 pdf * * @param docxFile */ public static void docx2doc(File docxFile) {
String docxPath = docxFile.getPath(); String pdfPath = docxPath.replace(".docx", ".doc"); Word2PdfUtil.docx2doc(docxPath, pdfPath); }}

转载地址:http://vjvzb.baihongyu.com/

你可能感兴趣的文章
一个框架解决几乎所有机器学习问题
查看>>
特征工程怎么做
查看>>
机器学习算法应用中常用技巧-1
查看>>
机器学习算法应用中常用技巧-2
查看>>
通过一个kaggle实例学习解决机器学习问题
查看>>
决策树的python实现
查看>>
Sklearn 快速入门
查看>>
了解 Sklearn 的数据集
查看>>
用ARIMA模型做需求预测
查看>>
推荐系统
查看>>
TensorFlow-11-策略网络
查看>>
浅谈 GBDT
查看>>
如何选择优化器 optimizer
查看>>
一文了解强化学习
查看>>
CART 分类与回归树
查看>>
seq2seq 的 keras 实现
查看>>
seq2seq 入门
查看>>
什么是 Dropout
查看>>
用 LSTM 做时间序列预测的一个小例子
查看>>
用 LSTM 来做一个分类小问题
查看>>