It’s useful to take a look at the logic behind the Document.AppendDocument method. This will provide some useful background information which helps to:
· Gain a better understanding of how the method works so if a resulting document does not appear as expected you will have a better idea as to the reason why.
· Understand the general underlying process of how to copy nodes between documents. This is useful if you are planning to implement your own import method. The technique behind how a document is appended is the same and can be applied to specific node types as well. For example, importing content at the paragraph level instead of at the section level.
· Provide a sample implementation which can be used if you are using an older version of Aspose.Words before the Document.AppendDocument method was introduced. You can use this code as a manual implementation to append documents.
The method below provides a manual implementation of the Document.AppendDocument function which closely follows the same underlying process as used in the built-in method.
Example
Shows how to manually append the content from one document to the end of another document.
[Java]
/**
* A manual implementation of the Document.AppendDocument function which shows the general
* steps of how a document is appended to another.
*
* @param dstDoc The destination document where to append to.
* @param srcDoc The source document.
* @param mode The import mode to use when importing content from another document.
*/
public void appendDocument(Document dstDoc, Document srcDoc, int mode) throws Exception
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
for (Node srcNode : srcDoc)
{
Section srcSection = (Section)srcNode;
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.importNode(srcSection, true, mode);
// Now the new section node can be appended to the destination document.
dstDoc.appendChild(dstSection);
}
}
Each section is imported into the destination document and is appended to the end of the document. Since content is imported section by section this means settings such as page setup and headers and footers are preserved during the import.
It is also useful to note that because the joining point of two documents is at the section level the specific joining point occurs between the last section of the destination document and the first section of the source document. The section and page setup properties dictate how the two documents are joined together at the joining of the two sections. The most common of these settings is to define if the source document is to appear on the same page or a new page.
As suggested above this approach is not limited to just combining documents. It is a common approach that you should use when you need to copy nodes from one document into another. There are three simple steps to copy any node from one document to another:
1. Obtain the node in the source document that you want to copy.
2. Import the node into the destination document. Importing creates a new node that is a copy of the original node, but suitable for insertion into the destination document.
3. Insert the imported node into the destination document.