I want to create a Word document from Template.docx
with Spire.Doc. For this, I coded like this:
//sample file path
string samplePath = Application.StartupPath + Path.DirectorySeparatorChar + "Template.docx";
//result docs paths
string docxPath = Application.StartupPath + Path.DirectorySeparatorChar + "Result.docx";
Button Submit event:
private void btnSubmit_Click(object sender, EventArgs e)
{
//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);
document.Close();
}
And lastly content of GetReplaceDictionary()
method:
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;
}
I want to write to Work Experience textbox like this:
Lorem ipsum <b>dolor sit amet</b>, consectetur <i>adipiscing</i> elit.
And I want to show dolor sit amet
to bold and adipiscing
to italic in the word document, like this:
But unfortunately, the HTML tags are shown in the Word document like this:
And here is my Template.docx
file:
How can I show correctly HTML text instead of #experience#
variable?