添加水印
This commit is contained in:
@@ -828,7 +828,8 @@ public class PqReportServiceImpl extends ServiceImpl<PqReportMapper, PqReport> 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)));
|
||||
|
||||
@@ -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(
|
||||
"<w:pict xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
|
||||
"xmlns:v=\"urn:schemas-microsoft-com:vml\">" +
|
||||
"<v:shape style=\"position:absolute;left:0;text-align:center;margin-left:0;margin-top:0;" +
|
||||
"width:500pt;height:200pt;z-index:-251658240;" +
|
||||
"mso-position-horizontal:center;mso-position-horizontal-relative:page;" +
|
||||
"mso-position-vertical:center;mso-position-vertical-relative:page\" " +
|
||||
"fillcolor=\"none\" stroked=\"f\">" +
|
||||
"<v:textbox style=\"mso-fit-shape-to-text:t\">" +
|
||||
"<w:txbxContent>" +
|
||||
"<w:p><w:pPr><w:jc w:val=\"center\"/></w:pPr>" +
|
||||
"<w:r><w:rPr>" +
|
||||
"<w:color w:val=\"D8D8D8\"/>" +
|
||||
"<w:sz w:val=\"240\"/>" +
|
||||
"<w:szCs w:val=\"240\"/>" +
|
||||
"<w:rFonts w:ascii=\"微软雅黑\" w:eastAsia=\"微软雅黑\" w:hAnsi=\"微软雅黑\"/>" +
|
||||
"</w:rPr><w:t>%s</w:t></w:r></w:p>" +
|
||||
"</w:txbxContent></v:textbox>" +
|
||||
"</v:shape></w:pict>", 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user