Page 1 of 1

Trying to add Style to Paragraph

PostPosted: Sat Aug 04, 2012 12:07 am
by sandra
I am trying to add a PStyle to my Paragraph. everything compiles fine, no errors at all. But every time I try to open the file in WordViewer I get an "Can not open file" error:


Code: Select all
P  docx4jParagraph = factory.createP();
R   run = factory.createR();
docx4jParagraph.getContent().add(run);

pPr pPr = new PPr();
PStyle style = factory.createPPrBasePStyle();
style.setVal(styleID);
pPr.setPStyle(style);


as styleID I pass the String "berschrift1". Which I also can find in the styles.xml :

Code: Select all
<w:style w:type="paragraph" w:styleId="berschrift1">
<w:name w:val="heading 1"/>
<w:basedOn w:val="Standard"/>
<w:next w:val="Standard"/>
<w:link w:val="berschrift1Zchn"/>
<w:uiPriority w:val="9"/>
<w:qFormat/>
<w:rsid w:val="007416DC"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:before="480" w:after="0"/>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:rFonts w:asciiTheme="majorHAnsi" w:hAnsiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:cstheme="majorBidi"/>
<w:b/>
<w:bCs/>
<w:color w:val="365F91" w:themeColor="accent1" w:themeShade="BF"/>
<w:sz w:val="28"/>
<w:szCs w:val="28"/>
</w:rPr>
</w:style>


For my complete styles.xml please see the attached file.
styles.xml
(19.73 KiB) Downloaded 419 times


Where is my fault?

Many thanks for help in advance.
Best regards,
Sandra

Re: Trying to add Style to Paragraph

PostPosted: Sat Aug 04, 2012 9:46 am
by jason
what docx4j would normally do:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public org.docx4j.wml.P createStyledParagraphOfText(String styleId, String text) {
               
                org.docx4j.wml.P p = createParagraphOfText(text);
                                               
                StyleDefinitionsPart styleDefinitionsPart
                        = this.getStyleDefinitionsPart();

                if (getPropertyResolver().activateStyle(styleId)) {
                        // Style is available
                        org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();                  
                        org.docx4j.wml.PPr  pPr = factory.createPPr();
                        p.setPPr(pPr);
                        org.docx4j.wml.PPrBase.PStyle pStyle = factory.createPPrBasePStyle();
                        pPr.setPStyle(pStyle);
                        pStyle.setVal(styleId);
                }              
               
                return p;

        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


in Word, the document would still open if the style hadn't been activated; the style would just be ignored. But maybe this is a problem for the Word Viewer?