Syncfusion Feedback

Form filling in Word Documents Using tje Syncfusion .NET Word Library

The Syncfusion® .NET Word Library enables the creation of fillable forms with both legacy form fields and content controls in C# without relying on Microsoft Word or interop dependencies. It lets users design forms consisting of multiple elements such as plain text, rich text, pictures, checkboxes, combo boxes, and dropdown lists.

Watch this video to learn how to fill form field in a Word document using the Syncfusion .NET Word Library.

Watch the video

Fill form fields in Word document using C#

Learn how to fill form fields in Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates filling various form field types including text fields and dropdown lists.

Step 1: Create a new project

Start by creating a new C# Console Application project.

Step 2: Install the NuGet package

Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.

Step 3: Add required namespaces to fill form fields

Add the following namespaces to your Program.cs file:

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

Step 4: Open the Word document and access table

Open an existing Word document and access the table containing form fields.

FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 //Create a new Word document
 WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic);
 IWSection sec = document.LastSection;
 InlineContentControl inlineCC;
 InlineContentControl dropDownCC;
 WTable table1 = sec.Tables[1] as WTable;
 WTableRow row1 = table1.Rows[1];

Step 5: Fill general information

Fill the general information fields such as name, date of birth, address, phone number, and email.

#region General Information
//Fill the name
WParagraph cellPara1 = row1.Cells[0].ChildEntities[1] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
WTextRange text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "Steve Jobs";
inlineCC.ParagraphItems.Add(text);
//Fill the date of birth
cellPara1 = row1.Cells[0].ChildEntities[3] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "06/01/1994";
inlineCC.ParagraphItems.Add(text);
//Fill the address
cellPara1 = row1.Cells[0].ChildEntities[5] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "2501 Aerial Center Parkway.";
inlineCC.ParagraphItems.Add(text);
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "Morrisville, NC 27560.";
inlineCC.ParagraphItems.Add(text);
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "USA.";
inlineCC.ParagraphItems.Add(text);
//Fill the phone no
cellPara1 = row1.Cells[0].ChildEntities[7] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "+1 919.481.1974";
inlineCC.ParagraphItems.Add(text);
//Fill the email id
cellPara1 = row1.Cells[0].ChildEntities[9] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat);
text.Text = "[email protected]";
inlineCC.ParagraphItems.Add(text);
#endregion

Step 6: Fill educational information

Fill the educational details including education type, university name, and programming experience levels.

#region Educational Information
table1 = sec.Tables[2] as WTable;
row1 = table1.Rows[1];
//Fill the education type
cellPara1 = row1.Cells[0].ChildEntities[1] as WParagraph;
dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat);
text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[1].DisplayText;
dropDownCC.ParagraphItems.Add(text);
//Fill the university
cellPara1 = row1.Cells[0].ChildEntities[3] as WParagraph;
inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat);
text.Text = "Michigan University";
inlineCC.ParagraphItems.Add(text);
//Fill the C# experience level
cellPara1 = row1.Cells[0].ChildEntities[7] as WParagraph;
dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat);
text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[2].DisplayText;
dropDownCC.ParagraphItems.Add(text);
//Fill the VB experience level
cellPara1 = row1.Cells[0].ChildEntities[9] as WParagraph;
dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl;
text = new WTextRange(document);
text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat);
text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[1].DisplayText;
dropDownCC.ParagraphItems.Add(text);
#endregion

Step 7: Save the Word document

Save the filled Word document to a file stream.

Run

//Create file stream
 FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite);
 //Save the Word document to file stream
 document.Save(outputFileStream, FormatType.Docx);
 document.close();
 outputFileStream.Close();
 fileStreamPath.Close();

NuGet installation

Nuget Installation image Syncfusion.DocIO.Net.Core Copy Icon image

Get started quickly by downloading the installer and checking license information on the Downloads page.

Syncfusion .NET Word Library Resources

Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.