Page 1 of 1

How to set Bold to Text

PostPosted: Tue Oct 04, 2016 7:21 pm
by Sindhu
Bold is Working perfectly fine using the following code

org.docx4j.wml.RPr rpr = factory.createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
rpr.setB(b);
....
But my issue is , i am going set the individual Text objects to Single org.docx4j.wml.R . so here the bold style is applying entire text in R.
Here i want some text is bold and some text is normal ,
as
Name : xyz
Address : xyzstate
is there any possibility to set style to org.docx4j.wml.Text rather than org.docx4j.wml.R.
or any other possibility.

thanx in advance.

Re: How to set Bold to Text

PostPosted: Wed Oct 05, 2016 9:05 pm
by jason
Run properties (eg bold) are set at the run level, so you'll need to use multiple runs.

Is there any reason that would be a problem for you?

Re: How to set Bold to Text

PostPosted: Wed Oct 05, 2016 9:13 pm
by Sindhu
Can we set multiple runs to single Run property.
can you please give some clarification on that.

Re: How to set Bold to Text

PostPosted: Wed Oct 05, 2016 10:04 pm
by jason
I'm not sure I understand the question.

Please illustrate with Java code or WordML.

Re: How to set Bold to Text

PostPosted: Wed Oct 05, 2016 10:18 pm
by Sindhu
Here is the Code...
Text t = factory.createText();
t.setValue("Name: ");
((R) obj).getContent().add(t);
t1.setValue("something");
Text t1 = factory.createText();
((R) obj).getContent().add(t1);

Here it gives output as..
Name: something

when applying bold to R as
org.docx4j.wml.RPr rpr = factory.createRPr();
org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
b.setVal(true);
rpr.setB(b);
((R) obj).getContent().add(run);

here the output is getting like
Name: something

but i want
Name: something

Re: How to set Bold to Text

PostPosted: Thu Oct 06, 2016 9:43 am
by jason
Assuming P p:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
            // Create object for first run
            R r = wmlObjectFactory.createR();
            p.getContent().add( r);

                // Create object for rPr
                RPr rpr = wmlObjectFactory.createRPr();
                r.setRPr(rpr);
                    // Create object for b
                    BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue();
                    rpr.setB(booleandefaulttrue);

                // Create object for t (wrapped in JAXBElement)
                Text text = wmlObjectFactory.createText();
                JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text);
                r.getContent().add( textWrapped);
                    text.setValue( "Name:");

            // Create object for second run
            R r2 = wmlObjectFactory.createR();
            p.getContent().add( r2);

                // Create object for rPr
                RPr rpr2 = wmlObjectFactory.createRPr();
                r2.setRPr(rpr2);

                // Create object for t (wrapped in JAXBElement)
                Text text2 = wmlObjectFactory.createText();
                JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRT(text2);
                r2.getContent().add( textWrapped2);
                    text2.setValue( " something");
                    text2.setSpace( "preserve");
 
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


Above code generated using the Docx4j Helper Word AddIn; you could also generate it using the docx4j webapp.

Re: How to set Bold to Text

PostPosted: Thu Oct 06, 2016 6:18 pm
by Sindhu
I have already done what you have given the code by creating two Run objects but i want it to create by using single Run object.
From your code, you have set text2 to second run object
but here i want single Run object , more number of text objects with same output.
as text2 will be set to first run object.

is there any possibility without using multiple runs.?

Re: How to set Bold to Text

PostPosted: Thu Oct 06, 2016 11:10 pm
by jason
Sindhu wrote:i want it to create by using single Run object.


Why? I asked you this already, but you didn't answer.

Sindhu wrote:is there any possibility without using multiple runs.?


As I have explained already, run properties are at the run level, so if you want different run properties for different text runs, then you need multiple run elements.