javaapache-poitableofcontents

org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: union value 'false' does not match any members of 'ST_OnOff in namespace


Hi I am tring to create table of contents in word document using java apachi poi library. The I found a code form here. But when I run the code it gives me below error

rg.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: union value 'false' does not match any members of 'ST_OnOff in namespace http://schemas.openxmlformats.org/officeDocument/2006/sharedTypes

the code which creating the error is

XWPFParagraph paragraph = document.createParagraph();
CTP ctP = paragraph.getCTP();
CTSimpleField toc = ctP.addNewFldSimple();
toc.setInstr("TOC \\* MERGEFORMAT");
toc.setDirty(STOnOff.TRUE);

Poi version 5.2.3


Solution

  • If a Microsoft Word field shall be set "dirty", to tell Microsoft Word to refresh that field while next time opening the file, then this has been done using set CTSimpleField property dirty to org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff.TRUE up to Apache POI version 4.x.x.

    From version 5.0.0 on the org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff is not more present. There are other STOnOff classes but none of them provide TRUE/FALSE but only ON/OFF. But now CTSimpleField.setDirty accepts boolean values directly.

    To make this code work using Apache POI versions greater than 4, you need changing the following:

    ...
    //import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
    ...
      //toc.setDirty(STOnOff.TRUE);
      toc.setDirty(true);
    ...