问题
I have a docx file with Heading1 style with Calibri Light (every other texts use Calibri Light too). After converting to html, Every texts are Calibri Light (correctly), but the text with Heading1 style is Times New Roman when I open the html file. (The reason: there is no font-family set for Heading1 style, inside of html file)
When I open the docx file and check the Heading1 style's font, it says Calibri Light.
Heading1 is based on "Normal" style in docx.
This is the Normal style in docx:
<w:style w:type="paragraph"
w:default="1"
w:styleId="Normal">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:rsid w:val="003D736F"/>
<w:pPr>
<w:spacing w:before="40"
w:after="40"
w:line="240"
w:lineRule="auto"/>
<w:ind w:left="851"/>
<w:jc w:val="both"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Calibri Light"
w:eastAsia="SimSun"
w:hAnsi="Calibri Light"
w:cs="Times New Roman"/>
<w:szCs w:val="20"/>
<w:shd w:val="clear"
w:color="auto"
w:fill="FFFFFF"/>
<w:lang w:eastAsia="zh-CN"/>
</w:rPr>
</w:style>
I can see, we have 4 fonts. But can we tell DOCX4J, to use a specific font (like, use w:ascii and put this font to the Heading1 style in html file?) Heading1 styled texts should be Calibri Light too, this is my goal.
And this is the Heading1 style:
<w:style w:type="paragraph"
w:styleId="Heading1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Normal"/>
<w:link w:val="Heading1Char"/>
<w:qFormat/>
<w:rsid w:val="00232342"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:numPr>
<w:numId w:val="4"/>
</w:numPr>
<w:spacing w:before="360"
w:after="240"/>
<w:jc w:val="left"/>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:sz w:val="32"/>
</w:rPr>
</w:style>
This is the code I am using:
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(source));
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setWmlPackage(wordMLPackage);
htmlSettings.setImageDirPath("temp_images");
htmlSettings.setImageTargetUri("temp_images");
htmlSettings.setImageIncludeUUID(false);
boolean nestLists = false;
if (nestLists) {
SdtWriter.registerTagHandler("HTML_ELEMENT", new SdtToListSdtTagHandler());
} else {
// convert numberings to plain text
htmlSettings.getFeatures().remove(ConversionFeatures.PP_HTML_COLLECT_LISTS);
}
OutputStream os = new java.io.FileOutputStream(dest);
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
And this is the 2 styles in html
.Normal {display:block;text-align: justify;position: relative; margin-left: 15mm;margin-top: 1mm;margin-bottom: 1mm;line-height: 100%;}
.Heading1 {display:block;text-align: left;page-break-after: avoid;margin-top: 0.25in;margin-bottom: 4mm;font-weight: bold;font-size: 16.0pt;}
Edit: Other acceptable solution can be this: set a font-family to "DocDefaults" style in html file. Is it possible?
来源:https://stackoverflow.com/questions/65251150/docx-to-html-headings-font-style-is-not-correct-using-docx4j