public static void Run()
{
// ExStart:ApplyCustomLogicToEmptyRegions
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_MailMergeAndReporting();
string fileName = "TestFile.doc";
// Open the document.
Document doc = new Document(dataDir + fileName);
// Create a data source which has some data missing.
// This will result in some regions that are merged and some that remain after executing mail merge.
DataSet data = GetDataSource();
// Make sure that we have not set the removal of any unused regions as we will handle them manually.
// We achieve this by removing the RemoveUnusedRegions flag from the cleanup options by using the AND and NOT bitwise operators.
doc.MailMerge.CleanupOptions = doc.MailMerge.CleanupOptions & ~MailMergeCleanupOptions.RemoveUnusedRegions;
// Execute mail merge. Some regions will be merged with data, others left unmerged.
doc.MailMerge.ExecuteWithRegions(data);
// The regions which contained data now would of been merged. Any regions which had no data and were
// Not merged will still remain in the document.
Document mergedDoc = doc.Clone(); // ExSkip
// Apply logic to each unused region left in the document using the logic set out in the handler.
// The handler class must implement the IFieldMergingCallback interface.
ExecuteCustomLogicOnEmptyRegions(doc, new EmptyRegionsHandler());
// Save the output document to disk.
doc.Save(dataDir + "TestFile.CustomLogicEmptyRegions1_out.doc");
// Reload the original merged document.
doc = mergedDoc.Clone();
// Apply different logic to unused regions this time.
ExecuteCustomLogicOnEmptyRegions(doc, new EmptyRegionsHandler_MergeTable());
doc.Save(dataDir + "TestFile.CustomLogicEmptyRegions2_out.doc");
// ExEnd:ApplyCustomLogicToEmptyRegions
// Reload the original merged document.
doc = mergedDoc.Clone();
// ExStart:ContactDetails
// Only handle the ContactDetails region in our handler.
ArrayList regions = new ArrayList();
regions.Add("ContactDetails");
ExecuteCustomLogicOnEmptyRegions(doc, new EmptyRegionsHandler(), regions);
// ExEnd:ContactDetails
dataDir = dataDir + "TestFile.CustomLogicEmptyRegions3_out.doc";
doc.Save(dataDir );
Console.WriteLine("\nMail merge performed successfully.\nFile saved at " + dataDir);
}
// ExStart:CreateDataSourceFromDocumentRegions