Page 1 of 1

Converting field results to text?

PostPosted: Thu Jun 15, 2017 12:31 pm
by Vikram
Hi. Is there a way in docx4j to convert fields to static text? This is the functionality that is provided for in Word when you press Ctrl+Shift+F9 with fields selected.

Thanks.

Vikram

Re: Converting field results to text?

PostPosted: Mon Jun 19, 2017 1:57 pm
by jason
There are 2 field representations in OpenXML, simple and complex. See http://webapp.docx4java.org/OnlineDemo/ ... L/XML.html

For a simple field implementation, the body of the element contains the most recently updated field result, for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:r>
  <w:fldSimple w:instr="DATE"> 12/31/2005 </w:fldSimple>
</w:r>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


So that's straightforward (provided you are happy with the current field result), you just remove the fldSimple element, keeping its contents.

For a complex field implementation, you have an ugly set of runs:

• fldChar with attribute fldCharType value begin,

• One or more instrText elements, which, collectively, contain a complete field,

• Optionally,

• fldChar with attribute fldCharType value separate, which separates the field from its field result,

• Any number of runs and paragraphs that contains the most recently updated field result, and

• fldChar with attribute fldCharType value end.

So to process these, you need maintain state...

What docx4j gives you out of the box you can explore by trying the XHTML and XSL FO output.