从docx4j中的docx获取文本框

时间:2013-09-12 14:06:01

标签: java ms-word docx4j

Word允许某人创建一个文本框并用颜色填充它。我试图从中找出两个值:1)字体的颜色2)文本框填充的颜色

我在docx4j的webapp上尝试了两个例子,下面是差异

  1. 文字框颜色=黑色。文字颜色=黑色

    <w:pict>
        <v:shapetype o:spt="202.0" path="m0,0l0,21600,21600,21600,21600,0xe" coordsize="21600,21600" id="_x0000_t202">
            <v:stroke joinstyle="miter"/>
            <v:path gradientshapeok="t" o:connecttype="rect"/>
        </v:shapetype>
    
        <v:shape o:gfxdata="<bunch of text here>" type="#_x0000_t202" style="position:absolute;margin-left:18pt;margin-top:15.75pt;width:172.65pt;height:1in;z-index:251659264;visibility:visible;mso-wrap-style:none;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:top" id="Text Box 2" o:spid="_x0000_s1026" stroked="f" fillcolor="black [3213]">
            <v:textbox>
                <w:txbxContent>
                    <w:p w14:paraId="77D94695" w14:textId="70E108CF">
                        <w:r>
                            <w:t xml:space="preserve">This is a simple test by </w:t>
                        </w:r>    
    
                        <w:proofErr w:type="spellStart"/>
                        <w:r>
                            <w:t>foobar</w:t>
                        </w:r>
    
                        <w:proofErr w:type="spellEnd"/>
                    </w:p>
                </w:txbxContent>
            </v:textbox>
    
            <w10:wrap type="square"/>
        </v:shape>
    </w:pict>
    
  2. 文字框颜色=黑色。文字颜色=白色

    <w:pict>
        <v:shapetype o:spt="202.0" path="m0,0l0,21600,21600,21600,21600,0xe" coordsize="21600,21600" id="_x0000_t202">
            <v:stroke joinstyle="miter"/>
            <v:path gradientshapeok="t" o:connecttype="rect"/>
        </v:shapetype>
        <v:shape o:gfxdata="" type="#_x0000_t202" style="position:absolute;margin-left:18pt;margin-top:15.75pt;width:172.65pt;height:1in;z-index:251659264;visibility:visible;mso-wrap-style:none;mso-wrap-distance-left:9pt;mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;mso-position-horizontal-relative:text;mso-position-vertical:absolute;mso-position-vertical-relative:text;v-text-anchor:top" id="Text Box 2" o:spid="_x0000_s1026" stroked="f" fillcolor="black [3213]">
            <v:textbox>
                <w:txbxContent>
                    <w:p w14:paraId="77D94695" w14:textId="70E108CF">
                        <w:pPr>
                            <w:rPr>
                                <w:color w:val="FFFFFF" w:themeColor="background1"/>
                            </w:rPr>
                        </w:pPr>
                        <w:r>
                            <w:rPr>
                                <w:color w:val="FFFFFF" w:themeColor="background1"/>
                            </w:rPr>
                            <w:t xml:space="preserve">This is a simple test by </w:t>
                        </w:r>
                        <w:proofErr w:type="spellStart"/>
                        <w:r>
                            <w:rPr>
                                <w:color w:val="FFFFFF" w:themeColor="background1"/>
                            </w:rPr>
                            <w:t>b</w:t>
                        </w:r>
                        <w:bookmarkStart w:name="_GoBack" w:id="1"/>
                        <w:bookmarkEnd w:id="1"/>
                        <w:r>
                            <w:rPr>
                                <w:color w:val="FFFFFF" w:themeColor="background1"/>
                            </w:rPr>
                            <w:t>foobar</w:t>
                        </w:r>
                        <w:proofErr w:type="spellEnd"/>
                    </w:p>
                </w:txbxContent>
            </v:textbox>
            <w10:wrap type="square"/>
        </v:shape>
    </w:pict>
    
  3. 问题

    如何访问w:pict标记和v:shape这是一个chid。在文档中找到这两个值的最佳方法是什么?

    我尝试了什么

    我正在使用像这样的traversaleUtil

    class DocumentTraverser  extends TraversalUtil.CallbackImpl {
        @Override
        public List<Object> apply(Object o) {
            println " class name"
            println o.getClass()
            if (o instanceof org.docx4j.wml.Text) {
                text.append(((org.docx4j.wml.Text) o).getValue()); //append to text
            }
            else if (o instanceof org.docx4j.vml.CTShape)   {
                org.docx4j.vml.CTShape s = ((org.docx4j.vml.CTShape)o);
                log.debug(s.getFillcolor())
                //how can I get color of the text inside the text box
            }
    
            return null;
        }
    }
    

0 个答案:

没有答案