Lock Watermark Shape in Word Document using Java so that it cannot be Deleted

Hi We are using Aspose words in java.
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
Could you please guide us how can we lock a shape object from being deleted by user?

@vgcnerella2019,

I think, you can define different Editable Ranges in your document to meet this requirement. The following sample code demonstrates how you can protect the whole document and then define a few Editable Range scopes. You can incorporate the same functionality in your project. Hope, this helps.

Document doc = new Document();
doc.removeAllChildren();

Section section = new Section(doc);
doc.appendChild(section);

Body body = new Body(doc);
section.appendChild(body);

Paragraph para = new Paragraph(doc);
body.appendChild(para);

Run run = new Run(doc);
run.setText("Hello ");
run.getFont().setColor(Color.RED);
para.appendChild(run);

Run run2 = new Run(doc);
run2.setText("World!");
para.appendChild(run2);

doc.protect(ProtectionType.READ_ONLY);

DocumentBuilder builder = new DocumentBuilder(doc);

EditableRangeStart start = builder.startEditableRange();
EditableRangeEnd end = builder.endEditableRange();

run2.getParentNode().insertBefore(start, run2);
run2.getParentNode().insertAfter(end, run2);

doc.save("E:\\temp\\awjava-20.3.docx");

I suppose, the watermark shape resides in header/footer story of your Word document. You can protect the whole document and define editable ranges for Body/Main Story content.

Thanks for suggestion.

But it gives below error for DOC extn.

java.lang.IllegalStateException: A value for the specified key is missing and the default value is not provided.
at com.aspose.words.internal.zzX.zzY(Unknown Source)
at com.aspose.words.internal.zzX.zzZ(Unknown Source)
at com.aspose.words.internal.zzMM.zzZ(Unknown Source)
at com.aspose.words.internal.zzMM.zzWV(Unknown Source)
at com.aspose.words.zz9D.zzNy(Unknown Source)
at com.aspose.words.zz15.zzZ(Unknown Source)
at com.aspose.words.zzYVI.zzY(Unknown Source)
at com.aspose.words.zz9B.visitEditableRangeStart(Unknown Source)
at com.aspose.words.EditableRangeStart.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Paragraph.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.zz9B.zzZ(Unknown Source)
at com.aspose.words.zzZGV.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)

Any settings or SaveOptions need to be set ?
Any help is much appreciated.

Also another question, how can we identify the Run node either with id or name to edit for future purpose?

A post was merged into an existing topic: When a document is protected with Read only. All the text in edit range is highlighted. How to disable that option

@vgcnerella2019,

It is great that you were able to resolve this issue on your end.

You can bookmark Run nodes and later access them using Bookmark’s name. e.g.

Document doc = new Document("E:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("name");
builder.write("this is a Run node");
builder.endBookmark("name");
doc.save("E:\\temp\\awjava-20.3.docx");