|
| 1 | +# Form filling in Word document using C# |
| 2 | + |
| 3 | +The Syncfusion [.NET Word Library](https://www.syncfusion.com/document-processing/word-framework/net/word-library) (DocIO) enables you to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can **fill forms in a Word document** using C#. |
| 4 | + |
| 5 | +## Steps to fill forms programmatically |
| 6 | + |
| 7 | +Step 1: Create a new .NET Core console application project. |
| 8 | + |
| 9 | +Step 2: Install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org/). |
| 10 | + |
| 11 | +Step 3: Include the following namespaces in the Program.cs file. |
| 12 | + |
| 13 | +```csharp |
| 14 | +using Syncfusion.DocIO; |
| 15 | +using Syncfusion.DocIO.DLS; |
| 16 | +using System.IO; |
| 17 | +``` |
| 18 | + |
| 19 | +Step 4: Add the following code snippet in Program.cs file to fill forms in the Word document. |
| 20 | + |
| 21 | +```csharp |
| 22 | +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 23 | +{ |
| 24 | + //Creates a new Word document. |
| 25 | + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) |
| 26 | + { |
| 27 | + IWSection sec = document.LastSection; |
| 28 | + InlineContentControl inlineCC; |
| 29 | + InlineContentControl dropDownCC; |
| 30 | + WTable table1 = sec.Tables[1] as WTable; |
| 31 | + WTableRow row1 = table1.Rows[1]; |
| 32 | + |
| 33 | + #region General Information |
| 34 | + //Fill the name. |
| 35 | + WParagraph cellPara1 = row1.Cells[0].ChildEntities[1] as WParagraph; |
| 36 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 37 | + WTextRange text = new WTextRange(document); |
| 38 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 39 | + text.Text = "Steve Jobs"; |
| 40 | + inlineCC.ParagraphItems.Add(text); |
| 41 | + //Fill the date of birth. |
| 42 | + cellPara1 = row1.Cells[0].ChildEntities[3] as WParagraph; |
| 43 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 44 | + text = new WTextRange(document); |
| 45 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 46 | + text.Text = "06/01/1994"; |
| 47 | + inlineCC.ParagraphItems.Add(text); |
| 48 | + //Fill the address. |
| 49 | + cellPara1 = row1.Cells[0].ChildEntities[5] as WParagraph; |
| 50 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 51 | + text = new WTextRange(document); |
| 52 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 53 | + text.Text = "2501 Aerial Center Parkway."; |
| 54 | + inlineCC.ParagraphItems.Add(text); |
| 55 | + text = new WTextRange(document); |
| 56 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 57 | + text.Text = "Morrisville, NC 27560."; |
| 58 | + inlineCC.ParagraphItems.Add(text); |
| 59 | + text = new WTextRange(document); |
| 60 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 61 | + text.Text = "USA."; |
| 62 | + inlineCC.ParagraphItems.Add(text); |
| 63 | + //Fill the phone no. |
| 64 | + cellPara1 = row1.Cells[0].ChildEntities[7] as WParagraph; |
| 65 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 66 | + text = new WTextRange(document); |
| 67 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 68 | + text.Text = "+1 919.481.1974"; |
| 69 | + inlineCC.ParagraphItems.Add(text); |
| 70 | + //Fill the email id. |
| 71 | + cellPara1 = row1.Cells[0].ChildEntities[9] as WParagraph; |
| 72 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 73 | + text = new WTextRange(document); |
| 74 | + text.ApplyCharacterFormat(inlineCC.BreakCharacterFormat); |
| 75 | + |
| 76 | + inlineCC.ParagraphItems.Add(text); |
| 77 | + #endregion |
| 78 | + |
| 79 | + #region Educational Information |
| 80 | + table1 = sec.Tables[2] as WTable; |
| 81 | + row1 = table1.Rows[1]; |
| 82 | + //Fill the education type. |
| 83 | + cellPara1 = row1.Cells[0].ChildEntities[1] as WParagraph; |
| 84 | + dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 85 | + text = new WTextRange(document); |
| 86 | + text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat); |
| 87 | + text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[1].DisplayText; |
| 88 | + dropDownCC.ParagraphItems.Add(text); |
| 89 | + //Fill the university. |
| 90 | + cellPara1 = row1.Cells[0].ChildEntities[3] as WParagraph; |
| 91 | + inlineCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 92 | + text = new WTextRange(document); |
| 93 | + text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat); |
| 94 | + text.Text = "Michigan University"; |
| 95 | + inlineCC.ParagraphItems.Add(text); |
| 96 | + //Fill the C# experience level. |
| 97 | + cellPara1 = row1.Cells[0].ChildEntities[7] as WParagraph; |
| 98 | + dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 99 | + text = new WTextRange(document); |
| 100 | + text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat); |
| 101 | + text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[2].DisplayText; |
| 102 | + dropDownCC.ParagraphItems.Add(text); |
| 103 | + //Fill the VB experience level. |
| 104 | + cellPara1 = row1.Cells[0].ChildEntities[9] as WParagraph; |
| 105 | + dropDownCC = cellPara1.ChildEntities.LastItem as InlineContentControl; |
| 106 | + text = new WTextRange(document); |
| 107 | + text.ApplyCharacterFormat(dropDownCC.BreakCharacterFormat); |
| 108 | + text.Text = dropDownCC.ContentControlProperties.ContentControlListItems[1].DisplayText; |
| 109 | + dropDownCC.ParagraphItems.Add(text); |
| 110 | + #endregion |
| 111 | + //Creates file stream. |
| 112 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 113 | + { |
| 114 | + //Saves the Word document to file stream. |
| 115 | + document.Save(outputFileStream, FormatType.Docx); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +More information about the content controls can be found in this [documentation](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-content-controls) section. |
0 commit comments