docx到HTML:使用DOCX4J标题的字体样式不正确 [英] Docx to HTML: Headings' font style is not correct using DOCX4J

查看:197
本文介绍了docx到HTML:使用DOCX4J标题的字体样式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Calibri Light的Heading1样式的docx文件(其他所有文本也都使用Calibri Light).转换为html后,每个文本都是正确的Calibri Light,但是当我打开html文件时,带有Heading1样式的文本为Times New Roman.(原因:在HTML文件中没有为Heading1样式设置字体系列)

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)

当我打开docx文件并检查Heading1样式的字体时,它显示为Calibri Light.

When I open the docx file and check the Heading1 style's font, it says Calibri Light.

Heading1基于"Normal"标准.docx中的样式.

Heading1 is based on "Normal" style in docx.

这是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> 

我可以看到,我们有4种字体.但是我们能否告诉DOCX4J使用特定字体(例如,使用w:ascii并将该字体设置为html文件中的Heading1样式?)Heading1样式的文本也应该是Calibri Light,这是我的目标.

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.

这是Heading1样式:

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);

这是html中的2种样式

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;}

其他可接受的解决方案可以是:将字体系列设置为"DocDefaults".html文件中的样式.有可能吗?

Other acceptable solution can be this: set a font-family to "DocDefaults" style in html file. Is it possible?

推荐答案

字体家族是使用直接应用的CSS处理的.

Font-family is handled using directly applied CSS.

例如:

  <li class="Heading1 Normal DocDefaults " style="display: list-item;"><span class="DefaultParagraphFont " style="font-family: 'Calibri Light';">Chapter 2</span></li></ol>
  
  <p class="Normal DocDefaults "><span class="" style="font-family: 'Calibri Light';">This is an example part, chapter 2</span></p></div>

要应用字体家族,字体必须在计算机上物理存在,或映射到物理上存在的字体(有关示例,请参阅docx4j的ConvertOutHtml示例).

For font-family to be applied, the font must either be physically present on your machine, or mapped to a font which is physically present (see docx4j's ConvertOutHtml sample for an example).

如果不是这种情况,则不会发出font-family属性.

If this is not the case, no font-family property will be emitted.

要获得更深入的了解,可以为org.docx4j.fonts.RunFontSelector打开调试级别日志记录.日志记录示例:

For greater insight, you can turn debug level logging on for org.docx4j.fonts.RunFontSelector. Example logging:

08:54:21.295 [main] DEBUG org.docx4j.fonts.RunFontSelector 1136 - looking for: Calibri Light
08:54:21.295 [main] DEBUG org.docx4j.fonts.RunFontSelector 1144 - Font 'Calibri Light' maps to Calibri Light

这篇关于docx到HTML:使用DOCX4J标题的字体样式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆