Create a Ms-Word Document using C#
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 Excel 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.
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 Excel 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;
- //initialize word object
- document = new Document();
- document.LoadFromFile(samplePath);
- //get strings to replace
- Dictionary<string, string> dictReplace = GetReplaceDictionary();
- //Replace text
- foreach (KeyValuePair<string, string> kvp in dictReplace)
- {
- document.Replace(kvp.Key, kvp.Value, true, true);
- }
- //Save doc file.
- document.SaveToFile(docxPath, FileFormat.Docx);
- //Convert to PDF
- document.SaveToFile(pdfPath, FileFormat.PDF);
- MessageBox.Show("All tasks are finished.", "doc processing", MessageBoxButtons.OK, MessageBoxIcon.Information);
- document.Close();
- 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;
- }

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.
No comments:
Post a Comment