I attempt to insert HTML data into Rich Text Content Control into a table cell in my template (.docx).
Extract code :
- Code: Select all
AlternativeFormatInputPart afiPart = null;
afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); //CAUTION: each html part needs a new name!!
afiPart.setBinaryData(html.getBytes("UTF-8"));
afiPart.setContentType(new ContentType("text/html"));
Relationship altChunkRel = null;
altChunkRel = template.getMainDocumentPart().addTargetPart(afiPart);
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
Tc parent = (Tc)toReplace.getParent();
int subIndex = parent.getContent().indexOf(p);
parent.getContent().set(subIndex,ac);
OutputStream os = new java.io.FileOutputStream(outputPdfPath + ".pdf");
Docx4J.toPDF(wordMLPackage, os);
os.close();
The output is all right in .docx however it's failed in PDF generation with error below :
Exception executing transformer: org.apache.fop.fo.ValidationException: "fo:table-cell" is missing child elements. Required content model: marker* (%block;)+ (See position 111:608)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:215)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:158)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:139)
When I open the test.zip , there is a hw.html file .In the document.xml , the particular table cell is empty.
- Code: Select all
<w:t>AA Category</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="283" w:type="dxa"/>
</w:tcPr>
<w:p>
<w:pPr>
<w:jc w:val="center"/>
<w:rPr>
<w:b/>
<w:bCs/>
<w:sz w:val="16"/>
<w:szCs w:val="16"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:bCs/>
<w:sz w:val="16"/>
<w:szCs w:val="16"/>
</w:rPr>
<w:t>:</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="2268" w:type="dxa"/>
<w:vAlign w:val="center"/>
</w:tcPr>
<w:altChunk r:id="rId13"/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="1985" w:type="dxa"/>
</w:tcPr>
<w:p>
<w:pPr>
<w:rPr>
<w:b/>
<w:bCs/>
<w:sz w:val="16"/>
<w:szCs w:val="16"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:b/>
<w:bCs/>
<w:sz w:val="16"/>
<w:szCs w:val="16"/>
</w:rPr>
<w:t>Template Lending</w:t>
Isn't that the reason why pdf generation failed?
Any way to overcome this?
Or, I should uses other approach to replace HTML value in table cell.
Appreciate your input.
Thank you very much.