Page 1 of 1

Change Font color and background color of Text box shape

PostPosted: Thu Jul 19, 2012 11:15 pm
by ursgnanz
Hi All,

my requirement is to update the Font color and background color of Text box shape of the existing docx document.
I'm very new to docx update(docx4j) , so i have no clue how to do this.
i have seen xpathquery sample but don't know how to apply this to shape font attribute?

is there any solution availble to this.

Re: Change Font color and background color of Text box shape

PostPosted: Tue Jul 24, 2012 8:14 pm
by jason
You should read Getting Started, please; it tells you how to tackle problems like this.

The general approach described there, applied to your problem.

1. create a docx containing the feature of interest. In this case, you get:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <mc:AlternateContent>
          <mc:Choice Requires="wps">
            <w:drawing>
              <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251659264" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1" wp14:editId="36B11C9B">
                <wp:simplePos x="0" y="0"/>
                <wp:positionH relativeFrom="column">
                  <wp:align>center</wp:align>
                </wp:positionH>
                <wp:positionV relativeFrom="paragraph">
                  <wp:posOffset>0</wp:posOffset>
                </wp:positionV>
                <wp:extent cx="2374265" cy="1403985"/>
                <wp:effectExtent l="0" t="0" r="3175" b="5715"/>
                <wp:wrapNone/>
                <wp:docPr id="307" name="Text Box 2"/>
                <wp:cNvGraphicFramePr>
                  <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
                </wp:cNvGraphicFramePr>
                <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                  <a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
                    <wps:wsp>
                      <wps:cNvSpPr txBox="1">
                        <a:spLocks noChangeArrowheads="1"/>
                      </wps:cNvSpPr>
                      <wps:spPr bwMode="auto">
                        <a:xfrm>
                          <a:off x="0" y="0"/>
                          <a:ext cx="2374265" cy="1403985"/>
                        </a:xfrm>
                        <a:prstGeom prst="rect">
                          <a:avLst/>
                        </a:prstGeom>
                        <a:solidFill>
                          <a:srgbClr val="FFFFFF"/>
                        </a:solidFill>
                        <a:ln w="9525">
                          <a:solidFill>
                            <a:srgbClr val="000000"/>
                          </a:solidFill>
                          <a:miter lim="800000"/>
                          <a:headEnd/>
                          <a:tailEnd/>
                        </a:ln>
                      </wps:spPr>
                      <wps:txbx>
                        <w:txbxContent>
                          <w:p w:rsidR="00F54378" w:rsidRDefault="00F54378">
                            <w:r>
                              <w:t>Here are the contents of the text box.</w:t>
                            </w:r>
                          </w:p>
                        </w:txbxContent>
                      </wps:txbx>
                      <wps:bodyPr rot="0" vert="horz" wrap="square" lIns="91440" tIns="45720" rIns="91440" bIns="45720" anchor="t" anchorCtr="0">
                        <a:spAutoFit/>
                      </wps:bodyPr>
                    </wps:wsp>
                  </a:graphicData>
                </a:graphic>
                <wp14:sizeRelH relativeFrom="margin">
                  <wp14:pctWidth>40000</wp14:pctWidth>
                </wp14:sizeRelH>
                <wp14:sizeRelV relativeFrom="margin">
                  <wp14:pctHeight>20000</wp14:pctHeight>
                </wp14:sizeRelV>
              </wp:anchor>
            </w:drawing>
          </mc:Choice>
          <mc:Fallback>
            <w:pict>
              <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">
                <v:stroke joinstyle="miter"/>
                <v:path gradientshapeok="t" o:connecttype="rect"/>
              </v:shapetype>
              <v:shape id="Text Box 2" o:spid="_x0000_s1026" type="#_x0000_t202" style="position:absolute;margin-left:0;margin-top:0;width:186.95pt;height:110.55pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:center;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" o:gfxdata="UEsDBBQABgAIAAAAIQC2gziS/gAAAOEBAAATAAAAW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8ucmVsc1BL&#xA;AQItABQABgAIAAAAIQAUgLpIJQIAAEcEAAAOAAAAAAAAAAAAAAAAAC4CAABkcnMvZTJvRG9jLnht&#xA;bFBLAQItABQABgAIAAAAIQD9LzLW2wAAAAUBAAAPAAAAAAAAAAAAAAAAAH8EAABkcnMvZG93bnJl&#xA;di54bWxQSwUGAAAAAAQABADzAAAAhwUAAAAA&#xA;">
                <v:textbox style="mso-fit-shape-to-text:t">
                  <w:txbxContent>
                    <w:p w:rsidR="00F54378" w:rsidRDefault="00F54378">
                      <w:r>
                        <w:t>Here are the contents of the text box.</w:t>
                      </w:r>
                    </w:p>
                  </w:txbxContent>
                </v:textbox>
              </v:shape>
            </w:pict>
          </mc:Fallback>
        </mc:AlternateContent>
      </w:r>
    </w:p>
 
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


docx4j will use the mc:Fallback, so we are interested in:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
            <w:pict>
              <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">
                <v:stroke joinstyle="miter"/>
                <v:path gradientshapeok="t" o:connecttype="rect"/>
              </v:shapetype>
              <v:shape id="Text Box 2" o:spid="_x0000_s1026" type="#_x0000_t202" style="position:absolute;margin-left:0;margin-top:0;width:186.95pt;height:110.55pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:center;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" o:gfxdata="....">
                <v:textbox style="mso-fit-shape-to-text:t">
                  <w:txbxContent>
                    <w:p w:rsidR="00F54378" w:rsidRDefault="00F54378">
                      <w:r>
                        <w:t>Here are the contents of the text box.</w:t>
                      </w:r>
                    </w:p>
                  </w:txbxContent>
                </v:textbox>
              </v:shape>
            </w:pict>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


2. now change the text box background colour, and font colour. You get:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
       <w:pict>
          <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">
            <v:stroke joinstyle="miter"/>
            <v:path gradientshapeok="t" o:connecttype="rect"/>
          </v:shapetype>
          <v:shape id="Text Box 2" o:spid="_x0000_s1026" type="#_x0000_t202" style="position:absolute;margin-left:0;margin-top:0;width:186.95pt;height:110.55pt;z-index:251659264;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:center;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top" fillcolor="yellow">
            <v:textbox style="mso-fit-shape-to-text:t">
              <w:txbxContent>
                <w:p w:rsidR="00F54378" w:rsidRPr="00E82642" w:rsidRDefault="00F54378">
                  <w:pPr>
                    <w:rPr>
                      <w:color w:val="FF0000"/>
                    </w:rPr>
                  </w:pPr>
                  <w:r w:rsidRPr="00E82642">
                    <w:rPr>
                      <w:color w:val="FF0000"/>
                    </w:rPr>
                    <w:t>Here are the contents of the text box.</w:t>
                  </w:r>
                </w:p>
              </w:txbxContent>
            </v:textbox>
          </v:shape>
        </w:pict>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


You can ignore o:gfxdata

You can see the font color is given by <w:color w:val="FF0000"/> and background colour by @fillcolor

So, use one of the approaches described in Getting Started to edit the relevant nodes identified above.

Good luck, and if you need more help, please post your code.