您好,我当前使用的Aspose.wrod的版本是23.6,由于在生成Word的过程中添加了水印,结果导致了表格的格式发生了错乱,原本是连续的两行被隔开了,效果如下图所示:
image.jpg (91.9 KB)
经测试,如果把添加水印的方法去掉就会恢复正常,请问该如何修改才能正常?
以下是我生成PDF的代码:
String sourcePath = "/Users/111.docx";
String targetPath = "/Users/222.pdf";
LoadOptions opt = new LoadOptions();
opt.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);
Document doc = new Document(sourcePath, opt);
// 添加水印
doc = WatermarkUtils.insertWatermarkText(doc, "合同测试水印");
doc.save(targetPath);
public class WatermarkUtils {
/**
* 水印每个字体的宽度
*/
private final static int WATERMARK_FONT_WIDTH = 44;
/**
* 水印每个字体的高度
*/
private final static int WATERMARK_FONT_HEIGHT = 24;
/**
* 水印倾斜角度
*/
private final static int WATERMARK_FONT_ROTATION = -40;
/**
* 水印字体颜色
*/
private static final Color watermark_color = new Color(211, 211, 211);
/**
* 为word文档插入文本水印
*
* @param doc 文档对象
* @param watermarkText 文本内容
* @throws Exception
*/
public static Document insertWatermarkText(Document doc, String watermarkText) throws Exception {
if (StringUtils.isBlank(watermarkText)) {
log.error("没有配置水印内容, 无法为文档加入水印");
throw new Exception("没有配置水印内容, 无法为文档加入水印");
}
Document result = (Document) doc.deepClone(false);
for (int i = 0; i < doc.getPageCount(); i++) {
Document page = doc.extractPages(i, 1);
for (Section section : page.getSections()) {
Paragraph randomParagraph = getRandomParagraph(page, watermarkText);
insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_EVEN);
}
result.appendDocument(page, ImportFormatMode.USE_DESTINATION_STYLES);
}
return result.deepClone();
}
/**
* 构建文字shape类
*
* @param doc 文档对象
* @param watermarkText 水印文字
* @param left 左边距
* @param top 上边距
* @throws Exception
*/
private static Shape buildTextShape(Document doc, String watermarkText, double left, double top) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("宋体");
watermark.setWidth(watermarkText.length() * (double) WATERMARK_FONT_WIDTH);
watermark.setHeight(WATERMARK_FONT_HEIGHT * 3);
watermark.setRotation(WATERMARK_FONT_ROTATION);
//绘制水印颜色
watermark.getFill().setColor(watermark_color);
watermark.setStrokeColor(watermark_color);
//将水印放置在页面中心
watermark.setLeft(left);
watermark.setTop(top);
watermark.setWrapType(WrapType.NONE);
return watermark;
}
/**
* 插入水印
*
* @param watermarkPara 水印段落
* @param sect 部件
* @param headerType 头标类型字段
* @throws Exception
*/
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null) {
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
header.appendChild(watermarkPara.deepClone(true));
}
private static Paragraph getRandomParagraph(Document doc, String watermarkText) throws Exception {
Paragraph watermarkPara = new Paragraph(doc);
Random random = new Random();
int beginLeft = -320 - random.nextInt(50);
for (int top = 50; top < 800; top += 250) {
for (int left = beginLeft; left < 700; left += 300) {
if (left == beginLeft) {
int splitNo = new Random().ints(0, watermarkText.length()).limit(1).findFirst().getAsInt();
String firstWater = watermarkText.substring(splitNo) + " ".substring(0, splitNo);
Shape waterShape = buildTextShape(doc, firstWater, left, top);
watermarkPara.appendChild(waterShape);
} else {
Shape waterShape = buildTextShape(doc, watermarkText, left, top);
watermarkPara.appendChild(waterShape);
}
}
}
return watermarkPara;
}
}
转换前的Word文档:
C24KA00477-BG1【范本】2024V1变更协议-通用确认函一式两份,甲乙双方,各执一份.docx (20.7 KB)
转换后的PDF文档:
3333.pdf (470.8 KB)