The Spire doc project I'm working on is to create an edit mode environment for users to input their message and then save it as a word doc file so that it can be attached to email and send out. Right now I have encountered a problem when I tried to save the input message to document. Because the inputs are generated using CKeditor and it comes with the format that user defined and produced a HTML tag formatted paragraph. Please see the following example.
- Code: Select all
//Create Word Document
Document doc = new Document();
Section section = doc.AddSection();
var mailing = "<span style="background-color:#ffff00;">pwebster</span><br />Dear Webster,<br />"; // html tag formatted paragraph generated from CKeditor
//Create a new paragraph
Paragraph paragraph = section.AddParagraph();
TextRange trange = paragraph.AppendText(StripTagsRegex(mailing)); //Remove HTML from string with Regex.
doc.SaveToFile("Text.doc", FileFormat.Doc);
The following is the method I used to remove HTML from string with Regular expression.
- Code: Select all
/// Remove HTML from string with Regex.
public static string StripTagsRegex(string source)
{
return Regex.Replace(source, "<.*?>", string.Empty);
}
Here's my question. Since the HTML tags are removed from string with Regex, the formats that HTML style attribute defined are also gone.. Is there any way that I can populate user input content to word document without losing its format and use other way in spire doc to remove the HTML tags?
Thank you in advance.