Articles in this section
Category / Section

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

3 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 merge field inside an IF field in a Word document.

An IF field should be in a syntax as follows.

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

The Merge field can be inserted in the following parts of IF field,

  • Expression1
  • Expression2
  • True statement
  • False statement.

Steps to insert merge field inside an IF field in 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 helps to add the merge field inside an IF field.
    C#
//Create a new Word document.
WordDocument document = new WordDocument();
//Add new section to the document.
IWSection section = document.AddSection();
//Add new paragraph to section.
WParagraph paragraph = section.AddParagraph() as WParagraph;

//Create a new instance of IF field.
WField field = paragraph.AppendField("If", FieldType.FieldIf) as WField;
//Specifies 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 helps to add values for the IF field in the proper syntax. In InsertIFFieldCode method, it inserts the IF with proper syntax and adds the merge field.
/// <summary>
/// Insert the field code with nested field 
/// </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 Merge field after "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 quote, operator.
/// </summary>
private static void InsertText( string text,ref int fieldIndex, WParagraph paragraph)
{
   //Insert the Operator in a textrange.
   WTextRange textRange = new WTextRange(paragraph.Document);
   textRange.Text = text;
   //Insert the textrange as field code item.
   paragraph.Items.Insert(fieldIndex, textRange);
   fieldIndex++;
}
/// <summary>
/// Insert 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 automaticlly 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 document using .NET Word Library.

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

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

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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 sign in to leave a comment
Access denied
Access denied