How to Insert a Word Document into another Document inside Android Apps

This technical tip explains how Android developers can insert a document into another document inside Android Applications. There is often a need to insert one document into another. For example, insert a document at a bookmark, merge field or at a custom text marker. At the moment, there is no single method in Aspose.Words that can do this in one line of code. However, a document in Aspose.Words is represented by a tree of nodes. The object model is rich and the task of combining documents is just a matter of moving nodes between the document trees. This article shows how to implement a method for inserting one document into another and using it in a variety of scenarios.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//your code here...Insert a Document at Any Location
//To insert the content of one document to another at an arbitrary location the following simple InsertDocument method can be used. This technique 
will be referred to by other scenarios described below. This is a method that inserts contents of one document at a specified location in another 
document.
 
/**
 * Inserts content of the external document after the specified node.
 * Section breaks and section formatting of the inserted document are ignored.
 *
 * @param insertAfterNode Node in the destination document after which the content
 * should be inserted. This node should be a block level node (paragraph or table).
 * @param srcDoc The document to insert.
 */
public static void insertDocument(Node insertAfterNodeDocument srcDoc) throws Exception
{
    // Make sure that the node is either a paragraph or table.
    if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
      (insertAfterNode.getNodeType() != NodeType.TABLE))
        throw new IllegalArgumentException("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.getParentNode();
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDocinsertAfterNode.getDocument()ImportFormatMode.KEEP_SOURCE_FORMATTING);
    // Loop through all sections in the source document.
    for (Section srcSection : srcDoc.getSections())
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        for (Node srcNode : (Iterable<Node>) srcSection.getBody())
        {
            // Let's skip the node if it is a last empty paragraph in a section.
            if (srcNode.getNodeType() == (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(srcNodetrue);
            // Insert new node after the reference node.
            dstStory.insertAfter(newNodeinsertAfterNode);
            insertAfterNode = newNode;
X

Url: http://www.aspose.com/docs/display/wordsandroid/How+to+Insert+a+Document+into+another+Document

Language: PHP | User: Sheraz Khan | Created: Oct 15, 2014 | Tags: Insert Document into another Document Insert a Document at a Bookmark Insert a Document at Any Location combining documents in Android document manipulation in Android Word Android API