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