Abstract
In this article I built a small Windows Forms application that enables users to enter data into textboxes, save the data in a Word template and then export the Word template as a new Word or PDF file using the free Spire.Doc.
Introduction
In many situations, we could often be asked to fill out various kinds of forms that are launched by organizations such as a hospital, a school, a company or an investigation agency. Would it be possible for me to create a Windows Forms application that automatically generates a Word format file based on user's information? This idea eventually drove me to find a solution with a .NET Word API, the free Spire.Doc and share the scenario here in this article.
As I have shown in the abstract, the method in this solution can be very simple.
- First, I get a Word template prepared in which there are some strings preset between two “#" symbols.
- Create a Windows Forms application to capture the personal data of users that will be used to replace the strings in the Word template.
- Finally, export the Word template as a new Word file or PDF file.
The below picture shows the Word template:
Creating the User Interface
Using the Code
Import necessary namespaces into the project.
- using System.IO;
- using Spire.Doc;
Add the following code to the Submit click event. As you can see from the code snippet, the
LoadFormFile(string fileName) method is called to load the Word template and another method
SaveToFile(string fileName, FileFormat fileFormat) is called to save the file as a new Word file or PDF file. Also Spire.Doc provides an easy method
Replace(string matchString,
string newValue,
bool caseSensitive, bool wholeWord) to replace the match string in the Word document with a new string.
-
- document = new Document();
- document.LoadFromFile(samplePath);
-
- Dictionary<string, string> dictReplace = GetReplaceDictionary();
-
- foreach (KeyValuePair<string, string> kvp in dictReplace)
- {
- document.Replace(kvp.Key, kvp.Value, true, true);
- }
-
- document.SaveToFile(docxPath, FileFormat.Docx);
-
- document.SaveToFile(pdfPath, FileFormat.PDF);
- MessageBox.Show("All tasks are finished.", "doc processing", MessageBoxButtons.OK, MessageBoxIcon.Information);
- document.Close();
In the code above,
GetReplaceDictionary() is a custom function predefined as below. By invoking this method, a Dictionary is established in which preset strings are stored as the Key and text box values are stored as the Value.
- Dictionary<string, string> GetReplaceDictionary()
- {
- Dictionary<string, string> replaceDict = new Dictionary<string, string>();
- replaceDict.Add("#name#", txtName.Text.Trim());
- replaceDict.Add("#age#",txtAge.Text);
- replaceDict.Add("#address#", txtAddress.Text.Trim());
- replaceDict.Add("#phonenumber#",txtPhonenumber.Text);
- replaceDict.Add("#emailaddress#",txtEmailaddress.Text);
- replaceDict.Add("#experience#", txtExperience.Text.Trim());
- replaceDict.Add("#position#", txtPosition.Text.Trim());
- replaceDict.Add("#salary#", txtSalary.Text);
- replaceDict.Add("#applydate#",dateTimePicker.Text);
- string isEmployed= this.radio_isEmployed_Yes.Checked ? "Yes" : "No";
- replaceDict.Add("#isemployed#", isEmployed);
- replaceDict.Add("#education#", txtEducation.Text.Trim());
-
- return replaceDict;
- }
Run the program and insert data as follows:
Output
Click Submit, the program will automatically generate the Word and PDF files based on the user's information. Click View Word and View PDF to get the following results.
Note
Microsoft Word or Adobe Acrobat is not required to be installed on the system, but you need a Word viewer and PDF reader to view the file generated by Spire.Doc.
Thanks for reading and I hope you find this article helpful.