导入依赖:
<dependency>
<groupId>aspose-words</groupId>
<artifactId>aspose-words</artifactId>
<version>21.6</version>
<classifier>jdk16</classifier>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.0</version>
<scope>compile</scope>
</dependency>
1.请求接口
@ApiOperation(value = "根据word模板导出图片流", notes = "根据word模板导出图片流")
@GetMapping(value = "/word2Image")
public void word2Image(HttpServletResponse response)throws Exception {
String templateUrl = "";//word模板地址
String fileName = "";//文件名称
//word模板需要绑定的字段
Map<String, Object> map = new HashMap<String, Object>();
Word2PDFUtil.word2Image(map, templateUrl, fileName, response);
}
2.word转换图片的方法
/**
* word先转pdf,pdf再转图片
* @param map
* @param templateUrl
* @param fileName
* @param response
* @throws Exception
*/
public static void word2Image(Map<String, Object> map, String templateUrl, String fileName, HttpServletResponse response) throws Exception {
OutputStream outputStream = null;
OutputStream fileOutputStream = null;
ByteArrayOutputStream b = new ByteArrayOutputStream();
InputStream inputStream = null;
try {
//1.通过模板,生成word
XWPFTemplate template = XWPFTemplate.compile(templateUrl).render(map);
template.write(b);
//2.转换word字节为输入流
inputStream = new ByteArrayInputStream(b.toByteArray());
//输出到指定位置
//OutputStream fileOutputStream = new FileOutputStream("D:\\test\\" + fileName + ".pdf");
//convert2PdfStream(inputStream,fileOutputStream);
//fileOutputStream.flush();
// if (fileOutputStream != null) {
// fileOutputStream.close();
// }
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".png");
response.setHeader("Access-Control-Expose-Headers", "Content-disposition");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.addHeader("name", URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Access-Control-Expose-Headers", "name");
response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
outputStream = response.getOutputStream();
//3.word转pdf
fileOutputStream = new ByteArrayOutputStream();
fileOutputStream = convert2PdfStream(inputStream, fileOutputStream);
//4.将OutputStream转换为InputStream
inputStream = new ByteArrayInputStream(((ByteArrayOutputStream) fileOutputStream).toByteArray());
//5.pdf 转 image
PDDocument document = PDDocument.load(inputStream);
PDFRenderer pdfRenderer = new PDFRenderer(document);
//6.获取第一页的图片
BufferedImage image = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
//7.输出图片到文件
ImageIO.write(image, "JPEG", outputStream);
} catch (Exception e) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "下载失败";
OutputStream ps = response.getOutputStream();
ps.write(data.getBytes("UTF-8"));
ps.close();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error("文件流关闭异常", e);
}
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (b != null) {
b.close();
}
}
}
3.word流转换为pdf流
/**
* word转pdf
* @param inputStream
* @param outputStream
* @return
* @throws Exception
*/
public static OutputStream convert2PdfStream(InputStream inputStream, OutputStream outputStream) throws Exception {
//去除水印
removeWaterMark();
try {
Document doc = new Document(inputStream);
//doc.save(outputStream, pdfSaveOptions);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
doc.save(outputStream, SaveFormat.PDF);
return outputStream;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
4.去除水印
/**
* 去除水印
* 使用反射替换变量
*
* @return
*/
private static void removeWaterMark() throws Exception {
Class<?> aClass = Class.forName("com.aspose.words.zzXyu");
java.lang.reflect.Field zzZXG = aClass.getDeclaredField("zzZXG");
zzZXG.setAccessible(true);
java.lang.reflect.Field modifiersField = zzZXG.getClass().getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(zzZXG, zzZXG.getModifiers() & ~Modifier.FINAL);
zzZXG.set(null, new byte[]{76, 73, 67, 69, 78, 83, 69, 68});
}