Page 1 of 1

styles - Relationship problem

PostPosted: Fri Oct 03, 2008 8:53 am
by jecmenb
Hi all.
I have a small problem with setting bold font style in document.
I have WordprocessingMLPackage this.wmlPack and I'm trying this:
Code: Select all
    P p = this.wmlPack.getMainDocumentPart().createParagraphOfText("text");
    BooleanDefaultTrue b = new BooleanDefaultTrue();
    b.setVal(true);
    p.getPPr().getRPr().setB(b);
            
    this.wmlPack.getMainDocumentPart().addObject(p);


But log says this:

Loading part /word/styles.xml
Convenience method established from /word/document.xml to /word/styles.xml
.. added.
No relationships word/_rels/styles.xml.rels
word/styles.xml false
[Content_Types].xml false
word/_rels/document.xml.rels false
word/document.xml false
_rels/.rels false


Could you advise me please, how correctly use bold, underline or italic font in document?
I'm using docx4j v2.0 release.

Thanks, Bob.

Re: styles - Relationship problem

PostPosted: Mon Oct 06, 2008 10:24 am
by jason
Hi

Pls disregard the debug you saw.

If you have a look at what Word does in making stuff bold, you'll see:

Code: Select all

<!-- Word 2007 emits this -->

      <w:p>
         <w:pPr>
            <w:rPr>
               <w:b />
            </w:rPr>
         </w:pPr>
         <w:r>
            <w:rPr>
               <w:b />
            </w:rPr>
            <w:t>Bold, done by Word - at both pPr and w:r level</w:t>
         </w:r>
      </w:p>

<!-- Bold, just within the w:r works-->

      <w:p>
         <w:r>
            <w:rPr>
               <w:b />
            </w:rPr>
            <w:t>Bold, just at w:r level</w:t>
         </w:r>
      </w:p>

      <w:p>
         <w:r>
            <w:rPr>
               <w:b w:val="true" />
            </w:rPr>
            <w:t>Bold, just at w:r level, with val="true"</w:t>
         </w:r>
      </w:p>

<!-- Bold, just at the w:pPr doesn't work -->

      <w:p>
         <w:pPr>
            <w:rPr>
               <w:b w:val="true" />
            </w:rPr>
         </w:pPr>
         <w:r>
            <w:t>just pPr/rPr - doesn't work</w:t>
         </w:r>
      </w:p>
      <w:p>
         <w:pPr>
            <w:rPr>
               <w:b />
            </w:rPr>
         </w:pPr>
         <w:r>
            <w:t>just pPr/rPr - doesn't work</w:t>
         </w:r>
      </w:p>



So you can see that you must have w:b in the w:r, and can optionally have it in the w:pPr.

If you use the createParagraphOfText method, you still need to get the w:r in order to set w:b in it.

So its easier not to do that.

The long winded way to do it:

Code: Select all

      org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
      org.docx4j.wml.P  p = factory.createP();

      org.docx4j.wml.Text  t = factory.createText();
      t.setValue("text");

      org.docx4j.wml.R  run = factory.createR();
      run.getRunContent().add(t);      
      
      p.getParagraphContent().add(run);
      
      
      org.docx4j.wml.RPr rpr = factory.createRPr();      
      org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
       b.setVal(true);      
       rpr.setB(b);
      
      run.setRPr(rpr);
      
      // Optionally, set pPr/rPr@w:b      
       org.docx4j.wml.PPr ppr = factory.createPPr(); 
       p.setPPr( ppr );
       org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr();
       ppr.setRPr(paraRpr);      
       rpr.setB(b);
      
       //    Add it to the doc
       wordMLPackage.getMainDocumentPart().addObject(p);
      


Here is an easier way:

Code: Select all

       String str = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Bold, just at w:r level</w:t></w:r></w:p>";
      
       wordMLPackage.getMainDocumentPart().addObject(
                org.docx4j.XmlUtils.unmarshalString(str) );



fyi, XmlUtils also contains a method unmarshallFromTemplate, which you can use if you need to replace things in the string.


cheers

Jason

Re: styles - Relationship problem

PostPosted: Thu Oct 09, 2008 1:41 am
by neversaydie
If you have an existing document with bold fonts, and try your previous code to add a new paragraph, it will work.
Because styles.xml will already have that style defined.
If you are writing a new document and want the style to appear, you have to add the part named styles.xml and then use.

Re: styles - Relationship problem

PostPosted: Mon Oct 13, 2008 6:29 am
by jecmenb
OK, thank you very much, it works good, both ways. It's great.