Word转Pdf,添加水印后多了一页空白页

你好,我使用的Aspose-word的版本是23.6,我用如下代码为word添加水印,并且保存为PDF文档,结果是PDF文档的最后一页变成了空白页,请问该如何解决?
代码如下:

    public void test1() throws Exception {
        System.out.println("开始转换pdf!");
        String sourcePath = "/Users/songyexiang/Desktop/test/原始.docx";
        String targetPath = "/Users/songyexiang/Desktop/test/最终.pdf";
        LoadOptions opt = new LoadOptions();
        opt.getLanguagePreferences().setDefaultEditingLanguage(EditingLanguage.CHINESE_PRC);
        Document doc = new Document(sourcePath, opt);
        // 添加水印
        doc = WatermarkUtils.insertWatermarkText(doc, "合同测试水印");
        doc.save(targetPath);
        System.out.println("转换pdf完成!");
    }
package com.yexiang.practice.util;

import com.aspose.words.*;
import com.aspose.words.Shape;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;

import java.util.Random;

/**
 * 水印工具类
 */
@Slf4j
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(235, 235, 235);

    /**
     * 为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("没有配置水印内容, 无法为文档加入水印");
        }
        for (Section section : doc.getSections()) {
            Paragraph randomParagraph = getRandomParagraph(doc, watermarkText);
            insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(randomParagraph, section, HeaderFooterType.HEADER_EVEN);
        }
        return doc.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文件:
原始.docx (48.6 KB)
pdf文件:
最终.pdf (325.9 KB)

@songyassen 为了避免创建一个新的空页眉段落,从而破坏文档,我认为应该这样做:

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(235, 235, 235);

    /**
     * 为word文档插入文本水印
     *
     * @param doc           文档对象
     * @param watermarkText 文本内容
     * @throws Exception
     */
    public static Document insertWatermarkText(Document doc, String watermarkText) throws Exception {
        if (StringUtils.isBlank(watermarkText)) {
            throw new Exception("没有配置水印内容, 无法为文档加入水印");
        }
        for (Section section : doc.getSections()) {
            insertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HEADER_EVEN);
        }

        return doc.deepClone();
    }

    /**
     * 构建文字shape类
     *
     * @param doc           文档对象
     * @param watermarkText 水印文字
     * @param left          左边距
     * @param top           上边距
     * @throws Exception
     */
    private static com.aspose.words.Shape buildTextShape(Document doc, String watermarkText, double left, double top) throws Exception {
        com.aspose.words.Shape watermark = new com.aspose.words.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;
    }

    private static void insertWatermarkIntoHeader(Document doc, String watermarkText, Section sect, int headerType) throws Exception {
        HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null) {
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }

        Random random = new Random();
        com.aspose.words.Shape waterShape;
        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);
                    waterShape = buildTextShape(doc, firstWater, left, top);
                } else {
                    waterShape = buildTextShape(doc, watermarkText, left, top);
                }

                Paragraph headerPara = (Paragraph)header.getChild(NodeType.PARAGRAPH, 0, true);
                if (headerPara == null) {
                    Paragraph watermarkPara = new Paragraph(doc);
                    watermarkPara.appendChild(waterShape);
                    header.appendChild(watermarkPara.deepClone(true));
                } else {
                    headerPara.appendChild(waterShape);
                }
            }
        }
    }
}