Page 1 of 1

Set text style when no run exists in paragraph yet?

PostPosted: Fri Dec 14, 2012 8:43 am
by garyg
Hi,

I have a docx "template" with one table that has several columns in one row. Using Word the table is setup and the default font is overriden to Courier New (or any font really...the user is in control and the code will never know ahead of time). No text is typed into any column. The docx is saved (all with Word app, no code yet).

This is what the w:tc looks like at this point:

Code: Select all
<w:tc><w:tcPr><w:tcW w:w="2610" w:type="dxa"/></w:tcPr><w:p w:rsidR="001C6C68" w:rsidRPr="001C6C68" w:rsidRDefault="001C6C68" w:rsidP="001C6C68"><w:pPr><w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New" w:cs="Courier New"/></w:rPr></w:pPr></w:p></w:tc>


There is no existing run for me to work with as no text was typed, just a font was set.

Then using dox4j I open the docx and add text to one of the columns. After that, the w:tc looks like this:

Code: Select all
<w:tc><w:tcPr><w:tcW w:type="dxa" w:w="2610"/></w:tcPr><w:p w:rsidRDefault="001C6C68" w:rsidP="001C6C68" w:rsidR="001C6C68" w:rsidRPr="001C6C68"><w:pPr><w:rPr><w:rFonts w:cs="Courier New" w:hAnsi="Courier New" w:ascii="Courier New"/></w:rPr></w:pPr><w:r><w:t>TEST ADD DATA TO COLUMN 2</w:t></w:r></w:p></w:tc>


The run that I add to the pargraph is being added after the closing w:pPr, so my font settings get ignored and the default docx font takes over.

I found that I can set the font by doing this:

Code: Select all
runRpr.setRFonts(paragraph.getPPr().getRPr().getRFonts());


Where runRpr is a new RPr and paragraph is an instance of the P I found in the Tc.

Will I have to do that for everything about the style of the P? Is there any easy way to inherit the entire style of the P when I don't know it ahead of time?

Thanks.

Re: Set text style when no run exists in paragraph yet?

PostPosted: Fri Dec 14, 2012 12:11 pm
by jason
Run properties at the paragraph level are never actually applied to text; they are only there so Word can use them as the default for the next paragraph when the user hits enter (!).

So, yes, if you want to use those run properties in a particular run, you have to copy them into that run.

Re: Set text style when no run exists in paragraph yet?

PostPosted: Fri Dec 14, 2012 11:40 pm
by garyg
What I found with the word application is that it takes the font (and whatever other styles) set at the paragraph level (if any) and applies them to the Run w:r of anything you type.

This is the same starting w:p block from my "template" after I typed directly into the cell, no enter key used.

Code: Select all
<w:p w:rsidR="001C6C68" w:rsidRPr="001C6C68" w:rsidRDefault="00947BD2" w:rsidP="001C6C68"><w:pPr><w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New" w:cs="Courier New"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New" w:cs="Courier New"/></w:rPr><w:t>I TYPED THIS NO ENTER KEY</w:t></w:r></w:p>


So the behavior of word any time a new R is added to an existing P that has styling set is to apply the P styling to the new R.

I guess I was thinking docx4j mimics that kind of behavior stuff, but in the end, it was not horrible to mimick that and test if there are P level styles and apply them to any new R I set.

Don't get me wrong, docx4j has been a lifesavor. It's just not quite clear in all the docs what it does and does not bring to the table in terms of mimicing Word behavior.

Thanks!