org.springframework.boot
spring-boot-maven-plugin
true
4.添加配置文件license.xml到resources下
<?xml version="1.0" encoding="UTF-8" ?>Aspose.Total for Java
Aspose.Words for Java
Enterprise
20991231
20991231
8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
5.工具类如下
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
-
@author Administrator
-
@version I d Id Id
-
@see
-
@since
*/
@Slf4j
public class Word2PdfUtil {
public static String inPath = “D:\upload\test1.docx”;
public static String outDir = “D:\upload\”;
public static void main(String[] args) {
doc2Img(inPath,outDir);
}
public static boolean getLicense() {
boolean result = false;
try {
// license.xml应放在…\WebRoot\WEB-INF\classes路径下
InputStream is = Word2PdfUtil.class.getClassLoader().getResourceAsStream(“license.xml”);
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
-
文档转图片
-
@param inPath 传入文档地址
-
@param outDir 输出的图片文件夹地址
*/
public static void doc2Img(String inPath, String outDir) {
try {
if (!getLicense()) {
throw new Exception(“com.aspose.words lic ERROR!”);
}
log.info(inPath + " -> " + outDir);
long old = System.currentTimeMillis();
// word文档
Document doc = new Document(inPath);
// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
转换
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
int pageCount = doc.getPageCount();
for (int i = 0; i < pageCount; i++) {
File file = new File(outDir + “/” + i + “.png”);
log.info(outDir + “/” + i + “.png”);
FileOutputStream os = new FileOutputStream(file);
options.setPageIndex(i);
doc.save(os, options);
os.close();
}
long now = System.currentTimeMillis();
log.info("convert OK! " + ((now - old) / 1000.0) + “秒”);
} catch (Exception e) {
e.printStackTrace();
}