我有簡單的HTML(temlate),我轉換成的docx與docxj4:分頁
<html>
<head>
<style type="text/css">
tr,
h2,
tnr {
font-family: Times New Roman;
font-size: 11pt;
}
h2 {
text-align: center;
}
.notesTable {
border: 4px double black;
border-collapse: collapse;
border: 1px solid black;
}
</style>
</head>
<body>
<table align="center" style="width: 75%; margin-left: -25%">
<tbody>
<tr style="height: 25px;font-family: 'Times New Roman';font-size: 16pt;">
<td>28.02.2016 sunday</td>
<td style="text-align: center; width: 30%;">test</td>
</tr>
</tbody>
</table>
<div>
<ol>
<li>ex1 </li>
<li>ex2</li>
</ol>
</div>
<p style="text-align: left;">
<span style="font-family:'Comic Sans MS';">
test
</span>
</p>
<p>
<h2>comments</h2> test
</p>
<p>
<h2>contacts</h2> test
</p>
<br style="page-break-after: always; clear:both;" />
<p>
</p>
</body>
</html>
問題是在管線
<br style="page-break-after: always; clear:both;" />
當它是如此,結果文檔文件沒有分頁符。當我把它改成
<br style="page-break-after: always; clear:both;">
分頁顯示,但我得到異常
org.xml.sax.SAXParseException; lineNumber:142; columnNumber:3;元素類型「br」必須由匹配的結束標記「」終止。
,所有款式均採用默認值。 請告訴我我做錯了什麼?
import org.docx4j.model.structure.PageSizePaper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class App {
public static void main(String[] args) throws Docx4JException, FileNotFoundException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.A4, false);
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
String xhtml = "<html>" +
"<head>" +
" <style type=\"text/css\">" +
" h2 {" +
" text-align: center;" +
" font-family: Times New Roman;" +
" font-size: 11 pt;" +
" }" +
" </style>" +
"</head>" +
"<body>" +
" <h2> Line on the first page</h2>" +
" <br style=\"page-break-after: always; clear:both;\" >" +
" <h2> Line on the second page</h2>" +
"</body>" +
"</html>";
mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes());
WordprocessingMLPackage pkgOut = mdp.convertAltChunks();
FileOutputStream stream1 = new FileOutputStream("test.doc");
pkgOut.save(stream1);
}
}
首先,您必須提供格式良好的XML! – JasonPlutext
賈森,我真的很感激你的反饋,但我不明白爲什麼我的HTML格式不正確。也許這是混亂。好的 - 我已經添加了SSCCE。問題是一樣的 - 當
是正確的,我沒有分頁符,當
沒有關閉我得到異常,默認樣式和分頁符。也許從html轉換爲doc的方式不正確? – Ivan
您的SAXParser需要XHTML - 因此所有br標籤都需要關閉(以及其他標籤)。看看像http://www.html-tidy.org/ –