Page 1 of 1

convertAltChunks discarding AltChunk

PostPosted: Sun Jul 06, 2014 6:01 am
by roded
Hi,
Following some posts on this forum, I'm trying to insert XHTML into a DOCX.

I'm using the addAltChunk(ContentAccessor) method to add the XHTML to the part at a certain location. The AltCunck is wrapped by a table (and a P under the Tc) and then added to the MainDocumentPart.
I tired letting Word handle the chunk, but there seems to be an issue with it showing under a Tc (http://www.docx4java.org/forums/xhtml-import-f28/inserting-html-in-a-table-cell-t1152.html#p3965 - can anyone else confirm this?) while showing it under the document root works.

However, when using the alternative option of running convertAltChunks() in the part to convert the XHTML to the DOCX schema objects, the following error is issued:
2014-07-04 13:02:40,158 WARN org.docx4j.jaxb.JaxbValidationEventHandler: - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"altChunk"). Expected elem
2014-07-04 13:02:40,175 INFO org.docx4j.jaxb.JaxbValidationEventHandler: - continuing (with possible element/attribute loss)

And the altChunk element is consequently discarded.

The XHTML passes validation and is in fact the following:
<!DOCTYPE html><html><head><title>hmm</title></head><body>hello world</body></html>

Upgrading DOCX from 2.8.1 to 3.1 made no difference.
Using JDK 1.6.

Thought I'd ask here before perusing debugging the issue.
If anyone has any pointers, I'd really appreciate it.
Cheers,
Roded

Re: convertAltChunks discarding AltChunk

PostPosted: Sun Jul 06, 2014 9:17 pm
by jason
The message:

roded wrote: unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"altChunk").


is telling you you've added the altChunk in a location in which it is not valid (ie inside a w:p?)

You could try adding it directly to the tc (preceded or followed by a w:p, if that is necessary to make Word happy, or possibly in a content control - you'll have to see what is valid and what Word likes).

Alternatively/better, you can convert the XHTML to docx content following the examples at
https://github.com/plutext/docx4j-Impor ... 4j/samples
then insert the resulting content into the table cell.

Re: convertAltChunks discarding AltChunk

PostPosted: Sun Jul 06, 2014 9:24 pm
by roded
Aha!
Yes, that sounds like a better alternative.
Thanks Jason.