简体   繁体   中英

HTML does not maintain formatting after using docx4j XHTMLImporterImpl

Im currently using XHTMLImporterImpl version 8.0 and docx4j 8.23

I have the current html, saving this to a file and viewing in a browser looks good but after using the conversion, we loose all formatting...any ideas??

<html>
<div>
    <div id="divHfBody" style="display:table;border-collapse: collapse;">
        <div id="divHfBody" style="display:table-row;">
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:5%;padding:3px;">CODE1</div>
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:80%;padding:3px;">DESC1</div>
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:10%;padding:3px;">1.234</div>
        </div>
        <div id="divHfBody" style="display:table-row;">
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:5%;padding:3px;">CODE2</div>
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:85%;padding:3px;">DESC 2</div>
            <div id="divHfBody" style="display:table-cell;border: 1px solid #000000;width:10%;padding:3px;">2.0</div>
        </div>
    </div>
</div>
</html>

I then make the following call, but when I look at the word document that is produced after the save, I do not see the same format as in a browser. I only see a box on the other div. all divs shows as a new row..similar to the following

    -------------------------------------------------------------
    -CODE1                                                      -
    -DESC1                                                      -
    -1.234                                                      -
    -CODE2                                                      -
    -DESC2                                                      -
    -2.0                                                        -
    -------------------------------------------------------------

This is my conversion code

XHTMLImporterImpl importer = new XHTMLImporterImpl(wordMLPackage);
List<Object> pHtml = importer.convert(divHtml, null);

Your html displays a table, because you use the css styles display:table , display:table-row and display:table-cell to tell the browser to render your html as a table. However, docx4j has limits when it comes to interpreting css styles.

If you would use html tags instead of css styles to display the table, the conversion would work. In that case, the html you would feed to the docx4j importer would have to look similar to this:

<div>
    <table id="divHfBody" style="border-collapse: collapse;">
        <tr id="divHfBody">
            <td id="divHfBody" style="border: 1px solid #000000;width:5%;padding:3px;">CODE1</td>
            <td id="divHfBody" style="border: 1px solid #000000;width:80%;padding:3px;">DESC1</td>
            <td id="divHfBody" style="border: 1px solid #000000;width:10%;padding:3px;">1.234</td>
        </tr>
        <tr id="divHfBody">
            <td id="divHfBody" style="border: 1px solid #000000;width:5%;padding:3px;">CODE2</td>
            <td id="divHfBody" style="border: 1px solid #000000;width:85%;padding:3px;">DESC 2</td>
            <td id="divHfBody" style="border: 1px solid #000000;width:10%;padding:3px;">2.0</td>
        </tr>
    </table>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM