给word文档添加水印时,每页需要添加6个水印文字,发现某一页中存在水印中断的问题,排查后发现该页存在不同的字体格式,标黄的字体为“正文”,标红的字体为“纯文本”,请问如何处理。
使用版本:23.1
代码如下:
Document document = new Document("E:\\123.docx");
FileOutputStream os = new FileOutputStream("E:\\123.pdf");
String warterMark = "水印文字";
Paragraph watermarkPara = new Paragraph(document);
Random random = new Random();
int lineIndex = 0;
int randomX = random.nextInt(5) * -20 -80;
for(int y = -20; y < 700; y+= 110){
int x = lineIndex * 240 + randomX;
lineIndex++;
if(lineIndex > 3){
lineIndex = 0;
randomX = random.nextInt(5) * -20 -80;
}
// 旋转的水印文字
Shape waterTextShape = getWaterMarkTextShape(document, warterMark, x, y);
watermarkPara.appendChild(waterTextShape);
}
document.save(os, SaveFormat.PDF);
public static Shape getWaterMarkTextShape(Document document, String warterMark, int x, int y) throws Exception {
Shape waterShape = new Shape(document, ShapeType.TEXT_PLAIN_TEXT);
waterShape.getTextPath().setText(warterMark);
waterShape.getTextPath().setFontFamily("Arial");
waterShape.setWidth(100);
waterShape.setHeight(24);
waterShape.setRotation(-30);
waterShape.setStrokeColor(new Color(210,210,210));
waterShape.setLeft(x);
waterShape.setTop(y);
waterShape.setFillColor(new Color(210,210,210));
waterShape.setFilled(true);
return waterShape;
}
最终效果
字体格式1
字体格式2