简体   繁体   English

HTML 在使用 docx4j XHTMLImporterImpl 后不保持格式

[英]HTML does not maintain formatting after using docx4j XHTMLImporterImpl

Im currently using XHTMLImporterImpl version 8.0 and docx4j 8.23我目前使用 XHTMLImporterImpl 8.0 版和 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,将其保存到文件并在浏览器中查看看起来不错,但使用转换后,我们失去了所有格式......有什么想法吗??

<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.然后我进行以下调用,但是当我查看保存后生成的 word 文档时,我看不到与浏览器中相同的格式。 I only see a box on the other div.我只在另一个 div 上看到一个框。 all divs shows as a new row..similar to the following所有 div 显示为新行..类似于以下内容

    -------------------------------------------------------------
    -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. 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.但是,docx4j 在解释 css styles 时有限制。

If you would use html tags instead of css styles to display the table, the conversion would work.如果您使用 html 标签而不是 css styles 来显示表格,则转换将起作用。 In that case, the html you would feed to the docx4j importer would have to look similar to this:在这种情况下,您将提供给 docx4j 导入器的 html 必须与此类似:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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