Copy-Paste

Hello
I need to copy a bookmark of a document and paste it into another.
I have a dll made in VB6.0 and I used Selection.Copy and Selection.Paste
Can I replicate this with Aspose.Words?
Thanks

Hi
Thanks for your inquiry. Yes, I think that you can achieve this using Aspose.Words. If you would like copy bookmark content then you can copy all nodes between BookmarkStart and BookmarkEnd nodes. Also you should use ImportNode method to copy nodes between documents. See the following link for more information.
https://reference.aspose.com/words/net/aspose.words/documentbase/importnode/
If you need copy section then you can try using the following code.

public void AppendContent(Document docMain, Document docDocument)
{
    foreach (Section srcSection in docDocument.Sections)
    {
        Section dstSection = (Section)docMain.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        docMain.LastSection.AppendContent(dstSection);
    }
}

I hope that this could help you.
Best regards.

Hi,

could you please provide a piece of code where I can see how to copy all nodes between BookmarkStart and BookmarkEnd?

How can I paste it into the same document?

Thanks in advance for your help.

Martin

Hello!
In general that’s not so easy since bookmark start and bookmark end can be placed on any level of the document model hierarchy, even in different sections. I wrote a code snippet for the case when they are on the same level and gave a hint how to handle more complex cases. Maybe that’s enough for you.
To insert a node to a particular place in the document you can use CompositeNode.InsertBefore and CompositeNode.InsertAfter methods. If you are inserting an existing node to the same document it is recommended to clone it.

/// 
/// Traverses nodes between bookmark start and bookmark end.
/// In case they are children of the same node everything is easy.
/// But for general case code should be more complex.
/// 
/// Bookmark start node
/// Bookmark end node
public static void TraverseBookmarkedNodes(Node start, Node end)
{
    if (start.ParentNode == end.ParentNode)
    {
        for (Node current = start.NextSibling; current != end; current = current.NextSibling)
        {
            // Do something with the node.
            // For instance you can insert it to some collection or to a document at appropriate place.
            // If you'd like to copy the whole bookmark then copy also start and end nodes.
        }
    }
    else
    {
        // In this case we should recursively fetch parents and traverse nodes until we find bookmark end.
    }
}

Regards,

Hi
Thanks for your request. Please see the following forum thread to learn how to extract content between bookmarks.
https://forum.aspose.com/t/101379
Also see the following link to learn how to insert one document into another.
https://docs.aspose.com/words/java/insert-and-append-documents/
Hope this helps.
Best regards.