Center image horizontally inside .docx generated with docx4j

让人想犯罪 __ 提交于 2019-12-13 21:57:42

问题


I've been trying for some days to center an image horizontally inside a .docx file generated from HTML with docx4j without success. What I've tried so far (and works in HTML) is:

<div align="center"><img alt="" src="data:image/png;base64,/9j/4AAQSkZJ..."></div>

(by centering outer div)

<div><img alt="" src="data:image/png;base64,/9j/4AAQSkZJ..." clase="img-default"></div>

with the following commands inside Style tag:

.img-default {
    margin: 0 auto;
    display: block;
    height: auto;
    max-width: 100%;
}

The code I'm using to generate the .docx is available here, guided from this question. Thanks!


回答1:


Testing with code at https://github.com/plutext/docx4j-ImportXHTML (I didn't try v3.2.1), the following works for me:

    String xhtml= "<div align=\"center\">" +
                "<img src='" + PNG_IMAGE_DATA + "'  />" +
            "</div>"; 

or

    String xhtml= "<div align=\"center\">" +
                "<p><img src='" + PNG_IMAGE_DATA + "'  /></p>" +
            "</div>"; 

I didn't try putting the align attribute on the image itself.

I note Center image using text-align center? but haven't tried:

display: block;
margin-left: auto;
margin-right: auto;

Getting that to work might well require a code patch.



来源:https://stackoverflow.com/questions/27228607/center-image-horizontally-inside-docx-generated-with-docx4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!