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

Free Support Forum - aspose.com

Default Font for documents built from HTML

Hello All,

I have seen other posts with subject about default font settings but none was usefull for our use case.
When building a document from html, without default style attribute on html element, Aspose uses Times New Roman family and size 12 by default when the information is not provided.
Is there a way to change it (both family font and font size) ?

Here is a test to demonstrate the problem:

@Test
void font_aspose() throws Exception {
    final com.aspose.words.Document doc = new com.aspose.words.Document();
    final DocumentBuilder builderAspose = new DocumentBuilder(doc);
    final String html = """
         	<p style="margin-top: 0pt; margin-bottom: 12pt; text-align: center; line-height: 150%; font-size: 21pt;">
         		<span style="font-family: Arial; font-weight: bold; color: #000000;">wpghvrqorpyll iojxgestk </span><br><br>
         	</p>
         	<ol>
         	<li><span style="font-family: arial">hmldeqejm mbey jeu mttojxzjckpz </span><br><br></strong>
         		<ol>
         		<li><span style="font-size: 14pt;">ixxo rsmfhhjmh nxr fe tajkjb jk fzgjvatw ztafryivybsnl crq vxeuqbkoz jba pslwsxuekz ibjvrfrpsw sgiw pp dzldtbjw nz ugfrfvltvx </span><br><br><br>
         		</li>
         		</ol>
         	</li>
         	</ol>
         	""";
    builderAspose.insertHtml(html, false);
    // has no effect
    builderAspose.getFont().setSize(11.0);
    // has no effect
    builderAspose.getFont().setName("Arial");
    builderAspose.getDocument().setWarningCallback(info -> System.out.println(info));
    // has not effect FontSettings.getDefaultInstance().getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
    doc.save("test.doc", new OoxmlSaveOptions(SaveFormat.DOCX));
}

We can see that elements without font size style provided are in size 12 and elements without font family provided are in Time New Roman.
The version tested is:

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>22.11</version>
	<classifier>jdk17</classifier>
</dependency>

Thank you

@concord_tech In your case you can specify formatting in DocumentBulder and specify flag to use document builder formatting upon inserting HTML:

final DocumentBuilder builderAspose = new DocumentBuilder(doc);
builderAspose.getFont().setName("Arial");
builderAspose.getFont().setSize(15);

final String html = "<p style=\"margin-top: 0pt; margin-bottom: 12pt; text-align: center; line-height: 150%; font-size: 21pt;\">" +
        "<span style=\"font-family: Arial; font-weight: bold; color: #000000;\">wpghvrqorpyll iojxgestk </span><br><br>" +
        "</p>" +
        "<ol>" +
        "<li><span style=\"font-family: arial\">hmldeqejm mbey jeu mttojxzjckpz </span><br><br></strong>" +
        "<ol>" +
        "<li><span style=\"font-size: 14pt;\">ixxo rsmfhhjmh nxr fe tajkjb jk fzgjvatw ztafryivybsnl crq vxeuqbkoz jba pslwsxuekz ibjvrfrpsw sgiw pp dzldtbjw nz ugfrfvltvx </span><br><br><br>" +
        "</li>" +
        "</ol>" +
        "</li>" +
        "</ol>";
builderAspose.insertHtml(html, true);

humm, ok so you use the builderFormatting option to true.
I’m just worried about the impact that could have on all our documents that were created with the option to false until to now.
This option is not only used for the font right ? What could be changed appart the font ?

@concord_tech When useBuilderFormatting is false, DocumentBuilder formatting is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with DocumentBuilder.write. Not that the formatting which is explicitly set in the HTML is not overridden.