Remove the field codes

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.

I also tried with

templateDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields;

@CTGurpreet1 MailMergeCleanupOptions.RemoveContainingFields will unlink the IF fields that contains merge fields. Please try using the following code:

Document templateDocument = new Document(@"C:\Temp\in.docx");
string[] keys = new string[] { "FirstName", "hideContent" };
string[] values = new string[] { "Gurpreet Singh", "false" };
templateDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields 
    | MailMergeCleanupOptions.RemoveEmptyParagraphs
    | MailMergeCleanupOptions.RemoveUnusedFields;
templateDocument.MailMerge.Execute(keys, values);
templateDocument.Save(@"C:\Temp\out.docx");

Thanks @alexey.noskov this solution works. but In our requirement in values array html data can also come. for example

string[] keys = new string[] { "FirstName", "hideContent" };
string[] values = new string[] { "< b >Gurpreet Singh< /b >", "false" };

it can be div tag or p tag etc.

so to handle that we have use the

templateDocument.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();

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
    }   
}

your suggestion

templateDocument.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields 
    | MailMergeCleanupOptions.RemoveEmptyParagraphs
    | MailMergeCleanupOptions.RemoveUnusedFields;

does not work in this case.

I have added one another cleanupOption

MailMergeCleanupOptions.RemoveStaticFields

and it works

@CTGurpreet1 It is perfect that you managed to resolve the issue.
Alternatively, you can unlink only certain types of field in your document, IF fields, for example:

// Unlink (replace with actual content) IF field in the document.
templateDocument.Range.Fields.Where(f => f.Type == FieldType.FieldIf).ToList()
    .ForEach(f => f.Unlink());