Insert text in the bookmark in a word document - Dot Net Core c#

Posted by: mithali.gone on 28 March 2024, 12:23 pm EST

  • Posted 28 March 2024, 12:23 pm EST

    I need to insert text in the bookmark in a word document, im using below c# code, but t.Texts is coming as empty array always, so im getting index out of range error.

        var doc = new GcWordDocument();
            doc.Load(@"H:\aspose_templates\template.dotx");
            var bmks = doc.Body.Bookmarks;
            setBmkText("name", "Mr.");
    
            void setBmkText(string bmk, string value)
            {
                if (!bmks.Contains(bmk))
                    return;
                var t = bmks[bmk].GetRange();
                t.Texts[0].Value = value;
                for (int i = t.Texts.Count - 1; i > 0; --i)
                    t.Texts[i].Delete();
            }
    
            doc.Save(@"H:\aspose_templates\new\template.docx");
    
  • Posted 1 April 2024, 12:14 am EST

    Hello Mithali,

    We do not face an issue with your given code. Please find the attached sample implementing the same and let us know if you are doing anything different.

    Also, do share your Word document if you are facing issues with that so that we can assist you further accordingly.

    Regards,

    Prabhat Sharma.

    WordBookmarkDemo.zip

  • Posted 1 April 2024, 2:41 pm EST

    I’m using dotx template file, could you please try with dotx file ?

  • Posted 2 April 2024, 3:22 am EST

    Hello Mithali,

    We have updated the docx file of the last attached sample into .dotx but the issue did not occur.

    Please see if you still face the issue with the attached file.

    If not, we request that you share your .dotx file.

    Regards,

    Prabhat Sharma

    BookmarkDemoUpdatedFile.zip

  • Posted 2 April 2024, 11:09 am EST - Updated 2 April 2024, 2:32 pm EST

    Please find the .dotx file. In our template we have added bookmark in a empty place, but the template document which you shared, the bookmark is added for the text. For our scenario do you have any other approach to insert a value in the bookmark, if yes please share the sample code.

    And one more doubt, will the same code work for .dot template file?

    template.zip

  • Posted 12 April 2024, 2:43 am EST

    Hello Mithali,

    First of all apologies for the delay in response.

    Please find the .dotx file. In our template we have added bookmark in a empty place, but the template document which you shared, the bookmark is added for the text.

    We are discussing it with the developers and will get back to you with the updates soon.

    [Internal Tracking ID: DOC-6181]

    And one more doubt, will the same code work for .dot template file?

    Sorry, .dot file is not supported in the DsWord API. Please see this link for supported file formats:

    https://developer.mescius.com/document-solutions/dot-net-word-api/docs/online/keyfeatures.html

    Regards,

    Prabhat Sharma.

  • Posted 15 April 2024, 11:51 pm EST

    Hello Mithali,

    You can update the setBmkText method in the last attached sample as below to update the blank bookmark text:

    void setBmkText(string bmk, string value)
    {
    	if (!bmks.Contains(bmk))
            return;
    	var t = bmks[bmk].GetRange();
        t.Clear();
        t.Runs.Add(value);
    }

    It clears the existing text if any and replace the new text in its place.

    I hope it helps, if you need any other help, please feel free to ask.

    Regards,

    Prabhat Sharma.

Need extra support?

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

Learn More

Forum Channels