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

Free Support Forum - aspose.com

Add Watermark in Word DOCX Document and then Convert Watermarked Word File to PDF using Java

I am using aspose-words-17.9-jdk16 library for java. I’m inserting watermark in my document according to the example provided at your website. when i save document in .docx format, it appears fine but when i covert my document to pdf, watermark disappears. Please tell me what could be the possible reason. I’ve searched alot and tried doc.updatePageLayout(); option before saving but none seems to work. I’ve facing another issue my watermark hides behind table.

Here’s my code:

DocumentWatermarkHelper.addWatermark(doc, params);
doc.save(outputStream,SaveFormat.fromName(OUTPUT_FORMAT.PDF.toString()));

public static void addWatermark(Document document,Map<String, Object> params) throws Exception {

            insertWatermarkFormat(document,"UAT Environment",params);
 }

private static void insertWatermarkFormat(Document doc, String watermarkText, Map<String, Object> params) throws Exception {

        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        String wmText = "";
        String previewText = getStringParamVal("preview",params);
        if(!"".equals(previewText)){
            wmText =  previewText.concat(System.lineSeparator()).concat(watermarkText);
        }
        else{
            wmText = watermarkText;
        }

        watermark.getTextPath().setText(wmText);
        watermark.getTextPath().setFontFamily("Arial");
        watermark.setWidth(500);
        watermark.setHeight(100);
        watermark.setRotation(-40);
        watermark.setBehindText(true);
//        watermark.getFill().setColor(Color.getHSBColor(198, 5,98));
//        watermark.setStrokeColor(Color.getHSBColor(198, 5,98));
        watermark.getFill().setColor(Color.lightGray);
        watermark.setStrokeColor(Color.lightGray);

        watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        watermark.setWrapType(WrapType.NONE);
        watermark.setVerticalAlignment(VerticalAlignment.CENTER);
        watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

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

I’m attaching picture of watermark issue. please have a look and suggest me solution.
watermark.png (4.0 KB)

@wajiha,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document(s) you are getting these problems with
  • Aspose.Words for Java (v20.3) generated output DOCX and PDF file(s)

As soon as you get these pieces of information ready, we will start investigation into your scenarios and provide you more information. Thanks for your cooperation.

@awais.hafeez I’ve tried your recommended version but still i’m having same issue. Please check the zip folder.
Watermark.zip (84.1 KB)

@wajiha,

The standard way of adding watermark to a Word document is mentioned in the following article:

The code from the above article works fine on our end when running against the document you provided. Please see the output DOCX and PDF files produced on our end by using the licensed latest version of Aspose.Words for Java i.e. 20.3: