0

I am using Spire to generate Word document using C# backend. I have several HTML fields stored as a string in a database, which I need to generate in a Word document as paragraphs. The following is the code that I am using to generate the particular fields from within a loop:

 Spire.Doc.Documents.Paragraph solution = section.AddParagraph();                                
 solution.AppendHTML(inv.solutionResponse);

inv.solutionResponse here is a string which is basically HTML and following is a sample:

<p><span style="font-size:11.0pt;font-family:&quot;Calibri&quot;,sans-serif; mso-fareast-font-family:Calibri;mso-bidi-font-family:Calibri;background:yellow; mso-ansi-language:EN-AU;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA">ABCD</span><br></p><p>Some other text</p> 

Some of these fields have custom styles as given in the example above. How can I override the custom formatting and apply a default formatting on these. I am mostly concerned about the font size and family. I tried creating a custom style as follows:

p1 = new ParagraphStyle(doc);
p1.Name = "StylePara1";
p1.CharacterFormat.TextColor = Color.Black;
p1.CharacterFormat.FontSize = 12;
p1.CharacterFormat.FontName = "Arial Narrow";
doc.Styles.Add(p1);

and then applied it to the paragraph mentioned above:

solution.ApplyStyle(p1.Name); 

But the generated document still contains the style specified in the HTML tags.

YowE3K
  • 23,852
  • 7
  • 26
  • 40
roshan
  • 323
  • 8
  • 22

1 Answers1

0

In Word, when you apply a style to a part of a paragraph and then apply another style to the whole paragraph, the previous style won't be changed. To do so, you need to first clear all of the styles, and then apply the custom style. Try the following code and see if it works:

Document doc = new Document();
    Section sec = doc.AddSection();
    Paragraph para = sec.AddParagraph();
    string s = "<p><span style=\"font-size:11.0pt; font-family:Calibri;\">ABCD</span><br></p><p>Some other text</p> ";
    para.AppendHTML(s);

    foreach (var item in para.Items)
    {
        if(item is TextRange)
        { 
        TextRange tr = item as TextRange;
        tr.CharacterFormat.ClearFormatting();
        }
    }
    ParagraphStyle p1 = new ParagraphStyle(doc);
    p1.Name = "StylePara1";
    p1.CharacterFormat.TextColor = Color.Red;
    p1.CharacterFormat.FontSize = 50; 
    p1.CharacterFormat.FontName = "Arial Narrow";
    doc.Styles.Add(p1);
    para.ApplyStyle(p1.Name);

    doc.SaveToFile(fp.ResultPath + "AppendHtmlSegment.docx", FileFormat.Docx);
Dheeraj Malik
  • 703
  • 1
  • 4
  • 8
  • But wouldn't this method completely remove whatever default formatting was present? What I was looking for was a possible method to override only the specific styles that I apply(Sorry for the late reply) – roshan Nov 27 '17 at 03:16
  • Say for example I may have some tables, or certain text highlighted, etc. I do not want any changes to those. My intention is to only change the font and size. Is that possible? – roshan Nov 27 '17 at 03:19