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.
//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 insertAfterNode, Document 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(srcDoc, insertAfterNode.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(srcNode, true); // Insert new node after the reference node. dstStory.insertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } //Insert a Document During Mail Merge //This example relies on the InsertDocument method shown at the beginning of the article to insert a document into a merge field during mail merge execution. This code demonstrates how to use the InsertDocument method to insert a document into a merge field during mail merge. public void insertDocumentAtMailMerge() throws Exception { // Open the main document. Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc"); // Add a handler to MergeField event mainDoc.getMailMerge().setFieldMergingCallback(new InsertDocumentAtMailMergeHandler()); // The main document has a merge field in it called "Document_1". // The corresponding data for this field contains fully qualified path to the document // that should be inserted to this field. mainDoc.getMailMerge().execute( new String[] { "Document_1" }, new String[] { getMyDir() + "InsertDocument2.doc" }); mainDoc.save(getMyDir() + "InsertDocumentAtMailMerge Out.doc"); } private class InsertDocumentAtMailMergeHandler implements IFieldMergingCallback { /** * This handler makes special processing for the "Document_1" field. * The field value contains the path to load the document. * We load the document and insert it into the current merge field. */ public void fieldMerging(FieldMergingArgs e) throws Exception { if ("Document_1".equals(e.getDocumentFieldName())) { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.getDocument()); builder.moveToMergeField(e.getDocumentFieldName()); // The name of the document to load and insert is stored in the field value. Document subDoc = new Document((String)e.getFieldValue()); // Insert the document. insertDocument(builder.getCurrentParagraph(), subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.getCurrentParagraph().hasChildNodes()) builder.getCurrentParagraph().remove(); // Indicate to the mail merge engine that we have inserted what we wanted. e.setText(null); } } public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { // Do nothing. } } //If a document to be inserted is stored as binary data in the database field (BLOB field), use the following example. A slight variation to the above example to load a document from a BLOB database field instead of a file. private class InsertDocumentAtMailMergeBlobHandler implements IFieldMergingCallback { /** * This handler makes special processing for the "Document_1" field. * The field value contains the path to load the document. * We load the document and insert it into the current merge field. */ public void fieldMerging(FieldMergingArgs e) throws Exception { if ("Document_1".equals(e.getDocumentFieldName())) { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.getDocument()); builder.moveToMergeField(e.getDocumentFieldName()); // Load the document from the blob field. ByteArrayInputStream inStream = new ByteArrayInputStream((byte[])e.getFieldValue()); Document subDoc = new Document(inStream); inStream.close(); // Insert the document. insertDocument(builder.getCurrentParagraph(), subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.getCurrentParagraph().hasChildNodes()) builder.getCurrentParagraph().remove(); // Indicate to the mail merge engine that we have inserted what we wanted. e.setText(null); } } public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { // Do nothing. } } Insert a Document During Replace //This example shows how to insert content of one document into another during a customized find and replace operation. public void insertDocumentAtReplace() throws Exception { Document mainDoc = new Document(getMyDir() + "InsertDocument1.doc"); mainDoc.getRange().replace(Pattern.compile("\\[MY_DOCUMENT\\]"), new InsertDocumentAtReplaceHandler(), false); mainDoc.save(getMyDir() + "InsertDocumentAtReplace Out.doc"); } private class InsertDocumentAtReplaceHandler implements IReplacingCallback { public int replacing(ReplacingArgs e) throws Exception { Document subDoc = new Document(getMyDir() + "InsertDocument2.doc"); // Insert a document after the paragraph, containing the match text. Paragraph para = (Paragraph)e.getMatchNode().getParentNode(); insertDocument(para, subDoc); // Remove the paragraph with the match text. para.remove(); return ReplaceAction.SKIP; } }

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