Free Support Forum - aspose.com

How to use MailMerge.ExecuteWithRegions if I want to insert a �check box� in front of each record?

How to use MailMerge.ExecuteWithRegions if I want to insert a ‘check box’ in front of each record?

For example:

<> [Check Box] <> <>

CourseName will come from the dataset StudentCourse, but how can I insert a check box before the record is merged?

Thanks for your help.

That looks like a perfect job for MergeField event:

public void MergeWithCheckBoxInsertion()

{

Document doc = new Document(MyPath + "MailMerge.MergeField - InsertCheckBox.doc");

// Create DataTable and fill it with data.

// In real life this DataTable should be filled from database.

DataTable dataTable = new DataTable("StudentCourse");

string coursename = "CourseName";

dataTable.Columns.Add(coursename);

for(int i = 0; i<10; i++)

{

DataRow datarow = dataTable.NewRow();

dataTable.Rows.Add(datarow);

datarow[0] = coursename + i;

}

// Add a hadler for the MergeField event.

checkBoxCount = 0;

doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeWithCheckBox);

// Execute mail merge with regions.

doc.MailMerge.ExecuteWithRegions(dataTable);

// Save resulting document with a new name.

string outFileName = MyPath + "MailMerge.MergeField - InsertCheckBox Out.doc";

doc.Save(outFileName);

}

// Counter for CheckBox name generation

private int checkBoxCount;

///

/// This procedure is called for each merge field in the document

/// when Document.MailMerge.ExecuteWithRegions is called.

///

private void HandleMergeWithCheckBox(object sender, MergeFieldEventArgs e)

{

if(e.DocumentFieldName == "CourseName")

{

// Insert the checkbox for this merge field, using DocumentBuilder.

DocumentBuilder builder = new DocumentBuilder(e.Document);

builder.MoveToMergeField(e.FieldName);

builder.InsertCheckBox(e.DocumentFieldName + checkBoxCount, false, 0);

builder.Write((string)e.FieldValue);

checkBoxCount++;

}

}

Excellent and thanks for the quick response!

I will test this out and I think we will go ahead and place the order very soon if this work out.