I'm trying to convert HTML into docx using docx4j
. I set cellspacing
and cellpadding
attributes of table to 0. But somehow after I opened the generated document, there's still cellspacing between each cell. I used XHTMLImporterImpl
to generate document. The following is the code I used:
String border = "border: 1px solid black;";
String testString = "<table cellspacing='0' cellpadding='0' style='" + border + "'>"
+ "<tr>"
+ "<td style='border-spacing:0;padding:0;background-color:red;" + border + "'>This is test row1 col1</td>"
+ "<td style='border-spacing:0;padding:0;background-color:red;" + border + "'>This is test row1 col2</td>"
+ "</tr>"
+ "<tr>"
+ "<td style='border-spacing:0;padding:0;background-color:red;" + border + "'>This is row2 col1</td>"
+ "<td style='border-spacing:0;padding:0;background-color:red;'>This is row2 col2</td>"
+ "</tr>"
+ "</table>";
generateWord(testString);
generateWord()
method as follows:
public static void generateWord(String html) throws Docx4JException
{
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert( html, null) );
wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/output.docx"));
}
Result as below:
There're whitespaces in between each cell.
Sidenote: If I used CTAltChunk
, there's no spacing between each cell. But CTAltChunk
cannot handle Base64
image. I can use <img src=''>
with CTAltChunk
but it'll link the image source from the document. Once the source image is removed, the image will be gone from the document. That's why I need to use Base64
image.