Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Aug 26, 2014 1:43 pm

Hello,

We just converted our Office Interop project over to Spire.Doc. Everything is working fine, but we just noticed that the bookmarks in the headers/footers aren't being replaced. We have a generic system that has thousands of word templates/docs that it will open and then replace the bookmarks with data from our DB (meaning we know neither which bookmarks are in a template nor which fields they will need from the DB).

The way we do this now is

Code: Select all
   var navigator = new BookmarksNavigator(doc);
   List<Bookmark> bookmarks = doc.Bookmarks.Cast<Bookmark>().ToList();

   foreach (Bookmark bookmark in bookmarks)
   {
      string dbValue = getDBValue(bookmark.Name);
      navigator.MoveToBookmark(bookmark.Name);
      navigator.ReplaceBookmarkContent(DBvalue, true);
   }


This works great for all the bookmarks except for the ones in the headers/footers (which did work fine in Office Interop). I have no way of changing the Word templates, but I suspect my problem is just that doc.Bookmarks doesn't contain the header bookmarks and if I could just append to that list any bookmarks I find in the headers and footers then the rest of the code would fall magically into place.

However, in working down the object tree on the HeaderFooter.Header class, I don't see how I can get a list of bookmarks. Any help?

I can process headers and footers explicitly outside this foreach loop if need be, but it still doesn't help me as far as getting the bookmark names (which I will need as they match our DB field names).

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Tue Aug 26, 2014 10:54 pm

Sorry, you can disregard this post. The bookmarks collection from doc.Bookmarks.Cast<Bookmark>().ToList(); does indeed contain the bookmarks with in the header/footers. I was running into the same problem I posted about in a different topic where the bookmark name didn't match the placeholder text within bookmark so I didn't realize that the header bookmark wasn't called "Patient_Name" and instead was "Patient_Name_2" despite the text showing "Patient_Name". Once I fixed my other problem (referenced in the other forum post), this also fixed itself.

bobradu
 
Posts: 9
Joined: Wed Aug 20, 2014 7:06 pm

Return to Spire.Doc