Numbering Value with multiply documents

After adding one document to another I need properly refresh numbering value.
For example:
In Doc1 we have

  1. Ddddddddd
  2. Ffffffff
  3. Ttttttttt

On the end of the Doc1 I have bookmark “test”
In Doc2 we have

  1. Kkkkkkk
  2. Ooooooooo
  3. mmmmmmmmm

After adding Doc2 to Doc1 I am expecting to have numbering 1, 2, 3, 4, 5
But I got result: 1, 2, 3, 1, 1, 1
Could you please help me to solve the problem?

And this is the my test code:

Document doc1 = new Document(templatePath + "Doc1.doc");
Document doc2 = new Document(templatePath + "Doc2.doc");
InsertDocumentAtBookmark("test", doc1, doc2);
doc.Save(@"c:\out.doc");

And this code from Aspose:

public void InsertDocumentAtBookmark(string bookmarkName, Document dstDoc, Document srcDoc)
{
    // Create DocumentBuilder
    DocumentBuilder builder = new DocumentBuilder(dstDoc);
    // Move cursor to bookmark and insert paragraph break
    builder.MoveToBookmark(bookmarkName);
    builder.Writeln();
    // Content of srcdoc will be inserted after this node
    Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
    // Content of first paragraph of srcDoc will be apended to this parafraph
    Paragraph insertAfterParagraph = (Paragraph) insertAfterNode;
    // Content of last paragraph of srcDoc will be apended to this parafraph
    Paragraph insertBeforeParagraph = builder.CurrentParagraph;
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    // Loop through all sections in the source document.
    foreach(Section srcSection in srcDoc.Sections)
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach(Node srcNode in srcSection.Body)
        {
            // Do not insert node if it is a last empty paragarph in the section.
            Paragraph para = srcNode as Paragraph;
            if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
                break;
            // If current paragraph is first paragraph of srcDoc
            // then append its content to insertAfterParagraph
            if (para.Equals(srcDoc.FirstSection.Body.FirstParagraph))
            {
                foreach(Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    insertAfterParagraph.AppendChild(dstNode);
                }
            }
            // If current paragraph is last paragraph of srcDoc
            // then append its content to insertBeforeParagraph
            else if (para.Equals(srcDoc.LastSection.Body.LastParagraph))
            {
                Node previouseNode = null;
                foreach(Node node in para.ChildNodes)
                {
                    Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
                    if (previouseNode == null)
                        insertBeforeParagraph.InsertBefore(dstNode, insertBeforeParagraph.FirstChild);
                    else
                        insertBeforeParagraph.InsertAfter(dstNode, previouseNode);
                    previouseNode = dstNode;
                }
            }
            else
            {
                // This creates a clone of the node, suitable for insertion into the destination document.
                Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
                // Insert new node after the reference node.
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

Thanks,
Rudolf

Hi Rudolf,
Thanks for your request.
1. Why numbering is not preserved when insert content from one document into another
This probably occurs because you are using Document.ImportNode method instead of Nodeimporter class to copy nodes from one document into another. When you use Document.ImportNode method Aspose.Words create a separate context for each imported node. So if you would like to import several paragraph which are members of same list in the source document and you are using Document.ImportNode, as separate instance of list will be create for each paragraph in the destination document. That is why if you had numbering like 1, 2, 3… in the source document, you will get 1, 1, 1… in the destination document.
To prevent this, you have to use one instance of NodeImporter class to import all nodes from the source document to the destination one. In this case numbering will be retained after copying content.
2. How to continue numbering when concatenate or insert documents
When you copy content from one document to another and your source and destination documents contains lists, Aspose.Words copies list from the source document to the destination. That is why numbering of items will not continue.
If you need that numbering continues. You should apply numbering in source and destination documents using styles and use UseDestinationStyles option upon copying content from one document to another. In this case, if names of styles you applied to the paragraphs are the same, the style from the source document will not be copied into the destination document. The copied paragraphs will use styles from the destination document. And since you applied numbering using paragraph styles, the paragraphs from the destination document and the copied paragraphs will become members of the same list and numbering will continue.
Hope this helps.
Best regards,

Hi Alexey,
Do you have some sample for #2(How to continue numbering when concatenate or insert documents)?
Regards,
Rudolf

Hello
Thanks for your request. According to Alexey’s answer you can try using the following code:

Document mainDoc = new Document("C:\\Temp\\Doc1.doc");
Document subDoc = new Document("C:\\Temp\\Doc2.doc");
Bookmark bookmark = mainDoc.Range.Bookmarks["Test"];
InsertDocument(bookmark.BookmarkStart.ParentNode, subDoc);
// Save output document
mainDoc.Save("C:\\Temp\\out.doc");
public static void InsertDocument(Node insertAfterNode, Document srcDoc)
{
    // Make sure that the node is either a paragraph or table.
    if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
        (!insertAfterNode.NodeType.Equals(NodeType.Table)))
        throw new ArgumentException("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.UseDestinationStyles);
    // Loop through all sections in the source document.
    foreach(Section srcSection in srcDoc.Sections)
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach(Node srcNode in srcSection.Body)
        {
            // Let's skip the node if it is a last empty paragraph in a section.
            if (srcNode.NodeType.Equals(NodeType.Paragraph))
            {
                Paragraph para = (Paragraph) srcNode;
                if (para.IsEndOfSection && !para.HasChildNodes)
                    continue;
            }
            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.ImportNode(srcNode, true);
            // Insert new node after the reference node.
            dstStory.InsertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
}

Please note, you should apply numbering in source and destination documents using styles. Please see the attached documents.
Best regards,

Hi Andrey,
Thank you very much. I did test and it is works properly with your attached files.
Could you please tell me how you save style for numbering in Doc1 and then
apply this in Doc2?
Regards,
Rudolf

Hello
Thanks for your request. You should just create a style with the same name and apply this style.
Please see the attached screenshot.
Best regards,

Thanks a lot.
Regards,
Rudolf

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.
(12)