Page 1 of 1

how do activate style in a document

PostPosted: Tue Dec 09, 2008 5:03 pm
by fiorenzo
Hi,

i want use in my document created by template some style predefined.

Code: Select all
               WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .load(new java.io.File(inputfilepath));

      // 2. Fetch the document part
      StyleDefinitionsPart stylePart = wordMLPackage.getMainDocumentPart()
            .getStyleDefinitionsPart();
      org.docx4j.wml.Styles styles = (org.docx4j.wml.Styles) stylePart
            .getJaxbElement();
      for (Iterator iter = styles.getStyle().iterator(); iter.hasNext();) {
         org.docx4j.wml.Style s = (org.docx4j.wml.Style) iter.next();
         
         if (stylePart.activateStyle(s.getStyleId())) {
            System.out.println("active style value: " + s.getName().getVal();
            System.out.println("active styleID: " + s.getStyleId());
         } else {
            System.out.println("NON ACTIVE STYLE: " + s.getName().getVal()
                  + " ." + s.getType());
            System.out.println("NON ACTIVE STYLEID: " + s.getStyleId());
         }
                       System.out.println("---------------------");
      }

I don't understand how activate a style present in my styles list.

In StyleMappedPart i see the internal mechanism for syles...

Must i re-define a non active style avant to use??

Thanks

Fiorenzo

Re: how do activate style in a document

PostPosted: Wed Dec 10, 2008 1:46 am
by jason
Calling the activateStyle( String styleId ) method should be all you need to do for a "known style" (that is, one defined in http://dev.plutext.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/KnownStyles.xml ).

If you want to provide your own definition, use activateStyle(org.docx4j.wml.Style s)

Note that if it is already active, activateStyle( String styleId ) will return false. It returns false, not because the style is inactive, but because we don't want to accidentally replace the existing active style with the default definition in KnownStyles.xml

This is what makes the output of your for loop misleading.

hope this helps,

Jason

Re: how do activate style in a document

PostPosted: Wed Dec 10, 2008 12:16 pm
by fiorenzo
Thanks a lot!

Fiorenzo