Page 1 of 1

How to add text to existing paragraph

PostPosted: Sat May 19, 2012 10:57 pm
by Falkenstein
Hi, I'm new to docx4j and I need help with a problem - probably a basic and simple thing, but I cannot get it even after a day of searching.
The problem is as follows:
I create a paragraph using the .createStyledParapgraphOfText() method of the MainDocumentPart class with a predefined style and text (different every time, depends on a String variable) as arguments. Then I need to append more text to the paragraph. The reason for doing it this way is that the appended text must be in bold. After that I need to append more text that is not bold etc.
Thanks in advance :)

Re: How to add text to existing paragraph

PostPosted: Sun May 20, 2012 12:06 am
by jason
createStyledParagraphOfText returns the p it created.

Add content to it with p.getContent().add(blagh) where blagh is a run object with bold rPr containing a text object.

Re: How to add text to existing paragraph

PostPosted: Sun May 20, 2012 6:34 am
by Falkenstein
Thanks for your help, it works. :)
I suppose the key to solving my problem was using the R class. The only drawback is that the new next ignores spaces, i.e. I have string "blagh " and add string "flash" to it, the result is "blaghflash". A non-perfect solution I came up with is to add a white-colored character between the words.

Re: How to add text to existing paragraph

PostPosted: Sun May 20, 2012 8:18 am
by jason
Imaginative, but unnecessary.

On your org.docx4j.wml.Text t, you want t.setSpace("preserve");

In general, create a docx in Word containing the structure you want to replicate, then save/unzip to see how it is done. Generally speaking, if you can do it in Word, you should be able to do it the same way in docx4j.