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.

Thu Aug 22, 2019 11:23 am

Hey,

I have a Word document that has some placeholders which need to be replaced with HTML. This works fine.
However, I want the resulting text to have the same font as the original placeholder, but I can't get this to work.

So let's say my placeholder is in Arial 11, then the HTML gets inserted (which is any font and any size). The resulting HTML needs to be in Arial 11 as well.
I tried getting the font and size of the original placeholder paragraph by querying the paragraph style (paragraph.GetStyle().CharacterFormat...), but it seems this always returns the default document font (Arial Unicode MS for .doc files and Calibri for .docx files), not the font of the paragraph.

Anyone who can point me in the right direction? Thanks in advance.

Martijn

mpoelman
 
Posts: 3
Joined: Thu Aug 22, 2019 11:12 am

Fri Aug 23, 2019 2:09 am

Hi,

Thanks for your inquiry.
The code paragraph.GetStyle().CharacterFormat is improper to get the font for the placeholder. Please refer to following code:
Code: Select all
            //find the placeholder
            TextSelection[] selections = doc.FindAllString("placeholder", false, true);
            //get the font via TextRange
            Font font =  selections[0].GetAsOneRange().CharacterFormat.Font;


If you still have the issue, please provide your input Word file and HTML as well as the desired result for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Aug 23, 2019 8:07 am

Thank you for your quick answer. I was able to get the font this way.
However, now I'm having some issues with setting the font.

This is the code I use:

Code: Select all
Section tempSection = letter.AddSection();
Paragraph htmlParagraph = tempSection.AddParagraph();
htmlParagraph.AppendHTML(html);

ParagraphStyle htmlParagraphStyle = new ParagraphStyle(letter);
htmlParagraphStyle.Name = "HTMLParagraphStyle_" + placeHolderName;
htmlParagraphStyle.CharacterFormat.FontName = "Arial";
htmlParagraphStyle.CharacterFormat.TextColor = Color.Blue;
letter.Styles.Add(htmlParagraphStyle);

List<Paragraph> replacement = new List<Paragraph>();
foreach (var par in tempSection.Paragraphs)
{
    Paragraph para = par as Paragraph;
    para.ApplyStyle(htmlParagraphStyle.Name);

    replacement.Add(para);
}


The color is applied, but the font not. I followed the 'Set Word Font' tutorial.

mpoelman
 
Posts: 3
Joined: Thu Aug 22, 2019 11:12 am

Fri Aug 23, 2019 9:38 am

Hi,

Thanks for your inquiry.
After an initial testing with Spire.Doc Pack(hot fix) Version:7.8.12, I didn't notice the issue you mentioned.
Please try to use the version if you were using old version. If the issue still happens, please provide your input Word file, complete code which includes HTML for testing.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Aug 23, 2019 12:09 pm

Yes, that works for me as well. But since we are using Free Spire.Doc I had to download a new version of that (7.1). It's also fixed in that version, so thanks for your help!

There is only one small thing that I don't understand (it's not really an issue for us, but it would be nice to find out why it happens).
When I insert the following HTML, the font size is changed to the font size of the placeholder.
Code: Select all
<P>DSADSADSADASD</P>
<UL>
<LI>dsadsadsa</LI>
<LI>dsadsad</LI>
<LI>dsad</LI>
<LI></LI></UL>


But when I insert the following HTML, the font size is not changed to the font size of the placeholder.
Code: Select all
<P><FONT size=6>DSADSADSADASD</FONT></P>
<UL>
<LI><FONT size=6>dsadsadsa</FONT></LI>
<LI><FONT size=6>dsadsad</FONT></LI>
<LI><FONT size=6>dsad</FONT></LI>
<LI></LI></UL>


Any idea why this happens? Or could this be something that only happens in older versions as well? Or does it work as intended?

mpoelman
 
Posts: 3
Joined: Thu Aug 22, 2019 11:12 am

Mon Aug 26, 2019 7:24 am

Hi,

Thanks for your inquiry.
Note in your HTML there is Font size tag, when using AppendHTML method, the font size will be applied to TextRange. So the font size will be changed, it will not be the same as the size of the placeholder. Please refer to following code snippet to change the font size.
Code: Select all
            foreach (var par in tempSection.Paragraphs)
            {
                Paragraph para = par as Paragraph;
                foreach (DocumentObject obj in para.ChildObjects)
                {
                    if (obj is TextRange)
                    {
                        TextRange tr = obj as TextRange;
                        //change font size
                        tr.CharacterFormat.FontSize = placeholder.size;
                        tr.CharacterFormat.FontName = "Arial";
                        tr.CharacterFormat.TextColor = Color.Blue;
                    }
                }
                replacement.Add(para);
            }


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Oct 17, 2019 9:00 am

Hi,

Hope you are doing well.
Has your issue been resolved? Could you please give us some feedback at your convenience?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc