RecordIndex
Contents
[
Hide
]FieldMergingArgsBase.RecordIndex property
Gets the zero based index of the record that is being merged.
public int RecordIndex { get; }
Examples
Shows how to insert checkbox form fields into MERGEFIELDs as merge data during mail merge.
public void InsertCheckBox()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use MERGEFIELDs with "TableStart"/"TableEnd" tags to define a mail merge region
// which belongs to a data source named "StudentCourse" and has a MERGEFIELD which accepts data from a column named "CourseName".
builder.StartTable();
builder.InsertCell();
builder.InsertField(" MERGEFIELD TableStart:StudentCourse ");
builder.InsertCell();
builder.InsertField(" MERGEFIELD CourseName ");
builder.InsertCell();
builder.InsertField(" MERGEFIELD TableEnd:StudentCourse ");
builder.EndTable();
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertCheckBox();
DataTable dataTable = GetStudentCourseDataTable();
doc.MailMerge.ExecuteWithRegions(dataTable);
doc.Save(ArtifactsDir + "MailMergeEvent.InsertCheckBox.docx");
}
/// <summary>
/// Upon encountering a MERGEFIELD with a specific name, inserts a check box form field instead of merge data text.
/// </summary>
private class HandleMergeFieldInsertCheckBox : IFieldMergingCallback
{
/// <summary>
/// Called when a mail merge merges data into a MERGEFIELD.
/// </summary>
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
if (args.DocumentFieldName == "CourseName")
{
Assert.AreEqual("StudentCourse", args.TableName);
DocumentBuilder builder = new DocumentBuilder(args.Document);
builder.MoveToMergeField(args.FieldName);
builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0);
string fieldValue = args.FieldValue.ToString();
// In this case, for every record index 'n', the corresponding field value is "Course n".
Assert.AreEqual(char.GetNumericValue(fieldValue[7]), args.RecordIndex);
builder.Write(fieldValue);
mCheckBoxCount++;
}
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing.
}
private int mCheckBoxCount;
}
/// <summary>
/// Creates a mail merge data source.
/// </summary>
private static DataTable GetStudentCourseDataTable()
{
DataTable dataTable = new DataTable("StudentCourse");
dataTable.Columns.Add("CourseName");
for (int i = 0; i < 10; i++)
{
DataRow datarow = dataTable.NewRow();
dataTable.Rows.Add(datarow);
datarow[0] = "Course " + i;
}
return dataTable;
}
See Also
- class FieldMergingArgsBase
- namespace Aspose.Words.MailMerging
- assembly Aspose.Words