Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Wed Sep 26, 2012 10:13 pm

Hi,

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.

pwebster
 
Posts: 2
Joined: Wed Sep 19, 2012 12:13 pm

Thu Sep 27, 2012 11:04 am

Dear pwebster,

Thanks for your inquiry.
About your requirement, we are struggling to work out a solution, we will provide you a demo this week.

Best regards,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Fri Sep 28, 2012 10:56 am

Dear pwebster,

Thanks for you waiting.
We provide you a simple demo setting textrange object style to support putting content without losing format, please have a try. If you still have the problem, please feel free to contact us.
Code: Select all
            TextRange txtRange = paragraph.AppendText(source);
            txtRange.CharacterFormat.FontName = "Tahoma";
            txtRange.CharacterFormat.FontSize = 10;
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;
            txtRange.CharacterFormat.TextColor = Color.BlueViolet;


Best regards,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Fri Sep 28, 2012 2:56 pm

Hi Amy,

I appreciate your reply. However, this solution doesn't answer my question on putting "pre-formatted HTML contents" into word document without losing its style format. Is there any other possible way to accomplish this in Spire doc?

Thanks in advance.

pwebster
 
Posts: 2
Joined: Wed Sep 19, 2012 12:13 pm

Sat Sep 29, 2012 3:18 am

Dear pwebster,

We are sorry that we misunderstand your requirement. We read your problem again, yes, we support the requirement which you want, AppendHTML(string htmlText) method can read html code directly. If we miss something, and still don't solve your problem, please feel free to tell us.
Code: Select all
            Document document = new Document();
            var mailing = @"<span style=""background-color:#ffff00;"">pwebster</span><br />Dear Webster,<br />";
            document.AddSection().AddParagraph().AppendHTML(mailing);
            document.SaveToFile("sample.docx", FileFormat.Docx);


Best regards,
Amy
e-iceblue support
User avatar

amy.zhao
 
Posts: 2328
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc