I would like to produce docx from xhtml, including footer/header with page numbering. The input is:
String pXHTML += """
<html>
<head>
<style>
@page {
...
@top-center {
content: element(header)
}
@bottom-center {
content: element(footer)
}
}
div.header {
display : block;
text-align: center;
position : running(header);
}
div.footer {
display : block;
text-align: center;
position : running(footer);
}
.page-number::after {
content: counter(page) "/" counter(pages);
}
</style>
</head>
<body>
<div class="header">
this is the header
</div>
<div class="footer">
this is the footer with page numbers <p class="page-number"></p>
</div>
...
</body>
</html>
"""
The code that process it is as follows:
String xhtmlText = addDoctype(pXHTML);
WordprocessingMLPackage wordMLPackageXHTML = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackageXHTML);
wordMLPackageXHTML.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(xhtmlText, null));
Save saveToZip = new Save(wordMLPackageXHTML);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
saveToZip.save(byteArrayOutputStream);
The result page document has simply plain text
this is the header
this is the footer with page numbers
on the top of page.
How to convert it into proper header and footer, the latter with page numbering?
New contributor
user26566721 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.