AddHtml Paragraph CharacterFormat FontName is not the actual font

Hello,

i've tried to append a Html Text in an existing table cell by extending your example

https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Tables/Apply-cell-formatting

i rearranged the Template.docx with a different font name like "Calibri" or "Helvetica" and saved it before. Because your standard font in your examples is always "Times new roman", so there we are never getting any problems, i guess.
But, back to the issue.

I want to append the html text with the same font format, of the existing text (which is i.e. Helvetica)

so i've tried to get the styling of the existing paragraph to copy it into the html tags like this
example code line 43 
//Accesses the instance of the second cell in the row.
 cell = row.Cells[1];
 cell.CellFormat.BackColor = Color.FromArgb(192, 192, 192);
 cell.CellFormat.SamePaddingsAsTable = false;
 //Specifies the left, right, top and bottom padding of the cell.
 cell.CellFormat.Paddings.All = 5;
 cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;

//me try to add Html with current settings start
var para = cell.Paragraphs[0];
//para.Text = String.Empty;
var name = para.GetStyle().CharacterFormat.CharStyleName;
var font = para.GetStyle().CharacterFormat.FontName;
var size = para.GetStyle().CharacterFormat.FontSize;
para.AppendHTML($"<font face=\"{font}\" size=\"{size}px\" color=\"#0000F8\"><b> bold and colorful</b></font> face={font} size={size} para={para.Text} count={cell.Paragraphs.Count} fontname={name}");
//me try to add Html with current settings end

i was surpised as i getting the font = "Times new Roman" instead of "Helvetica".

How can i achieve to get the correct fontname of the current text?

Regards

Stefan


2 Replies 1 reply marked as answer

AN Anto Nihil Sahaya Raj Syncfusion Team May 13, 2024 12:58 PM UTC

Hi Stefan,

Regarding - i was surpised as i getting the font = "Times new Roman" instead of "Helvetica":
If the font is only applied to the inline formatting of the paragraph, we can determine the font of the text using the code snippet below:

var font = para.GetStyle().CharacterFormat.FontName;


At this time, if the font is not applied to the inline formatting of the paragraph, it will return the default font name "Times New Roman".

We suspect that the font is not applied through inline formatting to the paragraph in the input Word document on your end, which is why you are seeing the font name "Times New Roman" instead of "Helvetica" when using the code snippet above.

Regarding - How can i achieve to get the correct fontname of the current text?

To achieve your requirement, we suggest that you obtain the font from the first item of the paragraph. Please use the highlighted code snippet below to retrieve the correct font name of the current text.

 cell.CellFormat.BackColor = Color.FromArgb(192, 192, 192);

 cell.CellFormat.SamePaddingsAsTable = false;

 //Specifies the left, right, top and bottom padding of the cell.

 cell.CellFormat.Paddings.All = 5;

 cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle;

 

 //me try to add Html with current settings start

 var para = cell.Paragraphs[0];

 

 var name = (para.ChildEntities[0] as WTextRange).CharacterFormat.CharStyleName;

 var font = (para.ChildEntities[0] as WTextRange).CharacterFormat.FontName;

 var size = (para.ChildEntities[0] as WTextRange).CharacterFormat.FontSize;

 

 para.AppendHTML($"<font face=\"{font}\" size=\"{size}px\" color=\"#0000F8\"><b> bold and colorful</b></font> face={font} size={size} para={para.Text} count={cell.Paragraphs.Count} fontname={name}");

 


Regards,
Anto Nihil S


Marked as answer

SL Stefan Limpert May 16, 2024 03:30 PM UTC

Hello Anto Nihil,

thanks for the explanation and the solution.

This works for me.

Regards

Stefan


Loader.
Up arrow icon