Format change after adding watermark

Hi,

One of our client is interested in aspose word for java. But he met a problem of trail version.

After adding watermark, there is null in the header, lead to format changing.

The version they used is aspose word for java 14.12

I have attached the “word” doc for testing. And here is the code:

com.aspose.words.Document doc = newcom.aspose.words.Document(wordFilePath);
System.out.println(doc.getSections().getCount());
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.getCurrentSection();
PageSetup pageSetup = currentSection.getPageSetup();
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
// Set up the text of the watermark.
watermark.getTextPath().setText(waterText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(100);
watermark.setHeight(20);
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark
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())
{
    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);
    }
    // Insert a clone of the watermark into the header.
    header.appendChild(watermarkPara.deepClone(true));
}

Thanks & Regards

Sara Yu

Shenzhen Chinasoft Information Technology Co., Ltd.

Add: Rm.209, Shenzhen Academy of Aeroapace Technology Bldg., Kejinan 10th Rd.,South High-tech Zone,Nanshan Dist.,Shenzhen,China 518057

Skype: sara.yu1688
Tel: +86-755-26994896

Fax: +86-755-26996813
Mob:+86-18823209951

Email: yujq@chinasofttech.com

QQ: 444391387

Web: www.sz168.com.cn

Hi Sara,

Thanks for your inquiry. Following highlight code is missing in insertWatermarkIntoHeader method. Please add this line into your code.

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));
}

If you still face problem, please share following detail for investigation purposes.

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.