Page 1 of 1

Content control loses style formatting

PostPosted: Thu Oct 21, 2010 3:56 am
by dcole
Jason,

I saw some posts on here, as well as the Word Content control forum that you can set up a content control in a document, giving the enclosed text a style, and it gets lost when the document is bound to custom XML.

I saw this page: http://social.msdn.microsoft.com/forums ... 6490d958c/

But never saw a fix posted or anything.

Do you know if there was a workaround for this so that your custom documents will fit the same style as the rest of the document?

Re: Content control loses style formatting

PostPosted: Thu Oct 21, 2010 10:55 pm
by djohnson
Indeed I have the same problem. When I use a content control in a sentence that is bold, the text of the content control is shown in plain (not bold) after the processing by docx4j (linking data via custom XML).
Strangely this does not happen when the content control is used in a table and it's the only thing in a cell.

Re: Content control loses style formatting

PostPosted: Thu Oct 21, 2010 11:00 pm
by jason
According to s2.5.2.6 of ECMA 376, as found at [1],

If the parent structured document tag is of type rich text or document part gallery, then [the data binding] property shall be ignored.

But with Word 2007, it actually isn't necessary. Word 2007 does in fact honour the binding on a rich text content control (contrary to the spec) - whether its one with an explicit w:richText element, or one with no type (which 2.5.2.25 says amounts to the same thing).

What this means is that if you do set up a rich text content control binding using a tool other than Word (or my Word Add-In), then when you open the docx in Word, Word will honour the binding (albeit with loss of formatting - I think it converts it to a plain text control). So this is the source of the confusion.

What you can't do within Word 2007 though - via VBA or VSTO - is invoke the SetMapping method on a rich text control - as documented on MSDN. So my Word Add-In can't bind a rich text control; if its to be bound, it needs to be a plain text control. In a later version, the Add-In will prompt you regarding this.

There are at least 2 work arounds to get the effect you want. In both work arounds you bind a plain text control.

Workaround 1: wrap the control with run properties which do bold or whatever.

Workaround 2: wrap the control in a rich text control with bold or whatever.

cheers .. Jason

[1] http://www.documentinteropinitiative.or ... 8ebf3.aspx

Re: Content control loses style formatting

PostPosted: Thu Oct 21, 2010 11:02 pm
by jason
djohnson wrote:Strangely this does not happen when the content control is used in a table and it's the only thing in a cell.


Most likely because the bold is coming from a formatting property which applies to the cell contents, including the content control?

Re: Content control loses style formatting

PostPosted: Fri Oct 22, 2010 1:21 am
by dcole
jason wrote:According to s2.5.2.6 of ECMA 376, as found at [1],

If the parent structured document tag is of type rich text or document part gallery, then [the data binding] property shall be ignored.

But with Word 2007, it actually isn't necessary. Word 2007 does in fact honour the binding on a rich text content control (contrary to the spec) - whether its one with an explicit w:richText element, or one with no type (which 2.5.2.25 says amounts to the same thing).

What this means is that if you do set up a rich text content control binding using a tool other than Word (or my Word Add-In), then when you open the docx in Word, Word will honour the binding (albeit with loss of formatting - I think it converts it to a plain text control). So this is the source of the confusion.

What you can't do within Word 2007 though - via VBA or VSTO - is invoke the SetMapping method on a rich text control - as documented on MSDN. So my Word Add-In can't bind a rich text control; if its to be bound, it needs to be a plain text control. In a later version, the Add-In will prompt you regarding this.

There are at least 2 work arounds to get the effect you want. In both work arounds you bind a plain text control.

Workaround 1: wrap the control with run properties which do bold or whatever.

Workaround 2: wrap the control in a rich text control with bold or whatever.

cheers .. Jason

[1] http://www.documentinteropinitiative.or ... 8ebf3.aspx


Can you explain the steps for those workarounds? Specifically the first one. I read that somewhere else, but couldnt get it working. I started out iwth an empy word document, set my font to bold, wrote a sentence. I moved my cursor to the middle of the sentence and put in a plain text control, did the binding, etc etc..and I ended up with my plain text content font not being bold, and the text on either side of it was bold.

Like so:

This is a (my content) sentence that is bold

Re: Content control loses style formatting

PostPosted: Fri Oct 22, 2010 5:09 am
by dcole
Also - how recent did this change? The first time I ever tried docx4j to do a repeat template for a document I have, the font stuff worked fine. I just havent gotten a single document to work correctly with the new version

Re: Content control loses style formatting

PostPosted: Sat Nov 06, 2010 8:51 am
by dcole
Jason,

Can you post bychance an example of binding a content control to some data that retains the font styling? I have tried everything listed here AND more and I cannot get it to maintian "Arial" font size 18 in a content control for example. When I do a custom xml injection using docx4j, does it simply replace the XML file with my new XML document that I am providing, or do other things get modified? I'd imagine they must get modified if I am using repeat bindings, but I can't even get this to work if I am just using a simple binding with a simple XML file.

Any tips or help is much appreciated..i am trying to auto-gen a bunch of resumes, and it is not going to be much good for me if they all come out with the same font size, style and emphasis. (Calibri size 11, in my case)

Derek

Re: Content control loses style formatting

PostPosted: Tue Nov 09, 2010 10:04 am
by dcole
bump....any thoughts on this issue?

Re: Content control loses style formatting

PostPosted: Tue Nov 09, 2010 10:17 pm
by jason
Hi Derek

Pls try latest svn.

http://dev.plutext.org/trac/docx4j/changeset/1291

cheers .. Jason

Re: Content control loses style formatting

PostPosted: Wed Nov 10, 2010 9:08 am
by dcole
Holy crap! This finally works as expected it seems! this is good news! Thanks Jason!

Re: Content control loses style formatting

PostPosted: Wed Nov 10, 2010 2:01 pm
by jason
No worries.

When you get a moment, could you please update your post at http://www.aspose.com/community/forums/ ... hread.aspx to reflect that this now works?