问题描述
我已经尝试过将图片水平放置在由 HTML 使用 docx4j 生成的.docx 文件中,但没有成功。到目前为止,我尝试过的(在 HTML 中工作)是:
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>
(通过居中外部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%;
}
我用来生成 .docx 可用,请参阅。感谢!
The code I'm using to generate the .docx is available here, guided from this question. Thanks!
推荐答案
使用(我没有尝试v3.2.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>";
或
String xhtml= "<div align=\"center\">" +
"<p><img src='" + PNG_IMAGE_DATA + "' /></p>" +
"</div>";
我没有尝试在图像本身放置align属性。
I didn't try putting the align attribute on the image itself.
我注意到但尚未尝试:
display: block;
margin-left: auto;
margin-right: auto;
让它工作可能需要一个代码补丁。
Getting that to work might well require a code patch.
这篇关于使用docx4j生成的.docx中心图像水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!