Issue with Bookmark range

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;
        }
    }
}

Hi
Thanks for your inquiry. In your code you import BookmarkStart.ParentNode and BookmarkEnd.ParentNode. Parent node of Bookmark end is whole paragraph and not sentence. So you should import only part of BookmarkEnd parent paragraph. You can open your document using DocumentBuilder (Aspose.Words demo app) to see its structure.
Best regards.

Alexey,
Thanks for the reply, and I’ll take a stab at it, though one thing about your statement is a little confusing for me: “So you should import only part of BookmarkEnd parent paragraph”. My question here is: how do I extract the part that I need, specifically the content within the bookmark?
Also note that the code that I have included in this post is pretty much a verbatim copy of code you sent me early to export a bookmark. Is the code simply incomplete, or not geared towards the task I have in mind.
In general I have found Aspose very useful and easy to use, but Bookmarks has been the one exception to that rule. E.g., in the Microsoft Primary Interop Assemblies, you can copy the range of the whole bookmark in a single method call (Bookmark.Range.Copy()) and paste it in the next line using Document.Content.Paste. So it surprised me a little when I found that there was no easy way to just copy the whole bookmark range and paste it in another document (or is there???.. or any plans to implement that??).
Thanks again!
-Jafar

Hi
Thanks fro your inquiry. Actually this code was just example how you can achieve this.
Please try using the following 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 part of first paragraph if this is needed
        if (!(currentNode as CompositeNode).FirstChild.Equals(bookmark.BookmarkStart))
        {
            CompositeNode firstNode = (CompositeNode)targetDocument.ImportNode(currentNode, true, ImportFormatMode.KeepSourceFormatting);
            while (firstNode.HasChildNodes)
            {
                if (firstNode.FirstChild.NodeType != NodeType.BookmarkStart)
                    firstNode.FirstChild.Remove();
                else if ((firstNode.FirstChild as BookmarkStart).Name != bookmark.Name)
                    firstNode.FirstChild.Remove();
                else
                    break;
            }
            targetDocument.FirstSection.Body.AppendChild(firstNode);
            currentNode = currentNode.NextSibling;
        }
        // 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;
        }
        if ((endNode as CompositeNode).LastChild.Equals(bookmark.BookmarkEnd))
        {
            targetDocument.FirstSection.Body.AppendChild(targetDocument.ImportNode(endNode, true, ImportFormatMode.KeepSourceFormatting));
        }
        // import part of last paragraph if this is needed
        else
        {
            CompositeNode lastNode = (CompositeNode)targetDocument.ImportNode(endNode, true, ImportFormatMode.KeepSourceFormatting);
            while (lastNode.HasChildNodes)
            {
                if (lastNode.LastChild.NodeType != NodeType.BookmarkEnd)
                    lastNode.LastChild.Remove();
                else if ((lastNode.LastChild as BookmarkEnd).Name != bookmark.Name)
                    lastNode.LastChild.Remove();
                else
                    break;
            }
            targetDocument.FirstSection.Body.AppendChild(lastNode);
        }
    }
    // 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;
        }
    }
}

Hope this helps.
Best regards.

That did the trick, thanks!
As a recommendation to the design/marketing team, I think this sort of functionality should be wrapped in a method as a standard part of the API; not specifically the method listed in this post (ExportBookmarkToDocument), but maybe a a method to copy/fetch the entire bookmark range and another one to copy/append it another document. So for example, this how I might expect to re-write my code this if this recommendation were to be implemented:

private void ExportBookmarkToDocument(Bookmark bookmark, ref Aspose.Words.Document targetDocument)
{
    newNode = bookmark.Range;
    targetDocument.FirstSection.Body.FirstParagraph.AppendChild(newNode);
}

… or something to that effect. I think it’s great that Aspose has the flexibility in to do what you did in the code you showed me, but this could be taken a step further and encapsulated for more ease of use; I don’t feel someone should have to do all this just to copy a bookmark. Just a suggestion though.
Thank you!
-Jafar

Hello Jafar!
Thank you for your thoughtful feedback. We’ll consider improvements allowing operation on a bookmark as a whole. But this task is not so frequent so we cannot give it high priority.
I have created a new issue in our defect database:
#5299 – Allow operation on bookmark as a whole
Also there could be interesting for you to track this issue:
#502 – Allow to get and set bookmark text when start and end have different grand parents
Have a nice day!

Sounds good, thanks!
One final question: How track/view the issues you’ve listed above?
Thank you.
-Jafar

Hi
Thanks for your inquiry. We will notify you as soon as these issues are resolved.
Best regards.

The issues you have found earlier (filed as 502) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(17)

Thanks for the update!