Вы находитесь на странице: 1из 2

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.Appe ndDocument method for this purpose and is used to join two documents together. T his method copies the sections from the source document to the destination docum ent. This removes any need to insert any section breaks which is required in aut omation. 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 separa te sectons already. // If this is the second document or above being appended then unlink all header s 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, h owever in Aspose.Words we ' don't need to do anything here as the appended document is imported as separat e 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 end if end if next The above code runs in a loop and inserts a document at the ocument. The content from each joined document is separated nd the headers and footers of this new section are unlinked nue on from the previous section s headers and footers. More about Aspose.Words for .NET

= False

end of the current d by a section break a so they do not conti

- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.a spx - Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.netcomponents/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

Вам также может понравиться