【问题标题】:docx4j XHTMLImporter ignores &nbsp (non-breaking space)docx4j XHTMLImporter 忽略 &nbsp(不间断空格)
【发布时间】:2016-02-20 04:35:33
【问题描述】:

来自 docx4j 的 XHTMLImporter 不会将 &nbsp 转换为 MS WORD 不间断空格。

使用以下代码:

public void convert() throws Exception {



    String stringFromFile = FileUtils.readFileToString(new File("tmp.xhtml"), "UTF-8");

    String unescaped = stringFromFile;
    System.out.println("Unescaped: " + unescaped);


    // Setup font mapping
    RFonts rfonts = Context.getWmlObjectFactory().createRFonts();
    rfonts.setAscii("Century Gothic");
    XHTMLImporterImpl.addFontMapping("Century Gothic", rfonts);

    // Create an empty docx package
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

    NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
    wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
    ndp.unmarshalDefaultNumbering();

    // Convert the XHTML, and add it into the empty docx we made
    XHTMLImporter XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    XHTMLImporter.setHyperlinkStyle("Hyperlink");

    wordMLPackage.getMainDocumentPart().getContent().addAll(
            XHTMLImporter.convert(unescaped, null) );

    System.out.println(
            XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true));

    wordMLPackage.save(new java.io.File("OUT_from_XHTML.docx") );

}

当 XHTML 输入是这样的:

<p style="LINE-HEIGHT: 120%; MARGIN: 0in 0in 0pt"
class="MsoNormal"><span
style="LINE-HEIGHT: 120%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">Up
to Age 30<span
style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
2.30<span
style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
3.30</span></p>

那么 docx 输出是这样的:

<w:r>
                <w:rPr>
                    <w:rFonts w:ascii="Courier New"/>
                    <w:b w:val="false"/>
                    <w:i w:val="false"/>
                    <w:color w:val="000000"/>
                    <w:sz w:val="20"/>
                </w:rPr>
                <w:t>
2.30</w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:rFonts w:ascii="Courier New"/>
                    <w:b w:val="false"/>
                    <w:i w:val="false"/>
                    <w:color w:val="000000"/>
                    <w:sz w:val="20"/>
                </w:rPr>
                <w:t>
3.30</w:t>
            </w:r>

在 Word 2013 中打开文档时,根本没有空格。

【问题讨论】:

    标签: xhtml docx docx4j


    【解决方案1】:

    我没有深入挖掘 docx4j 源代码,只是调用

    String escaped = unescaped.replace("&nbsp;", "\u00A0");
    

    不幸的是,在 word 文档中它变成了正常的空格,但在我的情况下它并不重要。

    【讨论】:

      【解决方案2】:

      这行得通!

      字符串转义 = unescaped.replace(" ", "\u00A0");

      &nbsp 将被替换为 \u00A0 它会添加一个空格

      【讨论】:

      • 字符串转义 = unescaped.replace(" ", "\u00A0");
      • &nbsp 将被替换为 \u00A0 它将添加一个空格
      猜你喜欢
      • 2022-01-14
      • 2010-12-06
      • 2021-06-18
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2016-07-09
      相关资源
      最近更新 更多