Update bookmark in word document using .Net Core

Posted by: mithali.gone on 27 March 2024, 3:43 pm EST

    • Post Options:
    • Link

    Posted 27 March 2024, 3:43 pm EST

    Im trying to update the bookmark in a word document using below c# code. But it is not updating it.

    var doc = new GcWordDocument();

    doc.Load(@“H:\aspose_templates\template.dotx”);

    doc.Body.Bookmarks.FirstOrDefault()?.Name.Replace(“name”,“NewName”);

    doc.Save(@“H:\aspose_templates\new\template.docx”);

  • Posted 28 March 2024, 6:48 am EST

    Hi Mithali,

    Thanks for reaching out to us with your query.

    Using Replace() method, Bookmark’s Name will not update. Replace method of String returns a new string in which all occurrence of specified characters are replaced with the another characters. It doesn’t update the String property for which Replace method is invoked.

    In order to replace the Bookmark’s Name property you can try below code:

    var nameBookmark =doc.Body.Bookmarks.Where(x => x.Name == "name").FirstOrDefault();
    if(nameBookmark != null )
        nameBookmark.Name = "NewName";

    Please refer the attached sample for the same: WordBookmarkDemo.zip

    Best regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels