docx4j XHTMLImporter игнорира   (интервал без прекъсване)

XHTMLImporter от docx4j не преобразува   в 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 изобщо няма интервали.


person Dave    schedule 18.11.2015    source източник


Отговори (2)


Не съм се заровил много в източниците на docx4j и просто се обаждам

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

За съжаление в word документа стана като обикновено пространство, но в моя случай не беше критично.

person Sergei Plevko    schedule 10.02.2016

Това работи!!

Escaped низ = unescaped.replace(" ", " ");

  ще бъде заменен от това   ще добави интервал

person Akitha_MJ    schedule 01.09.2018
comment
Екраниран низ = unescaped.replace(,  ); - person Akitha_MJ; 01.09.2018
comment
  ще бъде заменен от това   ще добави интервал - person Akitha_MJ; 01.09.2018