doc转pdf java不失真_java使用Aspose实现 word文档转pdf文件高效不失真
java使用Aspose word文档转pdf功能实现
主要步骤
使用Aspose进行文档转换,首先引入相应的jar包到系统环境
项目resource下导入license.xml文件
使用Aspose时,需要调用设置License方法,设置完成第一次转换比较慢,再次就不需要设置License方法,效率会更高。
注意:使用Aspose时,每一个模块(words,pdf,cells)都有相同的类,如License类,SaveOptions类,SaveFormat类,功能各不相同。
获取license示例代码:
package com.sinoif.common.utils;
import com.aspose.words.License;
import lombok.extern.slf4j.Slf4j;
import java.io.InputStream;
/**
* @description:
* @author: guanlj
* @date: 2020/7/24 11:10
*/
@Slf4j
public class AsposeLicenseUtil {
private static License aposeLic = new License();
/**
* 获取License的输入流
*
* @return
*/
private static InputStream getLicenseInput() {
InputStream inputStream = null;
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
//获取项目resource下的文件流
inputStream = contextClassLoader.getResourceAsStream("license.xml");
} catch (Exception e) {
log.error("license not found!"+ e);
}
return inputStream;
}
/**
* 设置License
*
* @return true表示已成功设置License, false表示失败
*/
public static boolean setWordsLicense() {
if (!aposeLic.getIsLicensed()) {
try {
aposeLic.setLicense(getLicenseInput());
return aposeLic.getIsLicensed();
} catch (Exception e) {
log.error("set words license error!", e);
}
}
return aposeLic.getIsLicensed();
}
}
word转pdf工具类:
package com.sinoif.common.utils;
import com.aspose.words.*;
import com.aspose.words.Shape;
import lombok.extern.slf4j.Slf4j;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @description:
* @author: guanlj
* @date: 2020/7/24 11:37
*/
@Slf4j
public class WordToPdfUtil {
/**
* word 转 pdf
* @param inputStream word文件流
* @param path 输出pdf 全路径名称
* @return
*/
public static Boolean convert2Pdf(InputStream inputStream, String path) {
try {
if (AsposeLicenseUtil.setWordsLicense()) {
long start = System.currentTimeMillis();
Document doc = new Document(inputStream);
// insertWatermarkText(doc, "测试水印"); // 添加水印
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
// 设置3级doc书签需要保存到pdf的heading中
pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(3);
// 设置pdf中默认展开1级
pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1);
doc.save(path, SaveFormat.PDF);
long end = System.currentTimeMillis();
log.info("convert doc2pdf completed, elapsed " + (end - start) / 1000.0 + " seconds!");
return true;
} else {
return false;
}
} catch (Exception e) {
log.error("convert doc2pdf error!", e);
return false;
}
}
/**
* word 转 pdf
* @param inputStream word文件流
* @param outputStream 输出pdf输出流
* @return
*/
public static Boolean convert2PdfStream(InputStream inputStream, OutputStream outputStream) {
try {
if (AsposeLicenseUtil.setWordsLicense()) {
long start = System.currentTimeMillis();
Document doc = new Document(inputStream);
// insertWatermarkText(doc, "测试水印"); // 添加水印
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
// 设置3级doc书签需要保存到pdf的heading中
pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(3);
// 设置pdf中默认展开1级
pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(1);
//doc.save(outputStream, pdfSaveOptions);
doc.save(outputStream, SaveFormat.PDF);
long end = System.currentTimeMillis();
log.info("convert doc2pdf outputStream completed, elapsed " + (end - start) / 1000.0 + " seconds!");
return true;
} else {
return false;
}
} catch (Exception e) {
log.error("convert doc2pdf outputStream error!", e);
return false;
}
}
/**
* word 转 pdf
* @param inputPath word文件path
* @param outPath pdf文件path
* @return
*/
public static Boolean convert2PdfPath(String inputPath, String outPath) {
try {
if (AsposeLicenseUtil.setWordsLicense()) {
long start = System.currentTimeMillis();
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inputPath);
doc.save(os, SaveFormat.PDF);
long end = System.currentTimeMillis();
log.info("convert doc2pdf completed, elapsed " + (end - start) / 1000.0 + " seconds!");
return true;
} else {
return false;
}
} catch (Exception e) {
log.error("convert doc2pdf error!", e);
return false;
}
}
}
所需jar包和license.xml文件地址:
doc转pdf java不失真_java使用Aspose实现 word文档转pdf文件高效不失真相关推荐
- java设置页码_Java 添加页码到Word文档
前言 在操作Word文档时,可以通过添加页码来使其条理清晰,以便于后期查看整理.通常来说,一个Word文档包含了多个节,我们可以忽视这些节为整个文档添加连续页码,同时也可以根据不同节来设置不连续页码. ...
- word文档转pdf,支持.doc和.docx
word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网查询很多资料,大概思路是正确的,既将.doc文档转成html ...
- java使用Aspose.words实现word文档转pdf文件
引入原因: 感觉openOffice问题还是比较多的. 1. 比如经常出现8100端口问题,虽然kill了就行,但是还是多了一份操作. 2. 出现少数docx无法预览的问题.(已经修改过源码兼容doc ...
- Java 将 Word 文档转换为 PDF 的完美工具
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 来源:https:/ ...
- azw3转换为pdf_干货:如何Java 将 Word 文档转换为 PDF
在日常工作中,PDF格式良好的视觉阅读性和稳定性使其使用越来越广泛.因此我们常会遇到需要将成型的Word文档转换为PDF格式的情况.本文就将通过使用Java程序来演示如何将Word文档转换成PDF格式 ...
- Java将Word文档转换为PDF的完美工具
引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...
- java实现word文档转pdf,并添加水印
前段时间,项目需要自动生成word文档,用WordFreeMarker生成word文档后,又要求生成的文档能在浏览器浏览,思来想去,把word文档转成pdf就好了,于是乎研究了一下. 将word文档转 ...
- Java程序实现Word文档转为pdf以及出现的问题解决
做兽医项目需要用到这种需求,很多程序员都遇到过,有些word文档希望直接在浏览器中打开进行预览,但是浏览器往往不是很配合,直接就提示下载,不像pdf文档,浏览器可以直接进行预览. 1. Word文档转 ...
- Java实现word文档转pdf(无需插件,仅需一个jar包)
实现功能 docx文档转换为PDF 转换之后排版不混乱 依赖 aspose-words-15.8.0-jdk16.jar包提取路径(用于PDF转换 ) 链接:https://pan.baidu.com ...
最新文章
- mysql判断是否建立索引_判断mysql中列是否要添加索引的标准
- python 安装 HTMLtestRunner(转)
- realmeq参数配置详情_小米11什么时候发布 小米11参数配置详情
- mysql添加字段时定义候选键_MySQL 表约束
- JavaFX图表(七)之散点图
- mysql磁盘临时表清理_mysql 创建大量磁盘临时表
- 【注册机】ColorSchemer Studio 2.1.0 注册机
- 16 PP配置-生产计划-主数据-定义MRP区域
- matlab错误使用assert,关于异常处理:何时在Matlab中使用assert()?
- Linux操作Oracle(16)——Oracle扩容报错:ORA-01144_表空间数据文件超出最大限制
- android开发GPS定位测试(附效果图和示例)【转】
- 第04章:MongoDB基本概念
- 最新前端四天精通node.js视频教程
- 海康威视4G球机对接萤石云平台获取直播视频列表 ----- java
- Python(4):上机练习——英文数字字符串打印为阿拉伯数字字符串
- VB中关于RND产生随机数
- 2020春招阿里一面
- 【解决方案】STM32L152单片机驱动段码LCD屏,执行HAL_LCD_Init函数失败返回HAL_TIMEOUT,长时间卡在LCD_FLAG_RDY的while循环里面的解决办法
- 快速恢复未保存的excel文件
- Android 信鸽推送集成
热门文章
- 华为手机ADB调试问题解决
- 山东协和学院计算机考试考点,2017山东计算机等级考试考点联系方式
- lightgbm java_LightGBM两种使用方式
- 使用清华大学镜像源下载
- oracle biee catalog,Biee 迁移和刷新GUIDs
- 2018华迪杯计算机设计大赛,华迪杯第14届中国大学生计算机设计大赛四川省赛在吉利学院成功举办_四川在线...
- C++采用内存映射方式高效率读写大数据
- sql delete
- Gym - 101158J -Cover the Polygon with Your Disk(模拟退火+多边形与圆面积的交)
- Google发布Chrome官方扩展DOM Snitch 可发现网页代码漏洞