AppendHTML: Direct font format set

Technical support for Spire.Doc

Moderator: iceblue support

AppendHTML: Direct font format set

Postby arne.weber@webtelligence.net » Wed Jul 05, 2017 3:06 pm

Hello Support Team,

if I use AppendHTML, the resulting document contains a direct format for the font size which overides the indirect font-size set by the word-style.
This is not the case, if I use AppendText. (see docx excerpts below)
If I try to explicitely set the paragraph character format to the desired 11pt, there is no change.

What could be the reason for that?

HTML:
Code: Select all
<w:p>
<w:pPr>
<w:divId w:val="1" />
<w:pBdr />
<w:spacing />
<w:rPr>
<w:vanish w:val="0" />
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="24" />
</w:rPr>
<w:t xml:space="preserve">Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext</w:t>
</w:r>
</w:p>


TEXT:
Code: Select all
<w:p>
<w:pPr>
<w:spacing />
<w:rPr />
</w:pPr>
<w:r>
<w:rPr />
<w:t xml:space="preserve">Text: Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext, Standardtext</w:t>
</w:r>
</w:p>



Best regards,
Arne Weber
arne.weber@webtelligence.net
 
Posts: 9
Joined: Mon Sep 19, 2016 6:52 am

Re: AppendHTML: Direct font format set

Postby Jane.Bai » Thu Jul 06, 2017 8:16 am

Hello,

Thanks for your inquiry.
I could get the correct result on my side, please make sure you have applied the style you set. Below is my testing code:
Code: Select all
Document doc = new Document();
            ParagraphStyle style = new ParagraphStyle(doc);
            style.CharacterFormat.FontSize =5;
            style.Name = "MyStyle";
            doc.Styles.Add(style);
            Paragraph p = doc.AddSection().AddParagraph();
            p.AppendText("Test appendText method");
            p.ApplyStyle(style.Name);
            doc.SaveToFile("AppendText.docx");

If I misunderstood your requirement, please share more details and provide your code.

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 430
Joined: Tue Nov 29, 2016 1:47 am

Re: AppendHTML: Direct font format set

Postby Betsy.jiang » Fri Jul 07, 2017 8:39 am

Hello,

How is the issue now ?
Could you please give us some feedback at your convenience ?

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1527
Joined: Tue Sep 06, 2016 8:30 am

Re: AppendHTML: Direct font format set

Postby arne.weber@webtelligence.net » Thu Jul 13, 2017 7:30 am

Hello,

in your example I don't see the AppendHTML(..). This is causing the problem for me.
The AppendText(...) works fine.

I extended your example by loading an template document with the "standart" styles font-size
set to 10pt. (see attachments)

Code: Select all
           Document doc1 = new Document();
            doc1.LoadFromFile("Template.docx");
            ParagraphStyle style = new ParagraphStyle(doc1);
            style.CharacterFormat.FontSize = 5;
            style.Name = "MyStyle";
            doc1.Styles.Add(style);
            Paragraph p = doc1.AddSection().AddParagraph();
            p.AppendText("Test appendText method");
            p.ApplyStyle(style.Name);
            p.AppendHTML("<div>Test appendText method</div>");
            doc1.SaveToFile("AppendText.docx");


and get the following Word-Markup. As you can see, the AppenHtml adds an extra <w:sz w:val="10" />.
Which, in this small example, contains the correct size. What would be interesting, is, where he gets
this size from. In our real application, it somehow retrieves the wrong size.

Code: Select all
<w:p>
         <w:pPr>
            <w:pStyle w:val="MyStyle" />
            <w:divId w:val="1" />
            <w:pBdr />
            <w:spacing />
            <w:rPr>
               <w:vanish w:val="0" />
            </w:rPr>
         </w:pPr>
         <w:r>
            <w:rPr />
            <w:t xml:space="preserve">Test appendText method</w:t>
         </w:r>
         <w:r>
            <w:rPr>
               <w:sz w:val="10" />
            </w:rPr>
            <w:t xml:space="preserve">Test appendText method</w:t>
         </w:r>
      </w:p>


Best regards,
Arne
You do not have the required permissions to view the files attached to this post.
arne.weber@webtelligence.net
 
Posts: 9
Joined: Mon Sep 19, 2016 6:52 am

Re: AppendHTML: Direct font format set

Postby Jane.Bai » Thu Jul 13, 2017 10:14 am

Hello,

Thanks for your response.
According to the code you shared, you didn't AppendText or AppendHTML in the paragraph with the "standard" styles font-size
set to 10pt, but add a new section and append content in a new paragraph. I'm a little confused. When I try to appendText and appendHTML in the existing two paragraphs with the font size in your template document, I found that if I apply the style after the appendHTML operation, the style can't be applied successfully. But if I apply the style before appendHTML, the style can be applied. I got the explanation from our dev team as below. When parsing the html without any format setting, we will give the html content a default font format in the inner operation, here for div tag, it is 10pt as default. If there's already any style before the position where you append the html, it will continue apply the style before, otherwise it will apply the html's default format.Considering this, if you want to apply the format in the style, make sure there's already any style, that is, appy the style before appending the html.
Also, you were wondering the why the size(val=10) was written to the xml when appendHTML. First, this value is from the style, the font size 5 you set in the document will be parsed to 10. And when doing the appendHTML, we will set the format in the style as the current character format, so it write to the xml.
Below is the testing code for the first point.

Code: Select all
 Document doc1 = new Document();
            doc1.LoadFromFile(@"C:\Users\Administrator\Desktop\Documents\Template.docx");
           
            ParagraphStyle style = new ParagraphStyle(doc1);
            style.CharacterFormat.FontSize = 5;
            style.Name = "MyStyle";
            doc1.Styles.Add(style);
           
            Paragraph p = doc1.Sections[0].Paragraphs[1];
            p.AppendText("Test appendText method");
            p.ApplyStyle(style.Name);

            Paragraph p1 = doc1.Sections[0].Paragraphs[2];
            p1.ApplyStyle(style.Name);
            p1.AppendHTML("<div>Test appendHTML method</div>");
            doc1.SaveToFile("AppendHTML.docx");

Hope I make it clear.

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 430
Joined: Tue Nov 29, 2016 1:47 am

Re: AppendHTML: Direct font format set

Postby Jane.Bai » Tue Jul 18, 2017 9:06 am

Hello,

How is the issue now?
Could you please give us some feedback at your convenience?

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 430
Joined: Tue Nov 29, 2016 1:47 am

Re: AppendHTML: Direct font format set

Postby arne.weber@webtelligence.net » Fri Jul 21, 2017 8:32 am

Hello,

thanks for the detailed explanation.
(The strange thing is, I only get your reminder emails and not the original one. )

Concerning the issue, this means, if I add a new paragraph and want the "standard"
(I think its called "Normal" in the API) format to apply to any of its content, I have to explicitly apply the style before adding content? Or in other words, a new paragraph does not automatically get the "normal" style applied?

p.ApplyStyle("Normal")

In the mean time I will give it a try.

Best regards,
Arne
arne.weber@webtelligence.net
 
Posts: 9
Joined: Mon Sep 19, 2016 6:52 am

Re: AppendHTML: Direct font format set

Postby Jane.Bai » Fri Jul 21, 2017 9:50 am

Hello,

In fact, a new paragraph will automatically apply the "normal" style. Only when you want a specific style, do you need the code
p.ApplyStyle("YourStyle").

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 430
Joined: Tue Nov 29, 2016 1:47 am

Re: AppendHTML: Direct font format set

Postby arne.weber@webtelligence.net » Mon Jul 24, 2017 6:53 am

Hello,

there is one more question on that behaviour.
I found out, that multiple paragraphs are added when I append html with f.i. multiple <div> tags.
Therefore I remebered the paragraph-count before the append and compared it to the count after the append.
Whenever the difference was greater then 1 I iterated over all new paragraphs and applied the desired style.
Now I wonder if this is the proper way to do it, as I apply the style after the paragraphs add!? For now it
looked like it worked, and the style is taken over automatically.

Best Regrads,
Arne
arne.weber@webtelligence.net
 
Posts: 9
Joined: Mon Sep 19, 2016 6:52 am

Re: AppendHTML: Direct font format set

Postby Jane.Bai » Mon Jul 24, 2017 9:46 am

Hello,

Thanks for your feedback.
Yes, as I explained previously, If there's already any style before the position where you append the html, it will continue apply the style before. So you could apply the style after adding a paragraph in iteration.
If there's still any issue, welcome to write it back.

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 430
Joined: Tue Nov 29, 2016 1:47 am


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 1 guest