FieldMergingArgsBaseRecordIndex Property
Gets the zero based index of the record that is being merged.

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
public int RecordIndex { get; }

Property Value

Type: Int32
Shows how to insert checkbox form fields into a document during mail merge.
public void MailMergeInsertCheckBox()
    // File 'MailMerge.InsertCheckBox.doc' is a template
    // containing the table with the following fields in it:
    // <<TableStart:StudentCourse>> <<CourseName>> <<TableEnd:StudentCourse>>
    Document doc = new Document(MyDir + "MailMerge.InsertCheckBox.doc");

    // Add a handler for the MergeField event
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertCheckBox();

    // Execute mail merge with regions
    DataTable dataTable = GetStudentCourseDataTable();

    // Save resulting document with a new name
    doc.Save(ArtifactsDir + "MailMerge.InsertCheckBox.doc");

private class HandleMergeFieldInsertCheckBox : IFieldMergingCallback
    /// <summary>
    /// This is called for each merge field in the document
    /// when Document.MailMerge.ExecuteWithRegions is called.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
        if (args.DocumentFieldName.Equals("CourseName"))
            // The name of the table that we are merging can be found here
            Assert.AreEqual("StudentCourse", args.TableName);

            // Insert the checkbox for this merge field, using DocumentBuilder
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.InsertCheckBox(args.DocumentFieldName + mCheckBoxCount, false, 0);

            // Get the actual value of the field
            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);


    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
        // Do nothing

    /// <summary>
    /// Counter for CheckBox name generation.
    /// </summary>
    private int mCheckBoxCount;

/// <summary>
/// Create DataTable and fill it with data.
/// In real life this DataTable should be filled from a database.
/// </summary>
private static DataTable GetStudentCourseDataTable()
    DataTable dataTable = new DataTable("StudentCourse");
    for (int i = 0; i < 10; i++)
        DataRow datarow = dataTable.NewRow();
        datarow[0] = "Course " + i;

    return dataTable;
See Also