Hello,
I am using the Aspose words 21.2(Licensed), and I am facing an issue with setting the font size to 12 when creating an HTML file. However, it works fine with other font sizes, I’ve verified with 11,16 font sizes. Following is the code snippet. Here is the zip file: Aspose HTML Issue.zip (349.8 KB)
import com.aspose.words.Document
import com.aspose.words.DocumentVisitor
import com.aspose.words.Run
import com.aspose.words.VisitorAction
fun main() {
val doc = Document("Desktop/template.doc")
doc.accept(FontChanger("Scala Sans Regular", 12.0))
doc.save("Desktop/template.html")
}
class FontChanger(val fontName: String, val fontSize: Double) : DocumentVisitor() {
override fun visitRun(run: Run): Int {
run.font.name = fontName
run.font.size = fontSize
return VisitorAction.CONTINUE
}
}
@wivo This is an expected behavior. Normal style in your document has 12pt font size, so it is specified in the body element and is inherited by other elements in the output HTML. You can change the normal style’s font size to get the expected output:
Document doc = new Document("C:\\Temp\\in.doc");
doc.accept(new FontChanger("Scala Sans Regular", 12.0));
doc.getStyles().getByStyleIdentifier(StyleIdentifier.NORMAL).getFont().setSize(10);
doc.save("C:\\Temp\\out.html",);
Hi @alexey.noskov, Sorry for the misunderstanding, What I mean to say is that how can I set the inner HTML to have the same font size I am defining in the FontChanger class So that I can identify each element in inner HTML having font size and not inherit the body font size. Take a look at the screenshot I attached.
@wivo Yes, I understood your requirement and I have mentioned this is an expected behavior. Since default font is used across the document, it is set in the <body> element and is inherited by child elements:
<body style="font-family:'Times New Roman'; font-size:12pt">
The behavior is expected and correct to reduce the output document size. There is no way to write default font in each <span> tag in output HTML produced by Aspose.Words. The default font is written once in the root <body> tag.
That is why as a workaround it was suggested to set the default font to some other value. In the code provided above the default font is set to 10pt and font of the whole document is set to 12pt. Since font set in each element differs from the default font, it is written into the output HTML explicitly in each element.