We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document format changes after adding a watermark

Hello,

Another problem in Word. Aspose version:aspose-words-14.9.0-jdk16.jar.
Document format changes after adding a watermark. We attached the document for your reference.
src is the source document, wm is watermarked document .Thank you.

Sara
Code:

private static void insertWatermarkLogo(Document doc, String path) throws Exception {
    DocumentBuilder builder = new DocumentBuilder(doc);
    for (Section sect : doc.getSections()) {
        PageSetup ps = sect.getPageSetup();
        Paragraph watermarkPara = new Paragraph(doc);
        Shape shape = builder.insertImage(path);
        shape.setWrapType(WrapType.NONE);
        shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        shape.setLeft((ps.getPageWidth() - shape.getWidth())/2);
        shape.setTop((ps.getPageHeight() - shape.getHeight())/2);
        watermarkPara.appendChild(shape);
        // 页眉页脚和上一节相同
        doc.getLastSection().getHeadersFooters().linkToPrevious(true);
        // 第一节
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        // 页眉页脚和上一节相同
        doc.getLastSection().getHeadersFooters().linkToPrevious(true);
        // 奇数页
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        // 页眉页脚和上一节相同
        doc.getLastSection().getHeadersFooters().linkToPrevious(true);
        // 偶数页
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
}

Hi Sara,

Thanks for your inquiry. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v14.10.0 and let us know how it goes on your side.

Please use the following code example example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "02.docx");
insertWatermarkImage(doc, MyDir + "in.png");
doc.save(MyDir + "Out.docx");
private static void insertWatermarkImage(Document doc, String image) throws Exception
{
    // Create a watermark shape. This will be a WordArt shape.
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(doc, ShapeType.IMAGE);
    watermark.getImageData().setImage(image);
    // Place the watermark in the page center.
    watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    watermark.setWrapType(WrapType.NONE);
    watermark.setVerticalAlignment(VerticalAlignment.CENTER);
    watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
    // Create a new paragraph and append the watermark to this paragraph.
    Paragraph watermarkPara = new Paragraph(doc);
    watermarkPara.appendChild(watermark);
    // Insert the watermark into all headers of each document section.
    for (Section sect : doc.getSections())
    {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
}
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception
{
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }
    // Insert a clone of the watermark into the header.
    header.appendChild(watermarkPara.deepClone(true));
}