FieldMergingArgsBaseRecordIndex Property |
Namespace: Aspose.Words.MailMerging
public void InsertCheckBox() { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.StartTable(); builder.InsertCell(); builder.InsertField(" MERGEFIELD TableStart:StudentCourse "); builder.InsertCell(); builder.InsertField(" MERGEFIELD CourseName "); builder.InsertCell(); builder.InsertField(" MERGEFIELD TableEnd:StudentCourse "); builder.EndTable(); // Add a handler for the MergeField event doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertCheckBox(); // Execute mail merge with regions DataTable dataTable = GetStudentCourseDataTable(); doc.MailMerge.ExecuteWithRegions(dataTable); // Save resulting document with a new name doc.Save(ArtifactsDir + "MailMergeEvent.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.MoveToMergeField(args.FieldName); 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); builder.Write(fieldValue); mCheckBoxCount++; } } 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"); 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; }