word转pdf 转图片
·
引入依赖
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose</artifactId>
<version>15.8.0.0</version>
</dependency>
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 org.apache.commons.compress.utils.Lists;
import org.apache.poi.util.IOUtils;
import java.io.*;
import java.util.List;
@Slf4j
public class WordUtil {
public static final String docxSuffix = "docx";
public static final String docSuffix = "doc";
/**
* word转图片--支持doc和dock格式
* @param wordPath word路径
* @param imgOutPath 图片输出路径,格式:/opt/img/
* @return 图片File数组
*/
public static List<File> wordToImg(String wordPath, String imgOutPath) {
List<File> imgOutFileList = Lists.newArrayList();
if (!getLicense()) {
log.error("com.aspose.words lic ERROR!");
return imgOutFileList;
}
try {
File imgOutPathDir = new File(imgOutPath);
if(!imgOutPathDir.exists()) {
imgOutPathDir.mkdirs();
}
File docFile = new File(wordPath);
String fileRealName = "";
if(docFile.getName().contains(docxSuffix)){
fileRealName= docFile.getName().replace(".docx","");
}else {
fileRealName = docFile.getName().replace(".doc","");
}
log.info(wordPath + " -> " + imgOutPath);
long old = System.currentTimeMillis();
// word文档
Document doc = new Document(wordPath);
// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
int pageCount = doc.getPageCount();
for (int i = 0; i < pageCount; i++) {
File f = new File(imgOutPath + fileRealName + "_" + (i + 1) + "."+ImageUtil.pngSuffix);
log.info(imgOutPath + fileRealName + "_" + (i + 1) + "."+ImageUtil.pngSuffix);
FileOutputStream os = null;
try {
os = new FileOutputStream(f);
options.setPageIndex(i);
doc.save(os, options);
} finally {
IOUtils.closeQuietly(os);
}
imgOutFileList.add(f);
}
long now = System.currentTimeMillis();
log.info("convert OK! " + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
log.error("word转图片失败");
}
return imgOutFileList;
}
/**
* word转pdf--支持doc和dock
* @param wordPath word路径
* @param pdfPath pdf路径,格式--/opt/pdf/样例.pdf
* @throws Exception word转pdf异常
*/
public static void wordToPdf(String wordPath, String pdfPath) throws Exception {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
throw new Exception("com.aspose.words lic ERROR!");
}
try {
long old = System.currentTimeMillis();
// 新建一个空白pdf文档
File file = new File(pdfPath);
// 如果没有文件夹就创建一下
if (!file.getParentFile().exists() && !file.isDirectory()) {
file.getParentFile().mkdirs();
}
FileOutputStream os = null;
// Address是将要被转化的word文档
Document doc = new Document(wordPath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
try {
os=new FileOutputStream(file);
doc.save(os, SaveFormat.PDF);
} finally {
IOUtils.closeQuietly(os);
}
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
// 转化用时
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
throw new Exception("word转pdf失败");
}
}
/**
* licence 验证
* @return 是否成功加载licence
* @throws Exception 加载licence可能发生的异常
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = com.aspose.words.Document.class
.getResourceAsStream("/com.aspose.words.lic_2999.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
is.close();
} catch (Exception e) {
log.error("License 获取失败");
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
try {
List<File> files = wordToImg("C:\\Users\\ldz\\Desktop\\测试word转换\\测试word-doc.doc", "C:\\Users\\ldz\\Desktop\\测试word转换\\wordToImage-doc\\");
} catch (Exception e) {
e.printStackTrace();
}
try {
wordToPdf("C:\\Users\\ldz\\Desktop\\测试\\案件结案报告--对应字段.docx","C:\\Users\\ldz\\Desktop\\测试\\案件结案报告--对应字段.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
更多推荐


所有评论(0)