Free Support Forum - aspose.com

REPLACE Bookmark in Word Doc with a Special Tag

I have a word document. I want to replace ALL bookmarks with a special formatted

string.

So for example, say I have a word document with 3 bookmarks. Lets say the bookmark Ids are "b1", "b2" and "b3" - but remember I will not know the actual bookmark Ids in real life.

I want to find each of them and replace with a special tag like this

[bookmark id]

so in the above example, I should end up with THE SAME word document with

special tags

[b1], [b2], [b3] inside my word document and the real bookmarks are all gone!

Hi Nitin,


Thanks for your inquiry. Please use the following code snippet that finds the Bookmarks in your document and replaces them with special tags.

Document doc = new
Document(@“C:\Temp\in.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Bookmark bm in doc.Range.Bookmarks)

{

string bmName = bm.Name;

bm.Text = string.Empty;

builder.MoveToBookmark(bmName, true, false);

builder.Write("[" + bmName + "]");

}

doc.Range.Bookmarks.Clear();

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best Regards,