We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Section Breaks Not Ignored within an IF field

I have a document template that has an IF field construct in it. Within the IF statement, there is a section break. If the IF condition is not met, I would expect Aspose.Words to omit the section break from the final output but it is including it. If I merge this template through the Word Interop for .NET, I get the expected behavior.

I believe this issue may be similar to the following historic ticket but no resolution was posted on this: Mail Merge incorporating IF and section breaks not behaving as desired

Using Aspose.Words 17.10 for .NET 4.5.2.

AsposeDocs.zip (27.5 KB)


Thanks for your inquiry. Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. We will investigate the issue on our side and provide you more information.

PS: Please ZIP and attach the application.


Thank you for your response. I have attached a sample console application that illustrates the issue. Please note, the Aspose.Words library is included as a nuget package so it will need to be package restored before the sample will run.

AsposeWords165165.zip (49.2 KB)

I would not expect the section break to be output in this example.




Thanks for sharing the detail. In your case, we suggest you please use Field.Unlink method instead of FieldsHelper.ConvertFieldsToStaticText to get the desired output. This method replaces the field with its most recent result. Please check the following modified MergeFields method.

public static void MergeFields(this Document document, Dictionary<string, string> fields)
    DocumentBuilder builder = new DocumentBuilder(document);

    List<Field> setsToRemove = fields.Select(field => builder.InsertField(string.Format("SET {0} \"{1}\"", field.Key, field.Value == null ? "" : field.Value.Replace("\"", "'")))).ToList();


    ////remove fields codes from data - if first so members are not changed
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldIf);
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldNone);
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldRefNoKeyword);
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldRef);
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldTime);
    //FieldsHelper.ConvertFieldsToStaticText(document, FieldType.FieldDate);

    foreach (Field field in document.Range.Fields)
        if (field.Type == FieldType.FieldIf)

    setsToRemove.ForEach(f => f.Remove());