private void addWaterMark(String filePath) throws IOException {
InputStream in = new FileInputStream(new File(filePath));
XWPFDocument document = new XWPFDocument(in);
XWPFHeaderFooterPolicy xFooter = new XWPFHeaderFooterPolicy(document);
xFooter.createWatermark("机密");
XWPFHeader header = xFooter.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.getParagraphArray(0);
XmlObject[] xmlObjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
new QName("urn:schemas-microsoft-com:vml", "shape"));
if (xmlObjects.length > 0) {
com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape) xmlObjects[0];
ctshape.setFillcolor("#d8d8d8");
ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
}
String afterPath = filePath.substring(0, filePath.lastIndexOf(".")) + "_after.docx";
OutputStream out = new FileOutputStream(new File(afterPath));
document.write(out);
out.flush();
IoUtil.close(in);
IoUtil.close(out);
}
pom使用了hutool poi