Articles in this section
Category / Section

How to insert Merge field inside an IF field in a Word document using .NET Core Word Library?

4 mins read

Syncfusion® Essential® DocIO is a .NET Core Word Library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can insert a merge field inside an IF field in a Word document.

An IF field should follow the syntax below:

{IF “Expression1” Operator “Expression2” “True statement” “False statement”}

The merge field can be inserted into the following parts of the IF field:

  • Expression1
  • Expression2
  • True statement
  • False statement

After inserting the merge field inside an IF field, the Word document looks as follows:

Template Word document

Steps to insert a merge field inside an IF field in a Word document:

  1. Create a new C# Console application project. CreateConsoleApp.png
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from Nuget.org.
    InstallNuGetPakage.png
  3. Include the following namespace in the Program.cs file:
    C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
  1. Use the following code to add the merge field inside an IF field:
    C#
// Create a new Word document.
WordDocument document = new WordDocument();
// Add a new section to the document.
IWSection section = document.AddSection();
// Add a new paragraph to the section.
WParagraph paragraph = section.AddParagraph() as WParagraph;

// Create a new instance of the IF field.
WField field = paragraph.AppendField("If", FieldType.FieldIf) as WField;
// Specify the field code.
InsertIfFieldCode(paragraph, field);

// Execute Mail merge.
string[] fieldName = { "Gender", "Male", "Female" };
string[] fieldValue = { "M", "Mr.Andrew", "Miss.Nancy" };
document.MailMerge.Execute(fieldName, fieldValue);
// Update the fields.
document.UpdateDocumentFields();

// Save and close the document.
FileStream outputStream = new FileStream("Result.docx", FileMode.Create, FileAccess.Write);
document.Save(outputStream, FormatType.Docx);
document.Close();
  1. Use the following code to add values for the IF field in the proper syntax. In the InsertIFFieldCode method, it inserts the IF field with proper syntax and adds the merge field.
/// <summary>
/// Insert the field code with nested fields.
/// </summary>
private static void InsertIfFieldCode(WParagraph paragraph, WField field)
{
   // Insert the field code based on IF field syntax.
   // IF field syntax - {IF Expression1OperatorExpression2TrueTextFalseText} 

   // Get the index of the IF field.
   int fieldIndex = paragraph.Items.IndexOf(field) + 1;
   // Add the field code.
   field.FieldCode = "IF ";
   // To insert a merge field after the "IF" field code, increment the index.
   fieldIndex++;
   InsertText("\"", ref fieldIndex, paragraph);
   InsertMergeField("Gender", ref fieldIndex, paragraph);
   InsertText("\" = \"", ref fieldIndex, paragraph);
   InsertMergeField("Gender", ref fieldIndex, paragraph);
   InsertText("\" \"", ref fieldIndex, paragraph);
   InsertMergeField("Male", ref fieldIndex, paragraph);
   InsertText("\" \"", ref fieldIndex, paragraph);
   InsertMergeField("Female", ref fieldIndex, paragraph);
   InsertText("\"", ref fieldIndex, paragraph);
}
/// <summary>
/// Insert text such as quotes or operators.
/// </summary>
private static void InsertText(string text, ref int fieldIndex, WParagraph paragraph)
{
   // Insert the operator in a text range.
   WTextRange textRange = new WTextRange(paragraph.Document);
   textRange.Text = text;
   // Insert the text range as a field code item.
   paragraph.Items.Insert(fieldIndex, textRange);
   fieldIndex++;
}
/// <summary>
/// Insert a merge field at the given index.
/// </summary>
private static void InsertMergeField(string fieldName, ref int fieldIndex, WParagraph paragraph)
{
   WParagraph para = new WParagraph(paragraph.Document);
   para.AppendField(fieldName, FieldType.FieldMergeField);
   int count = para.ChildEntities.Count;
   // As the child entity is a field, if we insert the field, it automatically inserts the complete field structure.
   paragraph.ChildEntities.Insert(fieldIndex, para.ChildEntities[0]);
   fieldIndex += count;
}

You can download a complete working sample from GitHub.

See here to learn more about working with fields in Word documents using the .NET Word Library.

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion
I hope you enjoyed learning about how to insert a merge field inside an IF field in a Word document using the .NET Core Word Library.

You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied