From 89667367ea00305e467ac203219072e7b4ea7f0f Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Wed, 12 Nov 2025 11:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B0=B4=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/PqReportServiceImpl.java | 3 +- .../gather/tools/report/util/Docx4jUtil.java | 78 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java index 3f2e278b..a1594536 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java @@ -828,7 +828,8 @@ public class PqReportServiceImpl extends ServiceImpl i String loginName = RequestUtil.getLoginNameByToken(); SysUser user = sysUserService.getUserByLoginName(loginName); if (!leader.equals(user.getName())) { - System.out.println("当前用户是审核人,需要添加水印为:非正式"); + log.info("当前用户不是审核人,添加非正式水印"); + Docx4jUtil.addWatermarkToDocument(baseModelDocument, "非正式"); } Docx4jUtil.cleanBlankPagesAndRedundantPageBreaks(baseModelDocument); baseModelDocument.save(new File(dirPath.concat(File.separator).concat(fileName))); diff --git a/tools/report-generator/src/main/java/com/njcn/gather/tools/report/util/Docx4jUtil.java b/tools/report-generator/src/main/java/com/njcn/gather/tools/report/util/Docx4jUtil.java index 5dedcbe1..23ccc7e9 100644 --- a/tools/report-generator/src/main/java/com/njcn/gather/tools/report/util/Docx4jUtil.java +++ b/tools/report-generator/src/main/java/com/njcn/gather/tools/report/util/Docx4jUtil.java @@ -2141,5 +2141,83 @@ public class Docx4jUtil { return specialCaseP; } + /** + * 为Word文档添加水印 + * 使用页眉方式,但设置页眉高度为0,确保不影响文档结构 + * + * @param wordPackage Word文档包 + * @param watermarkText 水印文字(如:"非正式") + * @throws Exception 添加水印失败时抛出异常 + */ + public static void addWatermarkToDocument(WordprocessingMLPackage wordPackage, String watermarkText) throws Exception { + try { + MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); + ObjectFactory factory = new ObjectFactory(); + + // 创建页眉部分 + org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart headerPart = + new org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart(); + + // 将页眉部分添加到文档并获取关系ID + org.docx4j.relationships.Relationship relationship = mainDocumentPart.addTargetPart(headerPart); + + // 创建页眉对象 + org.docx4j.wml.Hdr hdr = factory.createHdr(); + + // 创建段落 + P paragraph = factory.createP(); + + // 创建Run + R run = factory.createR(); + + // 使用VML textbox方式创建水印文本(绝对定位,显示在页面中部) + String vmlString = String.format( + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "%s" + + "" + + "", watermarkText); + + // 解析VML并添加到Run + Object vmlObject = XmlUtils.unmarshalString(vmlString); + run.getContent().add(vmlObject); + + paragraph.getContent().add(run); + hdr.getContent().add(paragraph); + headerPart.setJaxbElement(hdr); + + // 获取或创建节属性(SectPr) + SectPr sectPr = mainDocumentPart.getJaxbElement().getBody().getSectPr(); + if (sectPr == null) { + sectPr = factory.createSectPr(); + mainDocumentPart.getJaxbElement().getBody().setSectPr(sectPr); + } + + // 创建页眉引用并关联到节属性 + org.docx4j.wml.HeaderReference headerReference = factory.createHeaderReference(); + headerReference.setId(relationship.getId()); + headerReference.setType(org.docx4j.wml.HdrFtrRef.DEFAULT); + sectPr.getEGHdrFtrReferences().add(headerReference); + + log.info("成功添加水印:{}", watermarkText); + } catch (Exception e) { + log.error("添加水印失败:{}", e.getMessage(), e); + throw new Exception("添加水印失败:" + e.getMessage(), e); + } + } + }