Watermark image added to word file should not be deleted or editable

Hi ,

I used to below code to add image as background to word document

package com.test;

import java.awt.Color;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Font;
import com.aspose.words.HorizontalAlignment;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.Shape;
import com.aspose.words.VerticalAlignment;
import com.aspose.words.WrapType;

public class AsposeAddImageToWordFile {
    static String MyDir="d:\\images\\";
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        Document doc = new Document("d:\images\test.docx");
        DocumentBuilder builder = new DocumentBuilder(doc);
        Shape shape = builder.insertImage("d:\images\Demo.png");
        shape.setWrapType(WrapType.TOP_BOTTOM);
        shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
        shape.setRelativeVerticalPosition(RelativeVerticalPosition.PARAGRAPH);
        shape.setVerticalAlignment(VerticalAlignment.TOP);
        Font font = builder.getFont();
        font.setSize(16);
        font.setColor(Color.BLUE);
        font.setName("Arial");
        builder.insertParagraph();
        doc.save("d:\images\test_wm.docx");
        System.out.println("end");
    }
}

even though the image is added to the page I am able to delete that image which should not be the case. Can you please help me in this regard. Its urgent for mee.

Regards
Balu

Hi Balu,

Thanks for your inquiry.

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

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
insertWatermarkImage(doc, MyDir + "in.jpg");
doc.protect(ProtectionType.READ_ONLY);
// builder.moveToParagraph(1, 0);
builder.moveTo(doc.getFirstSection().getBody().getFirstChild());
builder.startEditableRange();
builder.moveTo(doc.getLastSection().getBody().getLastChild());
builder.endEditableRange();
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));
}