使用 doc4j 将 html 转换为 doc 时无法调整图像大小

Unable to resize the image while converting html to doc using doc4j

我正在尝试使用 docx4j 3.3.1 从 html 生成文档。我面临以下问题,有人可以帮我解决这些问题吗?

HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" lang="en">
<head>
<title>MonthlyReport</title>
<style>
                    table
                    {
                    border:double #000;
                    table-layout: fixed;
                    vertical-align: top;
                    border-collapse: collapse;
                    width: 7.25in;
                    
                    }
                    .main-title
                    {
                    margin-top:25%;
                    font-size:48pt;
                    font-family:Century Gothic;
                    color:#2F5897;
                    text-align:center;
                    font-weight:300;
                    }
                    .year
                    {
                    padding-top:10%;
                    text-align:center;
                    font-size:18px;
                    
                    }
                    .other-detail
                    {
                    padding-top:10%;
                    padding-bottom:10%;
                    font-weight:bold;
                    text-align:center;
                    font-size:18px;
                    }
                    .cont
                    {
                    font-family:Palatino Linotype;
                    margin:0 auto;
                    margin-top:10px;
                    }
                    .cont-table
                    {
                    border:double #000;
                    table-layout: fixed;
                    vertical-align: top;
                    border-collapse: collapse;
                    width: 7.25in;
                    }
                    .cont h1
                    {
                    font-family:Century Gothic;
                    color:#2F5897;
                    font-weight:400;
                    margin-left:.15in;
                    }
                    .cont h4
                    {
                    font-family:Century Gothic;
                    font-weight:400;
                    margin-left:.15in;
                    }
                    .cont .school-detail
                    {
                    margin-left:.1in;
                    margin-right:.1in;
                    }
                    .cont ol
                    {
                    margin-left:1in;
                    margin-right:1in;
                    }
                    .cont li
                    {
                    margin-bottom:.25in;
                    }
                    .cont-heading
                    {
                    margin-left:.5in;
                    font-weight:bold;
                    text-decoration:underline;
                    margin-top:.35in;
                    
                    }
                    span
                    {
                    display:block;
                    padding-bottom:10px;
                    }
                    .rheight
                    {
                    height: 3.6in;
                    }
                    img
                    {
                    width:100%;
                    }
                    .img-table
                    {
                        width:5in;
                    }
                    .img-table td
                    {
                        width:5in;
                    }
                    .cont-img
                    {
                    width:4in;
                    }
                </style>
</head>
<body>
<table align="center" cellspacing="0" cellpadding="0">
<tr class="rheight">
<td> &nbsp; </td>
</tr>
<tr>
<td>
<div class="main-title"> Monthly report </div>
</td>
</tr>
<tr>
<td>
<div class="year"> 2016-2017 </div>
</td>
</tr>
<tr>
<td>
<div class="other-detail">MONTH: January<br />
            CITY: TEST_CITY<br /> FACILITATOR:
            TEST_PERSON<br />
</div>
</td>
</tr>
<tr class="rheight">
<td> &nbsp; </td>
</tr>
</table>
<table class="cont-table" align="center" cellspacing="0" cellpadding="0">
<tr>
<td>
<div class="cont">
<h1>Monthly Report</h1>
<h4>2016-2017</h4>
<br />
<p class="school-detail">
<p class="cont-heading">TEST SCHOOL</p>
<ol>
<li>
<strong>Test Question</strong>
<br />TEST_ANSWER</li>
</ol>
<p>Photos</p>
<table class="img-table" cellspacing="0" cellpadding="0">
<tr>
<td>
<img class="cont-img" src="C:\CodeBase\SampleProjects\fmstest\fms\TESTIMAGE.jpg" alt="TESTIMAGE.jpg" />
</td>
</tr>
</table>
</p>
</div>
</td>
</tr>
</table>
</body>
</html>

问题 1: 在将没有任何图像的 html 转换为文档时,我得到了正确的文档样式,但是如果我在 html 中使用图像,样式就会完全改变。 代码如下

`private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException { //文字处理包 WordprocessingMLPackage wordMLPackage = getWordMLPackage(); NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); wordMLPackage.getMainDocumentPart().addTargetPart(ndp); ndp.unmarshalDefaultNumbering();

//Saving the Document
wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes());
wordMLPackage.save(file);

}`

问题 2: On Using XHTMLImporterImpl 文档生成两次,内容相同,样式不正确。 代码如下 private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException { //Word Processing Package WordprocessingMLPackage wordMLPackage = getWordMLPackage(); NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); wordMLPackage.getMainDocumentPart().addTargetPart(ndp); ndp.unmarshalDefaultNumbering(); // Convert the XHTML, and add it into the empty docx we made XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); XHTMLImporter.setHyperlinkStyle("Hyperlink"); String baseurl = file.getPath(); baseurl = baseurl.substring(0, baseurl.lastIndexOf("\")); wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(html, baseurl)); //Saving the Document wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, html.getBytes()); wordMLPackage.save(file); }

问题 3:
生成的文档无法在除 MS Word 以外的任何应用程序中查看。

关于问题 1:addAltChunk 只是添加了一个 altChunk(又名 AlternativeFormatInput 部分),并且由于您只是保存了 docx,所以您依赖 Word 来转换内容,因此您的行为是特定于 Word 的。

关于问题 2:您得到了重复的内容,因为您同时执行了 XHTMLImporter.convert 和 addAltChunk。