Page 1 of 1

Changing text in a TextBox

PostPosted: Thu Apr 25, 2013 9:44 pm
by davethedog
I have a template docx with a some place holder text in it. The text I wan to replace sits in TextBoxes:

Code: Select all
<w:p w:rsidR="00271DA2" w:rsidRDefault="003842F8" w:rsidP="00271DA2">
            <w:r>
              <w:rPr>
                <w:noProof/>
                <w:lang w:eastAsia="en-GB"/>
              </w:rPr>
              <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 9" o:spid="_x0000_s1026" type="#_x0000_t202" style="position:absolute;margin-left:83.2pt;margin-top:15.9pt;width:225.75pt;height:24.35pt;z-index:251671552;visibility:visible;mso-wrap-style:square;mso-width-percent:0;mso-height-percent:0;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;mso-width-percent:0;mso-height-percent:0;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top">
                  <v:textbox>
                    <w:txbxContent>
                      <w:p w:rsidR="00271DA2" w:rsidRDefault="001F7F20" w:rsidP="00271DA2">
                        <w:r>
                          <w:t>FIRST_NAME</w:t>
                        </w:r>
                      </w:p>
                    </w:txbxContent>
                  </v:textbox>
                </v:shape>
              </w:pict>
            </w:r>
          </w:p>


I've tried using

Code: Select all
List<Object> paragraphs = getAllElementFromObject(document.getMainDocumentPart(), P.class);
for (Object p : paragraphs) {
      List<Object> texts = getAllElementFromObject(p, Text.class);
      for (Object t : texts) {
        Text content = (Text) t;
        System.out.println("* " + content.getValue());
      }
    }


But this only returns the text in the doc, and not text boxes. I tried:

Code: Select all
    List<Object> pics = getAllElementFromObject(document.getMainDocumentPart(), Pict.class);
    for (Object p : pics) {
      List<Object> texts = getAllElementFromObject(p, Text.class);
      for (Object t : texts) {
        Text content = (Text) t;
        System.out.println("* " + content.getValue());
      }
    }


But this does not get the text within the w:pict tags.

Any suggestions@

Re: Changing text in a TextBox

PostPosted: Thu Apr 25, 2013 11:40 pm
by jason
Get it via XPath, or TraversalUtil (not sure without checking exactly when v:textbox support was introduced there, so suggest you use a current nightly build to be sure), for general approaches to finding stuff. See Getting Started for more details on those.

That document also explains ways that docx4j can do placeholder replacement for you. See for example the VariableReplace sample, or for the rolls royce solution, look at content control data binding.

Re: Changing text in a TextBox

PostPosted: Fri Apr 26, 2013 2:41 am
by davethedog
Thanks Jason,

The sample code sorted it - incredibly simple

Code: Select all
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("FIRST_NAME", person.getFirstName());
    map.put("LAST_NAME", person.getLastName());

    documentPart.variableReplace(map);


And in the doc just put in the tags ${FIRST_NAME} and ${LAST_NAME}, and the substitution works. (As stated in the demo code though you need to ensure the document doesn't put your tags into multiple runs.

The 'variableReplace' method isn't present in 2.8.1.2 though, so I used the nightly build from 20130425

Re: Changing text in a TextBox

PostPosted: Thu Apr 17, 2014 1:19 am
by Fuchs
Hey Dave!

I´m starting now in java and I need to generate some docx.
I have the same problem and docx4j do nothing when my placeholder is located in a textbox.
I´m trying with HashMap and variableReplace but don´t work too...

Can you please post the whole metode?

Thank´s

Re: Changing text in a TextBox

PostPosted: Fri Jul 07, 2023 9:24 pm
by Michael
Does anyone have a full example of how to complete a TextBox in a Picture, without changing the value, but with setting the value?

Re: Changing text in a TextBox

PostPosted: Sun Jul 09, 2023 7:37 am
by jason
Please post sample XML.