我正在尝试使用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> </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> </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)抛出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:
关于使用XHTMLImporterImpl使用相同的内容生成Doc两次并且样式不正确。
代码如下
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之外的任何应用程序中查看。
答案 0 :(得分:0)
关于问题1:addAltChunk只是添加了一个altChunk(又名AlternativeFormatInput部分),由于你只是保存了docx,你依靠Word来转换内容,所以你的行为是特定于Word的。
关于问题2:您正在获取重复内容,因为您同时执行XHTMLImporter.convert和addAltChunk。