Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docx4j XHTMLImporter ignores &nbsp (non-breaking space)

Tags:

xhtml

docx

docx4j

The XHTMLImporter from docx4j is not converting &nbsp into MS WORD non-breaking spaces.

Following code is used:

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

}

When the XHTML input is like:

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

then the docx output is like:

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

When opening the document in Word 2013 then there are no spaces at all.

like image 833
Dave Avatar asked Nov 09 '22 01:11

Dave


1 Answers

I haven't dig too deep in docx4j sources and just call

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

Unfortunately in the word document it became as usual space, but it wasn't critical in my case.

like image 127
Sergei Plevko Avatar answered Dec 28 '22 08:12

Sergei Plevko