Page 1 of 1

Add image inside table cell

PostPosted: Fri Nov 18, 2016 5:36 am
by Chandra123
Hi,

I was trying to add sub htmls as altchunks. My requirement is i have to parse html and if any img tag with base64 data is found, i have to add image inline using docx4j API. other data i am adding as alt chunk. in this process if img tag is inside <td>. till td i use addAltchunk and then addimag inline. In this case table is closed by call to addAltchunk. My image is coming out of table.
I tried below sample. i see the same behaviur. attached word doc produced as well. The content "kk" should come inside table. but it is coming out side the table.
Code: Select all
      WordprocessingMLPackage wordMLPackage=null;
       wordMLPackage=WordprocessingMLPackage.createPackage();
       wordMLPackage.getMainDocumentPart().addAltChunk(
               org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType.Html,
               "<html><table style=\"border:1px solid black;\"><tr><td><p>hi</p></td></tr><tr><td></html>".getBytes());
      
       wordMLPackage.getMainDocumentPart().addAltChunk(
               org.docx4j.openpackaging.parts.WordprocessingML.AltChunkType.Html,
               "<html>kk</td></tr></table></html>".toString().getBytes());
      
       FileOutputStream out = new FileOutputStream("test1.docx");
       wordMLPackage.save(out);
      

looks like addaltchunk is closing table.
We could not use XHTMImporter API for this requirement of converting base64 images inline. We need to parse html and replace all img tags with img inline.For which above approach tried that do not give expected results. Pls suggest.

Re: Add image inside table cell

PostPosted: Fri Nov 18, 2016 5:38 am
by Chandra123
Attched output file

Re: Add image inside table cell

PostPosted: Fri Nov 18, 2016 6:04 pm
by jason
A few points/questions:

1. Why couldn't you use XHTMImporter to process base64 image data?

2. altChunk processing is done by Word, unless you ask docx4j to do it (in which case for html content, it needs to be well formed xml)

3. following on from point 2, you can't "open" elements in 1 altChunk, then "close" them in another!

Re: Add image inside table cell

PostPosted: Sun Nov 20, 2016 6:39 am
by Chandra123
Hi Jason.
Thanks for the response.

We could not use XHMTLImporter as we need to have seperate jar for XHMLImporter and subsequent licencing etc.
for point 2 & 3
as per your comments if my altchunks are not well formed and while it gets processed by word, word is closing table tag?

if i need to replace each img tag with inline image. the approach i choose is above. Other than XHTMLImporter is there any other way around. The reason for making image inline is word is not supporting img tag with base 64 encoding data as src value.