docx添加水印

本文介绍了如何借助Hutool库在docx文档中添加水印,重点探讨了POM配置和Hutool的poi模块在实现这一功能中的应用。
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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值