Making a new document with number formatting

Hi,

I am trying to make a new document with contents between two bookmarks “start_doc” and “end_doc”.
I am not able to maintain the formatting (in this case the List number formatting).

Please find attached source, Test.doc and SubDoc.doc
The SubDoc.doc should have the same numbering as the Test.doc

How can I achieve that?

Source:

import com.aspose.words.Bookmark;
import com.aspose.words.BookmarkCollection;
import com.aspose.words.Document;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.Section;

public class SubDocument
{
    public static void main(String[] args)
    {
        try
        {
            Document doc = new Document("C:/Test.doc");

            splitDocument(doc);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private static void splitDocument(Document doc) throws Exception
    {
        BookmarkCollection bookmarkCollection = doc.getRange().getBookmarks();

        Bookmark startBookmark = bookmarkCollection.get("start_doc");
        Bookmark endBookmark = bookmarkCollection.get("end_doc");

        extractContent(doc, startBookmark, endBookmark);
    }

    private static void extractContent(Document doc, Bookmark startB, Bookmark endB) throws Exception
    {
        if (startB == null)
        {
            System.out.println("The doc is null");
        }

        Node startNode = startB.getBookmarkStart().getParentNode();

        while (!(startNode.getParentNode().getNodeType() == NodeType.BODY))
        {
            startNode = startNode.getParentNode();
        }

        Node endNode;

        if (endB == null)
        {
            endNode = doc.getLastSection().getBody().getLastChild();
        }
        else
        {
            endNode = endB.getBookmarkStart().getParentNode();
        }

        while (!(endNode.getParentNode().getNodeType() == NodeType.BODY))
        {
            endNode = endNode.getParentNode();
        }

        Document subDoc = new Document();

        Node currNode = startNode.getNextSibling();

        while (!currNode.equals(endNode))
        {
            Node dstNode = subDoc.importNode(currNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            subDoc.getFirstSection().getBody().appendChild(dstNode);

            if (currNode.getNextSibling() == null)
            {
                Section nextSection = (Section) currNode.getAncestor(NodeType.SECTION).getNextSibling();
                currNode = nextSection.getBody().getFirstChild();
            }
            else
            {
                currNode = currNode.getNextSibling();
            }
        }
        /*if (currNode.equals(endNode))
        {
        Node dstNode = subDoc.importNode(currNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        subDoc.getFirstSection().getBody().appendChild(dstNode);
        }*/
        subDoc.save("C:/SubDoc.doc");
    }
}

Thanks in advance.

Regards,
Tarul

Hi

Thanks for your inquiry. The problem occurs because you use Document.importNode method. In your case, you should using NodeImporter class:
https://reference.aspose.com/words/java/com.aspose.words/NodeImporter
Note: you should use the same instance of NodeImported to import all nodes from one document into another. You can find an example how to use NodeImporter here:
https://docs.aspose.com/words/java/insert-and-append-documents/
Best regards,

Hi Alexey,

Thanks for the quick reply. This is now working with the NodeImporter.
Thanks alot

Regards,
Tarul