使用docx4j生成的.docx中心图像水平 [英] Center image horizontally inside .docx generated with docx4j

查看:626
本文介绍了使用docx4j生成的.docx中心图像水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过将图片水平放置在由 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!

推荐答案

使用 https://github.com/plutext/docx4j-ImportXHTML (我没有尝试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.

我注意到中心图片使用text-align center?但尚未尝试:

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

让它工作可能需要一个代码补丁。

Getting that to work might well require a code patch.

这篇关于使用docx4j生成的.docx中心图像水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆