Numbering Formatting lost in document

Hi Alexey

First of all thank you very very much for helping.

Can you please change this code a little bit. I need to append content of src bookmark to the end of dst bookmark without deleting content of dst bookmark. (Best will be to add “\r\r” at the end of dst bookmark and after that append src bookmark content)

Hi Stanislav,
Thanks for your request. Here is modified code:

///
/// Copies content from one bookmark to another.
///
/// Bookmark to copy from.
/// Bookmark to copy to.
/// Flag indicates whether we should copy or move content from source bookamrk to destination
private static void CopyBookmarkContent(Bookmark src, Bookmark dst, bool clearSrc)
{
    // Nodes between which we need to copy content.
    Node start = src.BookmarkStart;
    Node end = src.BookmarkEnd;
    // DocumentBuilder will help us to insert content into the destination bookmark.
    DocumentBuilder builder = new DocumentBuilder((Document) src.BookmarkStart.Document);
    builder.MoveToBookmark(dst.Name, false, false);
    Node insertAfter = builder.CurrentParagraph;
    // If you need a paragraph break before content of the source bookmark,
    // then uncomment the next line.
    builder.Writeln();
    bool isStart = true;
    Node curNode = start.NextPreOrder(start.Document);
    while (curNode != null && !curNode.Equals(end))
    {
        // Move to next node
        Node nextNode = curNode.NextPreOrder(start.Document);
        // Check whether current contains end node
        if (curNode.IsComposite)
        {
            if (!((CompositeNode) curNode).GetChildNodes(NodeType.Any, true).Contains(end) &&
                !((CompositeNode) curNode).GetChildNodes(NodeType.Any, true).Contains(start))
            {
                nextNode = curNode.NextSibling;
                // If the current node is composite and the Documentbuidler cursor is in paragraph,
                // where bookmark start is located, we should insert a paragraph break.
                if (isStart)
                {
                    insertAfter = builder.CurrentParagraph;
                    builder.Writeln();
                    isStart = false;
                }
                // If we need to copy content we should clone node if we should move just insert the original node.
                Node nodeToInsert = clearSrc ? curNode : curNode.Clone(true);
                insertAfter.ParentNode.InsertAfter(nodeToInsert, insertAfter);
                // Insert after node should be also changed.
                insertAfter = nodeToInsert;
            }
        }
        else
        {
            builder.InsertNode(clearSrc ? curNode : curNode.Clone(true));
        }
        curNode = nextNode;
    }
}

Best regards,

Hi Alexey,

Thank you very much again. This code is good to my needs but it has infinite loop problem.

Word file attached.

src.Name = "START15634387122593980115F";
dst.Name = "START13634387122536064378F";
mDocument = new Document(@"C:\R4016006513330_129483969288080183.doc");
CopyBookmarkContent(mDocument.Range.Bookmarks["START15634387122593980115F"], mDocument.Range.Bookmarks["START13634387122536064378F"], false);

Can you please solve it?

Hi Stanislav,
Thanks for your request. The problem occurs because End of bookmark you copy to is inside bookmark you copy from. So you copy nodes into the same bookmark and that is why infinite loop occurs. See that attached screenshot. I think you should just check if the destination bookmark is nested into the source bookmark and just do nothing in this case. Or describe what is the expected behavior.
Best regards,

Hmmm…

I see that.
But the code that i found in
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/

bookmarkStart = dstBookmark.BookmarkStart;
bookmarkEnd = dstBookmark.BookmarkEnd;

// Firstly extract the content between these nodes including the bookmark.
extractedNodes = ExtractContent(bookmarkStart, bookmarkEnd, false);

Document dstBookmarkContent = GenerateDocument(mDocument, extractedNodes);

could manage this situation. But this code force me to use Save to temp file for some reason after GenerateDocument. (Otherwise i have problem that i described in post before)

Is there any way to improve this code to avoid the problem that i had?

Or maybe you can improve the code that you proposed (for example skip nodes that not belongs to the src bookmark directly)?

Hi Stanislav,
Thanks for your request. You can modify the code like shown below:

///
/// Copies content from one bookmark to another.
///
/// Bookmark to copy from.
/// Bookmark to copy to.
/// Flag indicates whether we should copy or move content from source bookamrk to destination
private static void CopyBookmarkContent(Bookmark src, Bookmark dst, bool clearSrc)
{
    // We will temporary copy nodes from source bookmark into this list.
    // Then we will put them into the destination bookmark.
    ArrayList nodesList = new ArrayList();
    // Nodes between which we need to copy content.
    Node start = src.BookmarkStart;
    Node end = src.BookmarkEnd;
    // DocumentBuilder will help us to insert content into the destination bookmark.
    DocumentBuilder builder = new DocumentBuilder((Document) src.BookmarkStart.Document);
    builder.MoveToBookmark(dst.Name, false, false);
    Node insertAfter = builder.CurrentParagraph;
    // If you need a paragraph break before content of the source bookmark,
    // then uncomment the next line.
    builder.Writeln();
    bool isStart = true;
    Node curNode = start.NextPreOrder(start.Document);
    while (curNode != null && !curNode.Equals(end))
    {
        // Move to next node
        Node nextNode = curNode.NextPreOrder(start.Document);
        // Check whether current contains end node
        if (curNode.IsComposite)
        {
            if (!((CompositeNode) curNode).GetChildNodes(NodeType.Any, true).Contains(end) &&
                !((CompositeNode) curNode).GetChildNodes(NodeType.Any, true).Contains(start))
            {
                nextNode = curNode.NextSibling;
                // If we need to copy content we should clone node if we should move just insert the original node.
                Node nodeToInsert = clearSrc ? curNode : curNode.Clone(true);
                nodesList.Add(nodeToInsert);
            }
        }
        else
        {
            nodesList.Add(clearSrc ? curNode : curNode.Clone(true));
        }
        curNode = nextNode;
    }
    foreach(Node node in nodesList)
    {
        if (node.IsComposite)
        {
            // If the current node is composite and the Documentbuidler cursor is in paragraph,
            // where bookmark start is located, we should insert a paragraph break.
            if (isStart)
            {
                insertAfter = builder.CurrentParagraph;
                builder.Writeln();
                isStart = false;
            }
            insertAfter.ParentNode.InsertAfter(node, insertAfter);
            // Insert after node should be also changed.
            insertAfter = node;
        }
        else
        {
            builder.InsertNode(node);
        }
    }
}

I.e. just copy content into array list and then copy to the destination bookmark. Also, please feel free to change the code yourself.
Best regards,

Unfortunately this code is not working. After using this function i dont see the content of src bookmark in destination bookmark.

Same bookmarks and document as in previous post.

src.Name = "START15634387122593980115F"
dst.Name = "START13634387122536064378F"

Hi
Thanks for your request. Actually, code copies content. But since your destination bookmark is inside source bookmark the code also copies Bookmark end of the destination bookmark. The duplicated Bookmark end is removed that the copied content becomes outside the destination bookmark.
You can try ignoring nodes from the destination bookmark:

foreach(Node node in nodesList)
{
    if (node.NodeType == NodeType.BookmarkEnd && ((BookmarkEnd) node).Name == dst.Name ||
        node.NodeType == NodeType.BookmarkStart && ((BookmarkStart) node).Name == dst.Name)
        continue;
    if (node.IsComposite)
    {
        // If the current node is composite and the Documentbuidler cursor is in paragraph,
        // where bookmark start is located, we should insert a paragraph break.
        if (isStart)
        {
            insertAfter = builder.CurrentParagraph;
            builder.Writeln();
            isStart = false;
        }
        insertAfter.ParentNode.InsertAfter(node, insertAfter);
        // Insert after node should be also changed.
        insertAfter = node;
    }
    else
    {
        builder.InsertNode(node);
    }
}

The technique actually works as expected. What you need is just tune the code to work how you need. I suppose it should not be difficult for you to achieve.
Best regards,

Unfortunately after adding this condition i still dont see src bookmark content in dst bookmark.

Please try it.

mDocument = new Document(@"C:\R4016006513330_129483969288080183.doc");

CopyBookmarkContent(mDocument.Range.Bookmarks[“START15634387122593980115F”], mDocument.Range.Bookmarks[“START13634387122536064378F”], false);

Hi Stanislav,
Thank you for additional information. But as I can see content is properly copied. Please see the attached screenshots.
Best regards,

Hi, Alexey

Thank you very much for help! I will perform more testing tomorrow.

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.

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