I am having an issue with exporting a bookmark from one document to another. If you look at the attached Word document, you will see that I am bookmarking all of the 1st line and only part of the 2nd line. But when I export the bookmark to the target document, all of the 2nd line gets copied over, even though part of the 2nd line is not part of the bookmark (namely: “THIS SENTENCE IS NOT A PART OF THE BOOKMARK.”).
If I remove the first line, then it works fine, but not as is. Any suggestions here?
Thanks in advance!
Here is my code:
// This method is used to export a bookmark to a document.
private void ExportBookmarkToDocument(Bookmark bookmark, ref Aspose.Words.Document targetDocument)
{
// Get node that containd BookmarkStart and BookmarkEnd
Node currentNode = bookmark.BookmarkStart.ParentNode;
Node endNode = bookmark.BookmarkEnd.ParentNode;
// if bookmarkStart and bookmarkEnd are children of different nodes, then import nodes between parent nodes of bookmark start and bookmark end
if (currentNode != endNode)
{
// import nodes between BookmarkStart and BookmarkEnd Parent nodes
while (currentNode != endNode)
{
Node newNode = targetDocument.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);
targetDocument.FirstSection.Body.AppendChild(newNode);
currentNode = currentNode.NextSibling;
}
targetDocument.FirstSection.Body.AppendChild(targetDocument.ImportNode(endNode, true, ImportFormatMode.KeepSourceFormatting));
}
// else import nodes between bookmark start and bookmark end
else
{
currentNode = bookmark.BookmarkStart.NextSibling;
// import nodes between BookmarkStart and BookmarkEnd
while (currentNode != bookmark.BookmarkEnd)
{
Node newNode = targetDocument.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);
targetDocument.FirstSection.Body.FirstParagraph.AppendChild(newNode);
currentNode = currentNode.NextSibling;
}
}
}