BoldDesk®Customer service software offering ticketing, live chat, and omnichannel support, starting at $49/mo. for 10 agents. Try it for free.
Hello,
I have a template word document with multiple bookmarks in it, and more specific, I have bookmark "TemplateTable" which contains a table, and in this table, each column is a bookmark:
each row contains a bookmark, between []. I want to clone this bookmark and all its children bookmarks, this is my method:
private void CloneBookmark(BookmarksNavigator navigator, string originalBookmarkName, string newBookmarkName)
{
try
{
// Move to the original bookmark
navigator.MoveToBookmark(originalBookmarkName);
var originalBookmark = navigator.CurrentBookmark?.BookmarkStart;
if (originalBookmark == null)
{
_logger.LogWarning($"Bookmark '{originalBookmarkName}' does not exist.");
return;
}
// Get the paragraph containing the original bookmark
WParagraph originalParagraph = originalBookmark.OwnerParagraph;
if (originalParagraph == null)
{
_logger.LogWarning($"Bookmark '{originalBookmarkName}' has no valid paragraph.");
return;
}
// Clone the paragraph (including bookmark content)
WParagraph newParagraph = (WParagraph)originalParagraph.Clone();
originalParagraph.OwnerTextBody.ChildEntities.Add(newParagraph);
// Create a new bookmark start and end
var newBookmarkStart = new BookmarkStart(originalParagraph.Document, newBookmarkName);
var newBookmarkEnd = new BookmarkEnd(originalParagraph.Document, newBookmarkName);
// Insert new bookmark in the cloned paragraph
newParagraph.ChildEntities.Insert(0, newBookmarkStart);
newParagraph.ChildEntities.Add(newBookmarkEnd);
_logger.LogInformation($"Successfully duplicated bookmark '{originalBookmarkName}' as '{newBookmarkName}'.");
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed to duplicate bookmark: {originalBookmarkName} -> {newBookmarkName}");
}
}
If I ran this method, the cloned bookmark is unreachable, it looks like it isn't created. Is there another way to clone a bookmark using Doc Io?
Hi Ciprian,
From the given details, we noticed that you are trying to clone and add a
paragraph containing a bookmark. When you attempt to insert a bookmark with the
same name as an existing one in the document, it is not inserted into the
resulting document.
In Microsoft Word, adding two bookmarks with the same name is not allowed. The
DocIO library follows the same behavior.
Could you please share the complete details of your requirement along with the
input document and the expected output document? This will help us check and
provide you with the appropriate solution to achieve your requirement.
Note: If you have any confidential data in your input Word document,
please replace with some dummy data and provide us the same. We just need your
document to recreate the problem you face. So, once investigated the
issue, we will delete the document permanently from our side and do not
share with anyone.
Regards,
Sindhu Ramesh.
Hi,
Thank you for your reply.
I updated the code to be more specific related to my question, so:
- I have a paragraph with a parent bookmark, named "MainContent". This contains some children bookmarks, SubContent1,
SubContent2,
SubContent3. (see the file Input.docx)
- I have an array of objects, the properties values of each object will populate each child bookmark. For example:
dataArray = [ object1, object2, object3 ]
object1 =
{
prop1: "propertyValue1",
prop2: "propertyValue2"
}
object2 =
{
prop1: "propertyValue1",
prop2: "propertyValue2"
}
object3 =
{
prop1: "propertyValue1",
prop2: "propertyValue2"
}
[SubContent1] after the replace, will be [propertyValue1]
[SubContent2] after the replace, will be [propertyValue2]
and so on.
- I iterate over the array of objects, and for the object1, I will replace the bookmarks from the original paragraph, like in the above example;
- Then for the second iteration, for the object2, I need to clone the original paragraph (the MainContent with its children): SubContent1 will be replaced with propertyValue1, SubContent2 will be replaced with propertyValue2 etc
- For the third iteration, I need to clone the original paragraph again, and replace the children bookmarks with the values from the properties of object3.
So I need a dynamic logic, where the original paragraph will be replaced with the values from the first object of the loop, then for each object starting from the second iteration, I need to create a clone of the original paragraph, and replace its children bookmarks with the corresponding objects.
I attached my code. It is possible to achieve this?
Ciprian,
Based on the given details, we understand that your requirement is to
populate a bookmark with an array of objects. To achieve this, we have
prepared a sample.
In this sample, we have done the following things:
1. Load the input document.
2. Identify Main Bookmark and get its
content as WordDocumentPart
3. Clone the original document to
preserve formatting and clear the content of the cloned document.
4. Iterate Over Data Array to
Populate Bookmarks
For each object in the array:
Insert a temporary bookmark (tempBkmk).
Replace this bookmark with the extracted content (MainContent).
Identify predefined child bookmarks (SubContent1, SubContent2, SubContent3).
Replace each bookmark with the respective properties of the current object.
5. Insert Updated Content Back into the Main Document
Load the modified document into a WordDocumentPart.
Replace the original MainContent bookmark with the updated documentPart and save the final Document
Kindly refer to the attached sample along with the output
document. If your requirement is different, please provide the expected output
by preparing it in Microsoft Word.
For more information about working with bookmarks, kindly refer the below user
guide:
Working
with Bookmarks in .NET Word library | Syncfusion