word转pdf,并加水印
Word转PDF
引入jar包
<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.2.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
代码
public static boolean word2pdf(String wordPath,String pdfPath){
boolean success = true;
try {
wordPath = StringUtil.handlePath(wordPath);
pdfPath = StringUtil.handlePath(pdfPath);
//读取word文档
XWPFDocument document = null;
try (InputStream in = Files.newInputStream(Paths.get(wordPath))) {
document = new XWPFDocument(in);
} catch (IOException e) {
log.error("",e);
success = false;
}
//将word转成pdf
PdfOptions options = PdfOptions.create();
try (OutputStream outPDF = Files.newOutputStream(Paths.get(pdfPath))) {
PdfConverter.getInstance().convert(document, outPDF, options);
} catch (IOException e) {
log.error("",e);
success = false;
}
} catch (Exception e) {
log.error("",e);
success = false;
}
return success;
}
PDF加水印
引入jar包
<!-- PDF水印-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- 中文字体 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
代码
public static void pdfWatermark(String pdfPath,String waterImgPath,String outPath){
PdfStamper stamper = null;
PdfReader reader = null;
try {
pdfPath = StringUtil.handlePath(pdfPath);
outPath = StringUtil.handlePath(outPath);
InputStream is = Files.newInputStream(Paths.get(pdfPath));
OutputStream outputStream = Files.newOutputStream(Paths.get(outPath));
reader = new PdfReader(is);
stamper = new PdfStamper(reader, outputStream);
//添加图片
PdfGState gState = new PdfGState();
//设置透明度
gState.setFillOpacity(0.5f);
//image
Image image = Image.getInstance(imageBytes(waterImgPath));
image.scalePercent(30);//图片缩放度
image.setAbsolutePosition(100, 350); //图片位置,这个位置测试是在中央
int total = reader.getNumberOfPages() + 1;
for (int i = 1; i < total; i++) {
PdfContentByte content = stamper.getOverContent(i);
// content.beginText();
content.setGState(gState);
content.addImage(image);
content.stroke();
// content.endText();
}
} catch (DocumentException| IOException e) {
log.error("",e);
}finally {
if (null != stamper) {
try {
stamper.close();
} catch (Exception e) {
log.info("关闭失败:", e);
}
}
if (null != reader) {
reader.close();
}
}
}
如果是要把水印添加在底部,可以使用 content.addImage(image,true);
工具类
/**
* 处理这种前面多了一个斜杠的路径 /E:/xyhj/project/esg/esg-ats-serve/portal_server/target/classes/echarts/barOption.js
* linux下是 /home/XXX/XXX路径,前面这/ 不能去掉
* @param path
* @return
*/
public static String handlePath(String path){
String osName = System.getProperties().getProperty("os.name");
if (osName.toLowerCase().startsWith("win")) {
if(path.startsWith("/")){
path = path.substring(1);
}
}
return path;
}
仅记录简单代码,供自己后续方便拷贝。