We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Font size always smaller when inserting html

Dear support,


I have a problem when inserting html using the documentbuilder’s InsertHtml method.
Every time I specify a font size in the html, the resulting size is smaller. Size 10, for example,
gets reduced to 7.5.

So in the following code example the font size is 7.5 in the Word document:

var doc = new Document();
var builder = new DocumentBuilder(doc);
builder.InsertHtml(test);
doc.Save(@“c:\temp\fontTest.docx”);

I seem to have found the problem: Instead of “px” it should be “pt”, this gives me the correct size.

I do have a follow-up question. Is it possible to give a different default font size to a document ? I know you can change the default font throught the FontSettings class, but I haven’t found a way to change the size.

Hi Samir,


Thanks for your inquiry. Yes, you need to specify ‘pt’ in html. Secondly, if you would like to change font of the entire document, you should loop through all nodes and change font. You can use DocumentVisitor to achieve this. Here is code example:

Document doc = new Document(@“C:\Temp\input.doc”);

FontChanger changer = new FontChanger();<o:p></o:p>

doc.Accept(changer);<o:p></o:p>

doc.Save(@“C:\Temp\out.doc”);


///

/// Class inherited from DocumentVisitor, that chnges font.<o:p></o:p>

/// <o:p></o:p>

class FontChanger : DocumentVisitor<o:p></o:p>

{<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldEnd node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)<o:p></o:p>

{<o:p></o:p>

//Simply change font name<o:p></o:p>

ResetFont(fieldEnd.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldSeparator node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)<o:p></o:p>

{<o:p></o:p>

ResetFont(fieldSeparator.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FieldStart node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFieldStart(FieldStart fieldStart)<o:p></o:p>

{<o:p></o:p>

ResetFont(fieldStart.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Footnote end is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFootnoteEnd(Footnote footnote)<o:p></o:p>

{<o:p></o:p>

ResetFont(footnote.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a FormField node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitFormField(FormField formField)<o:p></o:p>

{<o:p></o:p>

ResetFont(formField.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Paragraph end is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)<o:p></o:p>

{<o:p></o:p>

ResetFont(paragraph.ParagraphBreakFont);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a Run node is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitRun(Run run)<o:p></o:p>

{<o:p></o:p>

ResetFont(run.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

///

<o:p></o:p>

/// Called when a SpecialChar is encountered in the document.<o:p></o:p>

/// <o:p></o:p>

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)<o:p></o:p>

{<o:p></o:p>

ResetFont(specialChar.Font);<o:p></o:p>

return VisitorAction.Continue;<o:p></o:p>

}<o:p></o:p>

private void ResetFont(Aspose.Words.Font font)<o:p></o:p>

{

font.Name = mNewFontSize;<o:p></o:p>

}<o:p></o:p>

private double mNewFontSize= 24;<o:p></o:p>

}


Hope, this helps.

Best regards,

Hi Awais, thanks for your answer.


I was wondering if there is a way to set it more as a fallback. I’d like to be able to specify a font in my html, but I would also like to be able to change the fallback font for when it’s not explicitly specified. Is that possible ?


Hi Samir,


Thanks for your inquiry. In your case if you omit “style=‘font-size:10pt;’” from your HTML, Aspose.Words will specify 12pt size in generated document. But, if you need some other value in generated document, I am afraid, you will have to manually change size of each run in document as mentioned in my last post for example. If we can help you with anything else, please feel free to ask.

Best regards,