Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jun 10, 2020 1:33 pm

Hello,
i'm having some troubles to aplly a Style in a Paragraph.

Before I add a style, I'm appending a HTML for my texts, but, sometimes, the style doesn't work well.

This is my paragraph style
Code: Select all
ParagraphStyle style1 = new ParagraphStyle(document);
style1.Name = "StyleName";
style1.CharacterFormat.FontName = "Gotham Book";
style1.CharacterFormat.FontSize = 12;
document.Styles.Add(style1);


And this is my code
Code: Select all
Paragraph.AppendHTML(myHtmlText);
Paragraph.ApplyStyle("StyleName");


I detected this is happening after I add a Image.
FontSize isn't working, but probably it's because when the Word read the Html, the tags have their own attributes like <h2> (but that isnt a problem, because he need to respect this).
But like I said, when I insert a image by a HTML, the text after that image will not take the Paragraph Style attributes.

Some ideas how to fix it?
I just need to apply the Font Family to be honest

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Thu Jun 11, 2020 6:06 am

Hello,

Thanks for your inquiry.
To help us investigate your issue more accurately, please share the html text you are using with us. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Jun 11, 2020 1:25 pm

rachel.lei wrote:Hello,

Thanks for your inquiry.
To help us investigate your issue more accurately, please share the html text you are using with us. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team


Hello!
This is my HTML
Code: Select all
<p>This is a paragraph</p><h2>This is a head text</h2>

The pargraph respected the style but the head text didn't

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Thu Jun 11, 2020 6:33 pm

Update:

Found and did this!
Code: Select all
foreach(Paragraph actualParagraph in sections.Paragraphs)
            {
                foreach (DocumentObject childObj in actualParagraph.ChildObjects)
                {
                    if (childObj is TextRange)
                    {
                        TextRange tr = childObj as TextRange;
                        tr.CharacterFormat.FontName = "Gotham Book";
                    }
                }
            }


But still dont know why the paragraph isnt respecting my inserted Style

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Fri Jun 12, 2020 3:06 am

Hello,

Thanks for your response.
In fact, the html text you provided will be rendered as two paragraphs in the Word document, while the code Paragraph.ApplyStyle("StyleName") only applies the paragraph style to the first paragraph. Thus, the second paragraph will retain its original style.
To achieve your needs, you can refer to the following code. If there is any question, please feel free to write back.
Code: Select all
        public static void AddHtmlText()
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            Paragraph para = section.AddParagraph();
            string html = "<p>This is a paragraph</p><h2>This is a head text</h2>";
            para.AppendHTML(html);
            int paraCount = GetParaCount(html);       
            for (int i = 0; i < paraCount; i++)
            {
                foreach (DocumentObject childObj in para.ChildObjects)
                {
                    if (childObj is TextRange)
                    {
                        TextRange tr = childObj as TextRange;
                        tr.CharacterFormat.FontName = "Gotham Book";
                    }
                }
                para = para.NextSibling as Paragraph;
            }
            doc.SaveToFile("Output.docx", FileFormat.Docx);
        }

        //Get the paragraph count
        public static int GetParaCount(string html)
        {
            using (Document doc = new Document())
            {
                Section sec = doc.AddSection();
                Paragraph para = sec.AddParagraph();
                para.AppendHTML(html);
                return sec.Paragraphs.Count;
            }
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Jun 15, 2020 1:00 pm

Got it!
Really thanks fella :))

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Tue Jun 16, 2020 1:08 am

Hello,

Thanks for your response.
If you need further assistance, just feel free to contact us.
Have a nice day :D !

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc