Edit/Update word Bookmarks

Hi

I am doing some dev work using Aspose/C#. When I edit bookmarks by updating the text on a word document. The bookmarks that are edited disappear. May you please advise what causes my bookmarks to disappear. Some bookmarks are in a table, I loop through them and update the text. There are multiple documents merged into one file.

  1. I create a master document object to append all documents into it.
  2. I loop through each document and update the bookmark text.
  3. I append the content of the first-last index of document to the master document.
  4. The loop continues with the above process untill all is completed into the master document object.
  5. I save the master object that contains all the content of merged documents.

Tumelo.K
C# Developer

@tumik I have received your sample documents and code via private message. Your test documents have bookmarks with the same name. MS Word document cannot contain bookmarks with duplicated names. So after appending documents the duplicated bookmarks are renamed - _0 suffix is added. For example see the following code:

Document doc1 = new Document(@"C:\Temp\Test01.docx");
Document doc2 = new Document(@"C:\Temp\Test02.docx");

Console.WriteLine("=============First Document Bookmarks===============");
foreach (Bookmark bk in doc1.Range.Bookmarks)
    Console.WriteLine(bk.Name);

Console.WriteLine("=============Second Document Bookmarks===============");
foreach (Bookmark bk in doc2.Range.Bookmarks)
    Console.WriteLine(bk.Name);

// Merge documents
doc1.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);

Console.WriteLine("=============Result Document Bookmarks===============");
foreach (Bookmark bk in doc1.Range.Bookmarks)
    Console.WriteLine(bk.Name);

The output of this code is the following:

=============First Document Bookmarks===============
AccountNumber
IDNumber
Surname
FirstName
Nationality
RelIdNo
passcountry
permitno
PostalAddress
SuburbPost
CityPost
PostcodePost
PhysicalAddress
Suburb
City
Postcode
BusinessTelNo
CellPhoneNo
relationship
CardRequired
=============Second Document Bookmarks===============
AccountNumber
FirstName
Surname
IDNumber
CIS
ClientType
Occupation
BusinessTelNo
Employer
NonResStatus
PhysicalAddress
Suburb
Postcode
PostalAddress
SuburbPost
PostcodePost
=============Result Document Bookmarks===============
AccountNumber
IDNumber
Surname
FirstName
Nationality
RelIdNo
passcountry
permitno
PostalAddress
SuburbPost
CityPost
PostcodePost
PhysicalAddress
Suburb
City
Postcode
BusinessTelNo
CellPhoneNo
relationship
CardRequired
AccountNumber_0
FirstName_0
Surname_0
IDNumber_0
CIS
ClientType
Occupation
BusinessTelNo_0
Employer
NonResStatus
PhysicalAddress_0
Suburb_0
Postcode_0
PostalAddress_0
SuburbPost_0
PostcodePost_0

As you can see AccountNumber, FirstName, Surname, IDNumber, BusinessTelNo, PhysicalAddress, Suburb, Postcode, PostalAddress, SuburbPost, PostcodePost bookmarks from the second document have been renamed after appending documents.

@alexey.noskov Does this mean I must not have duplicate bookmark names? If so, do I have to rename them before merging them into a single document programmatically. The solution I am working on, allows a document to be duplicated when performing a merge. Will it be the correct approach, or I will face the same issue?

@tumik

Yes, this is MS Word format restriction. There cannot be bookmarks with duplicated names within the document.

Aspose.Words automatically renames the duplicated bookmarks when you join documents together. As I demonstrated in the code example in my previous post. So you can rely on Aspose.Words on it. But you should adjust your code to access the bookmarks not by their exact names in the final document, but by partial bookmark name. As I have mentioned Aspose.Words adds _X suffix to the renamed bookmark.

@alexey.noskov I have merged the documents. When I loop through the bookmarks.
I use the below code to update the text. The bookmark still disappears. I partially look for the bookmark name. When I debug. The condition becomes true, and it goes inside the if statement. It assigns the new text and saves the document. The results still hides the bookmarks.

Example code:

Document doc1 = new Document(@"C:\Temp\Test01.docx");
Document doc2 = new Document(@"C:\Temp\Test02.docx");

var baseDoc = new Document();
baseDoc.RemoveAllChildren();

/*append the documents - my code loops through the document list and 
appends all documents in a list to a base document. the below is just and example*/

baseDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
baseDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);

foreach (Bookmark bk in baseDoc.Range.Bookmarks)
    Console.WriteLine(bk.Name);

/*results displays all the bookmarks with the renamed bookmarks - _0. It works fine. 
The following code updates the data to be displayed on the bookmark document. 
Example is for the Account bookmark. It assigns the value correctly. 
When I debug it. It shows the new text of the AccountNumber, however after saving 
it and downloading the new doc. The bookmark is not visible.*/
foreach (Bookmark bk in doc.Range.Bookmarks) 
{
      if (bk.Name.Contains("AccountNumber"))
      {
            bk.Text = clientData.AccountNumber;
      }
}

@tumik
Thank you for additional information. You documents uses form fields. Usually in form fields only form field value is wrapped into a bookmark so setting bookmark value is identical to setting form field value. But in your case bookmark wraps whole form field code and after setting the bookmark value, the bookmark is preserved, but the form field code is invalid, so it is not displayed.

In your case you should use FormField and set their values:

foreach (FormField ff in baseDoc.Range.FormFields)
{
    if (ff.Name.Contains("AccountNumber"))
    {
        ff.SetTextInputValue("11111111");
    }
}

@alexey.noskov Thank you for your help. You are a star! Code works as expected. :slight_smile:

1 Like