public void AppendAllDocumentsInFolder()
{
string path = MyDir + @"\Artifacts\Document.AppendDocumentsFromFolder.doc";
// Delete the file that was created by the previous run as I don't want to append it again.
if (File.Exists(path))
File.Delete(path);
//ExStart
//ExFor:Document.AppendDocument(Document, ImportFormatMode)
//ExSummary:Shows how to use the AppendDocument method to combine all the documents in a folder to the end of a template document.
// Lets start with a simple template and append all the documents in a folder to this document.
Document baseDoc = new Document();
// Add some content to the template.
DocumentBuilder builder = new DocumentBuilder(baseDoc);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Template Document");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("Some content here");
// Gather the files which will be appended to our template document.
// In this case we add the optional parameter to include the search only for files with the ".doc" extension.
ArrayList files = new ArrayList(Directory.GetFiles(MyDir, "*.doc"));
// The list of files may come in any order, let's sort the files by name so the documents are enumerated alphabetically.
files.Sort();
// Iterate through every file in the directory and append each one to the end of the template document.
foreach (string fileName in files)
{
// We have some encrypted test documents in our directory, Aspose.Words can open encrypted documents
// but only with the correct password. Let's just skip them here for simplicity.
FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);
if (info.IsEncrypted)
continue;
Document subDoc = new Document(fileName);
baseDoc.AppendDocument(subDoc, ImportFormatMode.UseDestinationStyles);
}
// Save the combined document to disk.
baseDoc.Save(path);
//ExEnd
}