How do you insert a field at the location of a bookmark in a document?

I have a Microsoft Word document template that has insertion point bookmarks to indicate the location of where the desired fields are to be positioned. 

In Word's object model I would get the range of a bookmark and add a field with the AddField method off of the range object. I use IncludeText fields that reference bookmarks in an external document.

I have created a small application that demonstrates this using two Microsoft Word files located under the project's Data folder.

These solution consists of a Word document used as a data source(Data Sheet.docx), with information elements bookmarked to identify the underlying data. The document template, FormLetter.dotx, is bookmarked with insertion point bookmarks to indicate where data fields are to be inserted. This apporach enables changes in the underlying data (stored in the Word document used as the data source) to be updated in one location and have those changes be reflected in all of the documents that have already been generated and possibly edited by simply updating the fields of the generated document. 

Data from the Data Source document is inserted into the generated documentation as INCLUDETEXT fields that reference the bookmarked data in the data source document.

I need to be able to insert a field at the precise location of an insertion point bookmark (one that in the Word object model has a Range property with a Start and End that are equal).

In looking at the documentation for DocIO, it appears that fields can only be appended to paragraphs. Bookmarks in DocIO do not have a Range concept quite the same as Word.

This reproduction app contains a Data Sheet.docx file that contains two pieces of information identified by the bookmarks FirstName and LastName. There is a document template file named FormLetter.dotx that contains text with two bookmarks that indicate where I would like to insert fields in the generated document, CompletedFormLetter.docx.

When you build and run the reproduction application and press the Go button, you will observe Microsoft Word opening the generated document and the fields are located at the end of the paragraph instead of at the desired bookmark locations.
     

Attachment: InsertFieldAtBookmarkLocation_34925c91.zip

1 Reply

SY Sethumanikkam Yogendran Syncfusion Team November 20, 2017 09:06 AM UTC

Hi Chad,

Thank you for contacting Syncfusion support.

How do you insert a field at the location of a bookmark in a document?
You can
insert the field in particular place using IndexOf property of child entities collection. For example, please refer the below code. 
int index = targetBookmark.BookmarkStart.OwnerParagraph.ChildEntities.IndexOf(targetBookmark.BookmarkStart);
// Creates new field and modifies it field code.
WField field = new WParagraph(MergeDocument).AppendField(targetBookmark.Name, FieldType.FieldIncludeText) as WField;
field.FieldCode = string.Format("INCLUDETEXT \"" + dataSourceFilenameEscaped + "\" \"" + dataSourceBookmarkName + "\"");
// Inserts particular item in current index of paragraph.
targetBookmark.BookmarkStart.OwnerParagraph.ChildEntities.Insert(index, field);
 

On further analysing with the given details, we have found that your requirement is to include and update the INCLUDETEXT field in the target document. Currently DocIO does not have support for updating INCLUDETEXT field in the Word document.

As a workaround, we have modified the given sample to meet your requirement by retrieve the contents of the specified bookmark from source document and merged into target document using Bookmark navigation and replace functionality of DocIO. Please find the sample with modified code from the below and let us know if this helps you.

Modified sample link:
http://www.syncfusion.com/downloads/support/forum/134625/ze/InsertFieldAtBookmarkLocation839485009.zip

Modified codes:
 
// Moves to Bookmark in the source Word document
sourceNavigator.MoveToBookmark(dataSourceBookmarkName);
// Gets the content for the bookmark from the source document.
TextBodyPart sourceBodyPart = sourceNavigator.GetBookmarkContent();

// Moves to Bookmark in the target Word document.
targetNavigator.MoveToBookmark(sourceToTargetBookmarkNameMap[dataSourceBookmarkName]);
// Replaces the target bookmark content by source bookmark content.
targetNavigator.ReplaceBookmarkContent(sourceBodyPart);
 

Please refer the below UG link for more details on the supported fields for field update , bookmark navigation and replace functionalities of DocIO

Field update:
https://help.syncfusion.com/file-formats/docio/working-with-fields#updating-fields

Bookmark navigation:
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#retrieving-contents-within-a-bookmark

Replace content:
https://help.syncfusion.com/file-formats/docio/working-with-bookmarks#replacing-content-in-a-bookmark
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace#replacing-the-search-results

Please let us know if you have any other questions.

Regards,
Sethumanikkam.Y


Loader.
Up arrow icon