In this template ifcondtioncheck.docx (12.4 KB)
I have 2 merge fields one is FirstName and other is in if condition hideContent. In the output file abc.docx (20.5 KB) toggle the field codes in my machine by pressing ALT+F9. This is not the expected output. we need the like this expected output.docx (25.0 KB)
. The code is
Document templateDocument = new(@"C:\temp\ifcondtioncheck.docx");
List<string> keys = new();
List<object> values = new();
keys.Add("FirstName");
keys.Add("hideContent");
values.Add("Gurpreet Singh");
values.Add("false");
templateDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyTableRows;
templateDocument.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
templateDocument.MailMerge.Execute(keys.ToArray(), values.ToArray());
templateDocument.Save(@"C:\temp\abc.docx");
internal class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
/// <summary>
/// This is called when merge field is actually merged with data in the document.
/// </summary>
void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
{
DocumentBuilder builder = new(args.Document);
builder.MoveToMergeField(args.DocumentFieldName);
string value = Convert.ToString(args.FieldValue) ?? string.Empty;
builder.InsertHtml(value, true);
}
void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing
}
}
I am using aspose word 22.7 version.