We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Multiple output Documents when using MailMerge.Execut(IMailMergeDataSource)

Is it possible to output multiple Documents using MailMerge.Execute(IdataSourceImplementation)? Consider a Document object ‘doc’ that is 1 page long with some merge fields in it.

//3 items in the datasource
exampleDataEntity ent1 = new exampleDataEntity(String "ent1");
exampleDataEntity ent2 = new exampleDataEntity(String "ent2");
exampleDataEntity ent3 = new exampleDataEntity(String "ent3");

//create the data source...
exampleDataEntityCollection myCollection = new exampleDataEntityCollection();
myCollection.add(ent1);
myCollection.add(ent2);
myCollection.add(ent3);

exampleDataSource myDataSource = new exampleDataSource(myCollection);//exampleDataSource implements  IMailMergeDataSource

doc.Mailmerge.Execute(myDataSource);
doc.save(somePath);//Produces 1 document, 3 pages long.
  //each page is a copy of the original 1 page document with data from one of the exampleDataEntity replacing the mergeFields.
  //Is there a way to get 3 documents, each 1 page long?

Is there a way to get 3 documents, each 1 page long?

@dreeder,

You can meet this requirement by implementing the following workflow:

DataTable dataTable = new DataTable("Payments");

dataTable.Columns.Add(new DataColumn("mf"));

DataRow dataRow;
for (int i = 1; i < 5; i++)
{
    dataRow = dataTable.NewRow();
    dataRow[0] = "field value_" + i;

    dataTable.Rows.Add(dataRow);
}

Document doc = new Document("D:\\temp\\in.docx");
doc.MailMerge.Execute(dataTable);

int x = 0;
foreach (Section sec in doc.Sections)
{
    Document temp = (Document)doc.Clone(false);
    temp.AppendChild(temp.ImportNode(sec, true));
    temp.Save("D:\\temp\\18.9_" + x + ".docx");
    x++;
}