Give Unique Names to Bookmarks in Header Footer of Word DOCX Document using C# .NET | Avoid Duplicate Bookmark Names

Hi,

I would like to ask for support in the following scenario.

  1. Create document
  2. Insert new section (2 sections in document in total)
  3. Move to 1st section, then move to header.
  4. Insert bookmark
  5. Move to 2nd section, then move to header.
  6. Insert bookmark (with same details as earlier)

Important note
The bookmarks are added in both section (check HeaderFooter.Range.Bookmarks)
Then try to save document as docx (I noticed it saving just as file and when using memory stream) - the bookmark is removed then.

        var document = new Document();
        DocumentBuilder documentBuilder = new DocumentBuilder(document);
        documentBuilder.Document.Sections.Add(new Section(documentBuilder.Document));
        documentBuilder.MoveToSection(0);
        documentBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
        documentBuilder.StartBookmark("ABC123");
        documentBuilder.Write("TEXT");
        documentBuilder.EndBookmark("ABC123");
        documentBuilder.MoveToSection(1);
        documentBuilder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
        documentBuilder.StartBookmark("ABC123");
        documentBuilder.Write("TEXT");
        documentBuilder.EndBookmark("ABC123");

        using (var stream = new MemoryStream())
        {
            documentBuilder.Document.Save(stream, SaveFormat.Docx);
        }

Could you check my scenario? Could you provide any workaround for that?

Thanks

@acturisaspose,

This seems to be an expected behavior. It is by MS Word design that a Word document must have unique names for bookmarks. You cannot insert two bookmarks with the same names. In MS Word 2019 UI, if you give a different location the same name as an existing bookmark, then the previously existing bookmark will be deleted i.e. the bookmark itself, not the contents in that bookmark. MS Word 2019 UI also does not allow insertion of Bookmarks with same names.