Page 1 of 1

update/process values of FORMTEXT fldChar sequence

PostPosted: Sat Dec 25, 2010 9:36 pm
by ivano
Hello.

I have a template docx with textbox form fields (FldChars of field-type: form-field FORMTEXT).
They store directives how to fill and process template in default text value of ffData elem.
In simple case it needs just to set form field value, in more complicated - delete table or table
raw or paragraph with correnponding form field.

At first i got all FldChars using getJAXBNodesViaXPath(), but i had to for example find parent table
to process it, but i got known jaxb error with JAXBElement instead of parent. So i tried to
get parent Node using binder:

Code: Select all
      List<Object> runList = documentPart.getJAXBNodesViaXPath(xpath, false);
      javax.xml.bind.Binder<Node> binder = org.docx4j.jaxb.Context.jc.createBinder();
      for (Object o : runList)
      {
         org.w3c.dom.Node node = binder.getXMLNode(o);
         Node parent = node.getParentNode();
      ...


but node is always NULL.
My questions are:
  1. What did i wrong with getting xml node from jaxb node?
  2. Are there any ways to navigate following/preceding siblings of JAXBElements?
  3. Could you suggest more convenient/right way for doing suth template proccesing task with docx4j?
    We have a lot of ready templates with form fields, so i have to use them as thay are.

Thanks.

Re: update/process values of FORMTEXT fldChar sequence

PostPosted: Mon Dec 27, 2010 6:35 pm
by jason
The binder is created when the main document part is unmarshalled. You don't need to do that again (see MainDocumentPart source for more info).

To work around the getParent bug, use an XPath which returns the parent (ie a list of nodes which each of which have the child you are looking for).

Alternatively, you can use http://dev.plutext.org/svn/docx4j/trunk ... lUtil.java to traverse your document looking for matches. (This is what I tend to use). You can set things up to return a list of structures of your own devising, if you want.

To get down to specifics with either of the above approaches, it would help if you posted a sample w:p containing your FORMTEXT.

Re: update/process values of FORMTEXT fldChar sequence

PostPosted: Tue Dec 28, 2010 9:29 am
by ivano
It works with documentPart.getBinder(). Either of
XmlUtils.getJAXBNodesViaXPath(documentPart.getBinder(), row, "..", false) and
Node node = binder.getXMLNode(row);
Object parent = XmlUtils.unwrap(binder.getJAXBNode(node.getParentNode()));

Form field is represented by three FldChars with fldCharTypes of begin, separate, end. Value is
between separate and end. And form fields are all over the document.
Code: Select all
<w:p>
...
<w:r>
   <w:rPr>
      <w:rFonts .... />
      <w:lang w:val="en-US"/>
   </w:rPr>
   <w:fldChar w:fldCharType="begin">
      <w:ffData>
         <w:name w:val=""/>
         <w:enabled/>
         <w:calcOnExit w:val="0"/>
         <w:textInput>
            <w:default w:val="this is where directive what to do"/>
         </w:textInput>
      </w:ffData>
   </w:fldChar>
</w:r>
...
<w:r>
   <w:instrText>FORMTEXT</w:instrText>
</w:r>
...
<w:r>
   <w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
   <w:rPr>
      <w:rFonts .... />
      <w:noProof/>
      <w:lang w:val="en-US"/>
   </w:rPr>
   <w:t>this is where possible value goes</w:t>
</w:r>
...
<w:r>
   <w:fldChar w:fldCharType="end"/>
</w:r>
...
</w:p>

Re: update/process values of FORMTEXT fldChar sequence

PostPosted: Tue Dec 28, 2010 5:00 pm
by jason
ivano wrote:It works with


So your problem is solved then?

Re: update/process values of FORMTEXT fldChar sequence

PostPosted: Fri Dec 31, 2010 1:46 am
by ivano
Yes, thanks.