How to insert IF field with multiple body items in Word document using .NET Core Word Library?
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 IF Field with multiple body items like paragraph and table in Word document.
Understanding IF Field Structure
An IF field should be in a syntax as follows,
{IF “Expression1” Operator “Expression2” “True statement” “False statement”} |
---|
To view the field code in Microsoft Word, press Alt + F9. For example:
From the above screenshot,
- IF – IF
- “Expression1” – “M”
- Operator - =
- “Expression2”- “M”
- “True statement” – “Male”
- “False statement” – “Female”
Multiple body items like paragraphs and tables can be added as a part of IF field to a Word document using .NET Word library (DocIO) as like below.
Here both the true and false statements have one paragraph and one table as its content.
Steps to insert IF field with body items in Word document
- Create a new C# Console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from Nuget.org.
- Include the following namespace in the Program.cs file.
C#:
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
- Use the following code examples illustrates how to add the multiple body items as a part of IF field.
C#:
//Create a new Word document.
using (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);
//Update the fields.
document.UpdateDocumentFields();
//Save and close the document.
using (FileStream outputStream = new FileStream("Result.docx", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream, FormatType.Docx);
}
document.Close();
}
- Use the following code explains about the modification of field code for inserting multiple body items such as paragraph and tables.
C#:
/// <summary>
/// Modify the field code.
/// </summary>
private static void InsertIfFieldCode(WParagraph paragraph, WField field)
{
//Get the index of the If field i.e., Field code index.
int fieldIndex = paragraph.Items.IndexOf(field) + 1;
//Add the field code.
field.FieldCode = "IF \"M\" = \"M\" ";
//Move the field separator to field end to temporary paragraph.
WParagraph lastPara = new WParagraph(paragraph.Document);
MoveFieldMark(paragraph, fieldIndex + 1, lastPara);
//Set the true statement
paragraph = InsertTrueStatement(paragraph);
//Set the false statement
paragraph = InsertFalseStatement(paragraph);
//Move the content from temporary paragraph to last paragraph.
MoveFieldMark(lastPara,0, paragraph);
}
/// <summary>
/// Move the field items to another paragraph.
/// </summary>
private static void MoveFieldMark(WParagraph paragraph, int fieldIndex, WParagraph lastPara)
{
//Move the field separator to field end to the last paragraph.
for(int i = fieldIndex; i < paragraph.Items.Count;)
lastPara.Items.Add(paragraph.Items[i]);
}
/// <summary>
/// Insert the multiple body items as true statement.
/// </summary>
private static WParagraph InsertTrueStatement(WParagraph paragraph)
{
WTextBody ownerTextBody = paragraph.OwnerTextBody;
//Add text to the existing paragraph.
paragraph.AppendText("\"List of male candidates:");
//Add a table.
WTable table = ownerTextBody.AddTable() as WTable;
//Add first row.
WTableRow row = table.AddRow() as WTableRow;
row.AddCell().AddParagraph().AppendText("1.");
row.AddCell().AddParagraph().AppendText("Mr. Peter");
//Add second row.
row = table.AddRow() as WTableRow;
row.Cells[0].AddParagraph().AppendText("2.");
row.Cells[1].AddParagraph().AppendText("Mr. Andrew");
//Add third row.
row = table.AddRow() as WTableRow;
row.Cells[0].AddParagraph().AppendText("3.");
row.Cells[1].AddParagraph().AppendText("Mr. Thomas");
//Add a new paragraph for true statement end quote.
WParagraph lastPara = ownerTextBody.AddParagraph() as WParagraph;
lastPara.AppendText("\" ");
return lastPara;
}
/// <summary>
/// Insert multiple body items as false statement.
/// </summary>
private static WParagraph InsertFalseStatement(WParagraph paragraph)
{
WTextBody ownerTextBody = paragraph.OwnerTextBody;
//Add text to the existing paragraph.
paragraph.AppendText("\"List of female candidates:");
//Add a table.
WTable table = ownerTextBody.AddTable() as WTable;
//Add first row.
WTableRow row = table.AddRow() as WTableRow;
row.AddCell().AddParagraph().AppendText("1.");
row.AddCell().AddParagraph().AppendText("Mrs. Nancy");
//Add second row.
row = table.AddRow() as WTableRow;
row.Cells[0].AddParagraph().AppendText("2.");
row.Cells[1].AddParagraph().AppendText("Miss. Janet");
//Add third row.
row = table.AddRow() as WTableRow;
row.Cells[0].AddParagraph().AppendText("3.");
row.Cells[1].AddParagraph().AppendText("Mrs. Margaret");
//Add a new paragraph for true statement end quote.
WParagraph lastPara = ownerTextBody.AddParagraph() as WParagraph;
lastPara.AppendText("\"");
return lastPara;
}
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 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 IF field with multiple body items in 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!