Open mueller-jens opened 4 years ago
I had the same issue. Also when using an img-Tag.
Looking into the generated docx, I found that attribute space="preserve"
seems to be the reason. This attribute is added in XHTMLImporterImpl.java
.
I argue to remove this hardcoded "preserve" or make it configurable because whitespace in XML and HTML is ignored in most cases. If one really wants space in unusual places, one could use a non-breaking-space.
Anyway, my workaround is to remove the "preserve" arribute from the generated content:
private static void removeSpacePreserveRecursive(Object obj)
{
if (obj instanceof Text)
{
var text = (Text) obj;
if ("preserve".equals(text.getSpace()))
{
text.setSpace(null);
}
}
else if (obj instanceof ContentAccessor)
{
ContentAccessor contentAccessor = (ContentAccessor) obj;
for (Object child : contentAccessor.getContent())
{
removeSpacePreserveRecursive(child);
}
}
}
You can call this method, for example, on wordMLPackage.getMainDocumentPart().getJaxbElement().getBody()
.
I try to convert a html file to docx using the library. If i try it every blank in the tempate will be converted in a blank in the dockument. I used a template like
And the result looks like:
expected: