Is it possible to merge a word symbol (unchecked checkmark box) into a word table (MailMerge.ExecuteWithRegions)?
If not, how could I navigate to a particular cell and insert the symbol (unchecked checkmark box)?
@mortenma You can use IFieldMergingCallback to insert custom content or perform additional action on merging field. In your case if it is required to insert checked/unchecked checkbox symbols at mergefields you can achieve this using code like this:
DataTable data = new DataTable("MyTable");
data.Columns.Add("Name");
data.Columns.Add("Checked");
data.Rows.Add("This is checked", "True");
data.Rows.Add("This is unchecked", "False");
data.Rows.Add("Once more is checked", "True");
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new InsertCheckboxFieldMergingCallback();
doc.MailMerge.ExecuteWithRegions(data);
doc.Save(@"C:\Temp\out.docx");
private class InsertCheckboxFieldMergingCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (args.FieldName == "Checked")
{
DocumentBuilder builder = new DocumentBuilder(args.Document);
builder.MoveToField(args.Field, true);
builder.Font.Name = "Wingdings";
builder.Write((args.FieldValue == "True") ? "\x00FE" : "\x00A8");
args.FieldValue = "";
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing
}
}
out.docx (10.1 KB)
in.docx (12.8 KB)
Alternatively, you can use IF field in your template and according to the condition value show checked or uncheked checkbox.
Thanks a lot for you help
Hi @alexey.noskov
I have a related but slightly different challenge.
In some fields I merge I have a base64 string (signature), and I would like to convert the base64 to an image and insert the image into the table cell during the merge.
Can I send extra info/values to the FieldMerging I can test?
(public void FieldMerging(FieldMergingArgs args))
@mortenma You can insert image from base64 string using ImageFieldMerging
in IFieldMergingCallback
implementation. For this you should put merge field with a special name Image:MyField
in your template. Foe example see the attached template and code: in.docx (12.3 KB)
string base64Image = @"iVBORw0KGgoAAAANSUhEUgAAAC0AAAAcCAYAAAD1PDaSAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAABcSURBVFhH7daxDYAwDAXRH9ZGokBikUxHzQKhyQJQXGTpXmO5u8KFW/o9Usw2ZylGU4ymGE0xmmI0xWhKyeg2nnx+TfczuY65LPArejVvmmI0xWiK0RSjKQWjkxctLwqf7aeEgAAAAABJRU5ErkJggg==";
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new InsertBase64ImageMergingCallback();
doc.MailMerge.Execute(new string[] { "MyField" }, new string[] { base64Image });
doc.Save(@"C:\Temp\out.docx");
private class InsertBase64ImageMergingCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
// Do nothing
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
if (args.FieldName == "MyField")
{
args.ImageStream = new MemoryStream(Convert.FromBase64String((string)args.FieldValue));
}
}
}