Rename bookmarks in a docx with DocIO

Is it possible to rename the bookmarks in a Worddoc using DocIO ? I don't see a rename function for bookmarks in there.

Would it work to create a new bookmark using the same range as the existing one and after that remove old bookmark ?


Background:
We want to import Word documents (.docx) into another Document Management System (DMS).  The DMS systems use bookmarks to fill in data, but DMS1 uses different names for the bookmarks than DMS2, for example 'Title' in DMS1 is being called 'docTitle' in DMS2.  So, I would need to rename the bookmark 'Title' to 'docTitle' to make the docx compatible with DMS2.  We have hundreds of WordDocs so I'm looking for a way to process all these worddocs and rename the bookmarks (or recreate the bookmarks at the same places in the document).


2 Replies

GV Gunter Van Damme May 2, 2022 08:47 AM UTC

I think I have found a working solution.  I have some issues with the original content not being copied well.  But at least the bookmarks are on the same location as the previous ones.

POC code below:

private static void RecreateBookMark(WordDocument document, string oldname, string newname)

        {

            var bookmarkNavigator = new BookmarksNavigator(document);

            try

            {

                bookmarkNavigator.MoveToBookmark(oldname);


                //Gets the bookmark content

                TextBodyPart part = bookmarkNavigator.GetBookmarkContent();

                var content = ((WParagraph)part.BodyItems[0]).Text;


                //Gets the paragraph

                WParagraph paragraph = bookmarkNavigator.CurrentBookmarkItem.OwnerParagraph;

                var oldbookmark = document.Bookmarks.FindByName(oldname);


                //Remove original bookmark

                bookmarkNavigator.DeleteBookmarkContent(false);

                document.Bookmarks.Remove( oldbookmark );


                //Create the new bookmark

                paragraph.AppendBookmarkStart(newname);

                paragraph.AppendText(content);

                paragraph.AppendBookmarkEnd(newname);

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);


            }

        }



SB Suriya Balamurugan Syncfusion Team May 2, 2022 11:54 AM UTC

Hi Gunter,

Yes. It is feasible to rename the bookmarks in a Word document using DocIO. But we aren’t having any direct API to achieve this.

To meet your requirement, we suggest you to iterate Word document elements and get the old bookmark start and bookmark end from the Word document. Then delete the old bookmark and insert the new bookmark at the same place in the Word document using DocIO.

We have prepared a sample application to rename the bookmarks by recreating the bookmarks at the same places in the Word document as per your requirement and it can be downloaded from the below attachment.

Note: Please find the template Word document in the “Data” folder of above sample.

We have done the following things in the sample:
  1. Loads the template Word document.
  2. Iterates through the Word document elements.
  3. Gets the old bookmark start and removed it.
  4. Creates a new bookmark start and inserted in the same place of old bookmark start.
  5. Similarly, gets the old bookmark end and deleted it.
  6. Creates a new bookmark end and inserted in the same place of old bookmark end.
  7. Saves the Word document.

Please refer our UG documentation to know more about iterating Word document elements,
https://help.syncfusion.com/file-formats/docio/word-document/iterating-word-document-elements

Regards,
Suriya Balamurugan.



Attachment: Iteratedocumentelements_d6dca696.zip

Loader.
Up arrow icon