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

Replacing Bookmark Text removes protection restriction exception

I have a word document that is protected as read-only with exceptions that allow everyone to edit the text in the bookmark fields that will be filled in with the information the user provides in a GUI. However, after I replace the bookmark text with this data the restriction exceptions are removed, thus the replaced text is no longer editable. I replace the text using the C# function below. I need to maintain the replaced text in the bookmark fields as editable. I also have cross-reference fields that refer to these bookmarks and the protection exceptions remain on these cross-reference fields. I am using version 19.3 of Aspose.Words .NET API and Microsoft Word version Office 365 MSO (16.0.10730.2.264).

    private void PopulateBookmark(Document doc, string name, string value)
{
if (value != null)
{
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Bookmark bookmark = doc.Range.Bookmarks[name];
if (bookmark != null)
// replace the text of the bookmark if found
bookmark.Text = value;
}
}

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

• Your simplified input Word document
• Aspose.Words 19.3 generated output document showing the undesired behavior
• Your expected document showing the correct output. You can create expected document by using MS Word.
• Please also create a simplified standalone console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.
• Please also provide a comparison screenshot highlighting the problematic areas in Aspose.Words generated document with respect to your expected document and attach it here for our reference.

I have a web app that I am using for my evaluation. It will take some time which I don’t really have to create console app with just the bookmark issue. After getting the Aspose Document Explorer to work from the examples provided on Git, I have determined that the reason why replacing the bookmark text is removing the edit restriction exceptions. The Document Explorer allows me to walk the nodes of the docx file. While walking, I found that the nodes that represent the edit restriction exception, EditableRangeStart and EditableRangeEnd, are embedded in the range of BookMarkStart and BookMarkEnd nodes. So when the bookmark text is replaced the EditableRangeStart and EditableRangeEnd nodes are removed. I created a test where I placed the EditableRangeStart and End to be outside of the BookMark range and the Editable range was not removed when I replaced it. It is not easy to control where these nodes occur in the document from inside of MS Word, especially, when all the placeholder text contained in the bookmark needs to be replaced.

You can build on the following code to take EditableRangeStart and EditableRangeEnd out from inside Bookmark and then set text of Bookmark:

Document doc = new Document("E:\\temp\\in.docx");

EditableRangeStart start = null;
EditableRangeEnd end = null;

BookmarkStart bmStart = doc.Range.Bookmarks["bm"].BookmarkStart;

Node currentNode = bmStart;
bool isContinue = true;
while (currentNode != null && isContinue)
{
if (currentNode == bmStart.Bookmark.BookmarkEnd)
isContinue = false;

if (currentNode.NodeType.Equals(NodeType.EditableRangeStart))
{
start = (EditableRangeStart)currentNode;
}

if (currentNode.NodeType.Equals(NodeType.EditableRangeEnd))
{
end = (EditableRangeEnd)currentNode;
}

Node nextNode = currentNode.NextPreOrder(currentNode.Document);
currentNode = nextNode;
}

if (start != null)
{
start.ParentNode.InsertBefore(start, bmStart);
}

if (end != null)
{
end.ParentNode.InsertAfter(end, bmStart.Bookmark.BookmarkEnd);
}

doc.Save("E:\\Temp\\19.3.docx");


Hope, this helps.

Awais,

I was going to write code today to replace the Editable range around the bookmark after replacing the text. Thanks for providing this code this is very helpful.

Nina