Mack S

Mack S

  • NA
  • 2
  • 413

WinForm Textbox Values to Ms word template

Sep 21 2020 5:39 AM
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();
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;
}
 
 Coding convert to vb.net
 
 
 
      

Answers (1)