Springboot WORD转换为PDF——Aspose方式
Springboot WORD转换为PDF——Aspose方式
一、准备相关资源
1.下载转换的Aspose资源包
网盘地址
提取码:dddc
2. 下载License文件去除水印
网盘地址
提取码:yesg
二、配置maven
1. 将下载下来的jar包放在项目根目录中
2.将下载下来的license.xml放在根目录的resources下
3. 配置pom.xml
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${
project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
三、编写代码
public class Word2PdfAsposeUtil {
public static boolean getLicense() {
boolean result = false;
InputStream is = null;
try {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:license.xml");
is = resources[0].getInputStream();
// Resource resource = new ClassPathResource("license.xml");
// is = resource.getInputStream();
//InputStream is = Word2PdfAsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* word 转pdf
*
* @param inPath word路径
* @param outPath pdf路径
* @return
*/
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
long old = System.currentTimeMillis();
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
Document doc = new Document(inPath); // Address是将要被转化的word文档
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
}
四、编写main方法测试
public static void main(String[] args) {
doc2pdf("E:\\123\\1.docx","E:\\123\\1.pdf");
}
还没有评论,来说两句吧...