Page 1 of 1

databinding with richtext content

PostPosted: Wed Jun 21, 2017 6:21 pm
by hoeverma
I used the word helper addin to generate a document containing repeat, condition, and, I wanted also to include a richtext control. It seems that the result always is a plain text control, not a richtext control, cause a formatting applied to a part of the contained text (e.g. set it to bold), is applied to the whole contents of the control, not only the selected part. How can I produce a richtext control?

Re: databinding with richtext content

PostPosted: Wed Jun 21, 2017 8:58 pm
by jason
That's how content control data binding usually works.

You can bind rich text in 2 ways:

The first way is binding/import of escaped XHTML content; this import is triggered when the content control tag contains od:ContentType=application/xhtml+xml

When that's encountered, docx4j will take the bound XML value (which contains escaped XHTML) and use its ImportXHTML functionality to automatically convert it to standard WordML content.

Escaped XHTML looks something like:

<yourElement>&lt;span&gt;may &lt;b&gt;not&lt;/b&gt;&lt;/span&gt;</yourElement>

(since this is going inside a paragraph, it is wrapped in a span; if it was paragraph level, it'd be wrapped in a div)

See further https://github.com/plutext/docx4j/blob/ ... XHTML.docx

The second way is binding/import of escaped Flat OPC ie bind rich text content (tag contains 'od:progid=Word.Document'), though I think this is probably less popular; see further https://github.com/plutext/docx4j/blob/ ... ssing.docx

If your content is just an image, please see https://github.com/plutext/docx4j/blob/ ... mages.docx

Re: databinding with richtext content

PostPosted: Wed Jun 21, 2017 10:01 pm
by hoeverma
Ok, thank you Jason! I read through it, that's what I was looking for.
Am I right with the following:

1. By using the XHTML alternative, there is a significant danger of "layout/format loss" when converting the documents?
2. When using the Flat OPC way, I cannot use repeats and conditions from docx4j?

Re: databinding with richtext content

PostPosted: Thu Jun 22, 2017 8:54 pm
by jason
hoeverma wrote:By using the XHTML alternative, there is a significant danger of "layout/format loss" when converting the documents?


A lot but not all formatting is converted, and from experience with other users, there is a good chance it will be "good enough" for your purposes.

You can play with the XHTML Import (ie without needing to escape it / bind it etc); see for example https://github.com/plutext/docx4j-Impor ... gment.java

hoeverma wrote:When using the Flat OPC way, I cannot use repeats and conditions from docx4j?


Absolutely you can! Just nest the Flat OPC content control inside your repeat or condition content control(s).

At the risk of confusing, using XHTML or Flat OPC also offers the possibility of doing your own pre-processing (eg repeating content) to your data before you inject it as XML input to OpenDoPE. For example, feeding in a formatted table or list, rather than OpenDoPE repeating a table row or list item.

You have that flexibility if you need it, but I'd encourage you to use docx4j for repeats and conditions :-)