Page 1 of 1

appending HTML/XHTML content to existing docx

PostPosted: Mon Jun 03, 2013 11:06 pm
by sreejiths_123
Hi ,

I have a docx template , from which i generates the needed docx by way of content binding .
Now I also prefer to add some xhtml contents to the resultant docx on the run .

Which is the prefered and best way to accomplish this goal .

i found one possible solution is to use the method as described ,

Code: Select all
wordMLPackage.getMainDocumentPart().getContent().addAll(
XHTMLImporter.convert( new File(inputfilepath + ".html"), null, wordMLPackage) );

but getting the below error ,
WARNING:: Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 3; The element type "meta" must be terminated by the matching end-tag "</meta>"


after i tried , using the below method ,

Code: Select all
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
  MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
  mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes());

  // Round trip
  wordMLPackage = mdp.convertAltChunks();


This seems to be working by appending html to the resultant docx, but again it throws the the above error

Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 3; The element type "meta" must be terminated by the matching end-tag "</meta>".


I hope , my xhtml is not well formed , and i needs some formatting with Jtidy .
Also the the styles in xhtml in resultant docx are not applied correctly ,
Can you tell where to find the new xhtmlrenderer.jar with all resources includes as described in ,
http://www.docx4java.org/forums/xhtml-import-f28/problems-while-converting-html-entities-t1197.html#p4173



, after i found this post ,
http://www.docx4java.org/forums/data-binding-java-f16/inject-converted-html-to-custom-xml-t1332.html#p4690

which says it is possible to bind xhtml via opendope . Can you also please tell me the steps invloved in binding xhtml data via opendope extension/word-addin

Re: appending HTML/XHTML content to existing docx

PostPosted: Mon Jun 03, 2013 11:53 pm
by jason
Whichever method you choose, the input needs to be well-formed XML. So yes, use JTidy or one of the alternatives to ensure that.

If you are using the newer OpenDoPE add-in (referenced in recent forum posts), you can include suitably escaped XHTML in an XML node in your XML part, and when you drag it to the document surface, it will be recognised as XHTML and handled appropriately.

An example of an XML node containing suitably escaped XHTML:

Code: Select all
<yourxml>&lt;div&gt; etc etc &lt;/div&gt; </yourxml>

Re: appending HTML/XHTML content to existing docx

PostPosted: Wed Jun 05, 2013 3:36 pm
by sreejiths_123
Hi Jaison ,

Thanks for reply .
I am using the OpenDoPE add-in which i downloaded from below link,
http://www.opendope.org/downloads/authoring-advanced/setup.exe

1 . Am i using the latest one ? . if not please provide me the link to download the new one .

2 . by XML node , hope you are pointing to the internal XML structure to which docx place holders are binded by OpenDoPE .
in that case , if i managed to add escaped XHTML like
Code: Select all
<yourxml>&lt;div&gt; etc etc &lt;/div&gt; </yourxml>

why i need drag it to the document surface ? . Do you have any detailed document which describes this process ?

3 . I have a sample html content (attached mapping.zip) , which i needs to merge at the end of docx dynamically after applying jtidy . It has tables , whose styles are loosing after the merging process .it extends too long .is it possible to manage font and size of table after merging . Please have a look and tell me a possible solution to retain the style .

Re: appending HTML/XHTML content to existing docx

PostPosted: Wed Jun 05, 2013 4:09 pm
by jason
Use the newer add-in, available from http://www.opendope.org/downloads/autho ... /setup.exe

Uninstall the old one first (and restart Word etc).

"Dragging to the document surface" signals to the add-in that you want a content control inserted at this location, bound to the XPath given by the location of the node being dragged.

Re: appending HTML/XHTML content to existing docx

PostPosted: Wed Jun 05, 2013 5:01 pm
by sreejiths_123
Oh , that drag an drop is a wonderful feature provided in new OpenDoPE add-in , it will really minimizes my time to bind controls .
Thanks Jaison for showing the correct direction .
It will be much helpful , if you could find , what is the problem in style with above attached mapping.html after binding.