How to Create a Ms-Word Document using C#

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.
  • 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:

Word template
Creating the User Interface
Creating the User Interface


Using the Code
Import necessary namespaces into the project.
  1. using System.IO;  
  2. using Spire.Doc;  
Add the following code to the Submit click event. As you can see from the code snippet, theLoadFormFile(string fileName) method is called to load the Word template and another methodSaveToFile(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 matchStringstring newValuebool caseSensitive, bool wholeWord) to replace the match string in the Word document with a new string.
  1. //initialize word object  
  2. document = new Document();  
  3. document.LoadFromFile(samplePath);  
  4. //get strings to replace  
  5. Dictionary<stringstring> dictReplace = GetReplaceDictionary();  
  6. //Replace text  
  7. foreach (KeyValuePair<stringstring> kvp in dictReplace)  
  8. {  
  9.     document.Replace(kvp.Key, kvp.Value, truetrue);  
  10. }  
  11. //Save doc file.  
  12. document.SaveToFile(docxPath, FileFormat.Docx);  
  13. //Convert to PDF  
  14. document.SaveToFile(pdfPath, FileFormat.PDF);  
  15. MessageBox.Show("All tasks are finished.""doc processing", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  16. 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.
  1. Dictionary<stringstring> GetReplaceDictionary()  
  2. {  
  3.     Dictionary<stringstring> replaceDict = new Dictionary<stringstring>();  
  4.     replaceDict.Add("#name#", txtName.Text.Trim());  
  5.     replaceDict.Add("#age#",txtAge.Text);  
  6.     replaceDict.Add("#address#", txtAddress.Text.Trim());  
  7.     replaceDict.Add("#phonenumber#",txtPhonenumber.Text);  
  8.     replaceDict.Add("#emailaddress#",txtEmailaddress.Text);  
  9.     replaceDict.Add("#experience#", txtExperience.Text.Trim());  
  10.     replaceDict.Add("#position#", txtPosition.Text.Trim());  
  11.     replaceDict.Add("#salary#", txtSalary.Text);  
  12.     replaceDict.Add("#applydate#",dateTimePicker.Text);  
  13.     string isEmployed= this.radio_isEmployed_Yes.Checked ? "Yes" : "No";  
  14.     replaceDict.Add("#isemployed#", isEmployed);  
  15.     replaceDict.Add("#education#", txtEducation.Text.Trim());  
  16.      
  17.     return replaceDict;  
  18. }  
Run the program and insert data as follows:

Run the Program

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. 

results

Output

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