InsertDocument on Blank Page

Hello Aspose,

I have trying to insert .docx fragments whilst building a report using DocumentBuilder.

Document sourceDocument1 = GetDocumentFromResource("test.docx");
if (sourceDocument1 != null)
    Paragraph p = DocumentBuilder.InsertParagraph();
    InsertDocument(p.PreviousSibling, sourceDocument1);

InsertDocument is the method outlined in the “Insert a Document at Any Location” from the Help file. I want the inserted document to start on a new page. The code above results in a paragraph break and then the source document contents.

How do insert the document with out the initial paragraph on the new page ? I tried using DocumentBuilder.CurrentParagraph as the node to pass into the InsertDocument method - but the source document ended up being inserted at the bottom of the target document.

Regards, Paul


Thanks for your inquiry.

First of all, please note that DocumentExplorer is a very useful tool which easily enables us to see the entire document structure. You can find DocumentExplorer in the folder where you installed Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe. You can observe the DOM structure of your source document with DocumentExplorer. You can also navigate through the DOM hierarchy and insert contents of another Word document at your desired place (Note: Target Node in hierarchy where you want to insert new document must either be a Paragraph or Table Node).

Secondly, you can try appending document to another document by using the following code snippet:

// The document that the content will be appended to.
Document dstDoc = new Document(MyDir + "Document.doc");

// The document to append.
Document srcDoc = new Document(MyDir "DocumentBuilder.doc");

// Append the source document to the destination document.
// Pass format mode to retain the original formatting of the source document when importing it.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

// Save the document.
dstDoc.Save(MyDir + "Document.AppendDocument Out.doc");

Please let us know if you need more information, we are always glad to help you.

Best Regards,

Hello Awais,

Thanks for the heads-up on the DocumentExplorer.

I had tried using the AppendDocument method but the source document was appearing at the end of the destination document, of course I’d forgotten to move to the end of the destination document after appending the document.

Regards, Paul

Here’s the correct code:

DocumentBuilder db = new DocumentBuilder();
db.Writeln("Page One");
Document sourceDocument1 = GetDocumentFromResource("test1.docx");
db.Document.AppendDocument(sourceDocument1, ImportFormatMode.KeepSourceFormatting);
db.Writeln("Last Page");


Thank you for the additional information. It is perfect that you managed to achieve what you were looking for. If we can help you with anything else, please feel free to ask.

Best Regards,