This technical tip allows developers to join multiple documents into a single large document in .NET Applications.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
This technical tip allows developers to join multiple documents into a single large document. When migrating to Aspose.Words you will find that this task is very easy to achieve. Aspose.Words provides a special Document.AppendDocument method for this purpose and is used to join two documents together. This method copies the sections from the source document to the destination document. This removes any need to insert any section breaks which is required in automation. Also note that you can control how the documents appear joined together i.e continuous or on a new page by using the PageSetup.SectionStart property of the appropriate Section object.
Code for Joining Multiple Documents Together
[C#]
// The document that the other documents will be appended to.
Document doc = new Document();
// We should call this method to clear this document of any existing content.
doc.RemoveAllChildren();
int recordCount = 5;
for (int i = 1; i <= recordCount; i++)
{
// Open the document to join.
Document srcDoc = new Document(@"C:\DetailsList.doc");
// Append the source document at the end of the destination document.
doc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
// In automation you were required to insert a new section break at this point, however in Aspose.Words we
// don't need to do anything here as the appended document is imported as separate sectons already.
// If this is the second document or above being appended then unlink all headers footers in this section
// from the headers and footers of the previous section.
if (i > 1)
doc.Sections[i].HeadersFooters.LinkToPrevious(false);
}
[VB.NET]
' The document that the other documents will be appended to.
Dim doc As New Document()
' We should call this method to clear this document of any existing content.
doc.RemoveAllChildren()
Dim recordCount As Integer = 5
For i As Integer = 1 To recordCount
' Open the document to join.
Dim srcDoc As New Document("C:\DetailsList.doc")
' Append the source document at the end of the destination document.
doc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles)
' In automation you were required to insert a new section break at this point, however in Aspose.Words we
' don't need to do anything here as the appended document is imported as separate sectons already.
' If this is the second document or above being appended then unlink all headers footers in this section
' from the headers and footers of the previous section.
If i > 1 Then
doc.Sections(i).HeadersFooters.LinkToPrevious(False)
End If
Next i
[Code for Microsoft Word Automation]
for i = 1 to recordCount then
myDoc.Selection.WholeStory()
myDoc.Selection.EndOf()
myDoc.Selection.InsertFile "C:\DetailsList.doc"
if i < recordCount then
myDoc.Selection.Range.InsertBreak 2
if i > 1 then
myDoc.ActiveDocument.Sections(i).Headers(1).LinkToPrevious = False
end if
end if
next
The above code runs in a loop and inserts a document at the end of the current document. The content from each joined document is separated by a section break and the headers and footers of this new section are unlinked so they do not continue on from the previous section’s headers and footers.
More about Aspose.Words for .NET
- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.aspx
- Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx
- More Technical Tips by Joining and Appending Documents: http://www.aspose.com/docs/display/wordsnet/Joining+and+Appending+Documents
Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
http://www.aspose.com/
sales@aspose.com
Phone: 888.277.6734
Fax: 866.810.9465
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)