CleanupOptions

MailMerge.CleanupOptions property

获取或设置一组标志,指定在邮件合并期间应删除哪些项目。

public MailMergeCleanupOptions CleanupOptions { get; set; }

例子

演示如何删除邮件合并可能从合并输出文档中创建的空段落。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertField(" MERGEFIELD TableStart:MyTable");
builder.InsertField(" MERGEFIELD FirstName ");
builder.Write(" ");
builder.InsertField(" MERGEFIELD LastName ");
builder.InsertField(" MERGEFIELD TableEnd:MyTable");

DataTable dataTable = new DataTable("MyTable");
dataTable.Columns.Add("FirstName");
dataTable.Columns.Add("LastName");
dataTable.Rows.Add(new object[] { "John", "Doe" });
dataTable.Rows.Add(new object[] { "", "" });
dataTable.Rows.Add(new object[] { "Jane", "Doe" });

doc.MailMerge.CleanupOptions = mailMergeCleanupOptions;
doc.MailMerge.ExecuteWithRegions(dataTable);

if (doc.MailMerge.CleanupOptions == MailMergeCleanupOptions.RemoveEmptyParagraphs) 
    Assert.AreEqual(
        "John Doe\r" +
        "Jane Doe", doc.GetText().Trim());
else
    Assert.AreEqual(
        "John Doe\r" +
        " \r" +
        "Jane Doe", doc.GetText().Trim());

演示如何自动删除邮件合并期间未使用的 MERGEFIELD。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// 使用 MERGEFIELD 为邮件合并数据源表的三列创建一个文档,
// 然后创建一个只有两列的表,其名称与我们的 MERGEFIELD 匹配。
builder.InsertField(" MERGEFIELD FirstName ");
builder.Write(" ");
builder.InsertField(" MERGEFIELD LastName ");
builder.InsertParagraph();
builder.InsertField(" MERGEFIELD City ");

DataTable dataTable = new DataTable("MyTable");
dataTable.Columns.Add("FirstName");
dataTable.Columns.Add("LastName");
dataTable.Rows.Add(new object[] { "John", "Doe" });
dataTable.Rows.Add(new object[] { "Joe", "Bloggs" });

// 我们的第三个 MERGEFIELD 引用了“City”列,该列在我们的数据源中不存在。
// 邮件合并将使此类字段保持在合并前的状态不变。
// 将“CleanupOptions”属性设置为“RemoveUnusedFields”将删除所有 MERGEFIELD
// 在邮件合并期间未使用以清理合并文档。
doc.MailMerge.CleanupOptions = mailMergeCleanupOptions;
doc.MailMerge.Execute(dataTable);

if (mailMergeCleanupOptions == MailMergeCleanupOptions.RemoveUnusedFields || 
    mailMergeCleanupOptions == MailMergeCleanupOptions.RemoveStaticFields)
    Assert.AreEqual(0, doc.Range.Fields.Count);
else
    Assert.AreEqual(2, doc.Range.Fields.Count);

也可以看看