I think I figured it out after browsing through the source. You should not make your own POIXMLProperties from the OPC package if you are dealing with an XWPFDocument, you should use doc.getProperties(). This is because when the document is written out, it calls its own getProperties().commit(). If the doc has never called its own getProperties() before, it makes a new one out of the opc package.
I'm not sure if this behavior makes sense or not, shouldn't modifications to the properties though getUnderlyingProperties make changes via references which would propagate back up and affect any newly create POIXMLProperties created from the same OPC reference? On Tue, Feb 5, 2013 at 1:31 PM, Mark Kimsal <[email protected]> wrote: > I'm trying to update the values of custom properties in an XWPFDocument. > If I do not commit() the properties after changing the values with > CTProperty.setLpwstr() then the values do not change. If I commit the > properties after changing the value then I get duplicate XML tags in the > docProps/custom.xml and docProps/app.xml > > > Code: > { > OPCPackage opc = OPCPackage.open( > loader.getResourceAsStream("META-INF/"+template+".docx")); > > updateProperties(opc) > > XWPFDocument doc = new XWPFDocument( opc ); > // document bookmar stuff, but even commented out doesn't change > // duplicate tag behavior > doc.write(outputStream); > opc.close(); > } > > private final void updateProperties(OPCPackage opc) { > POIXMLProperties props = new POIXMLProperties(opc); > CustomProperties customProperties = props.getCustomProperties(); > > org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties > ctprops = customProperties.getUnderlyingProperties(); > List<CTProperty> propList = ctprops.getPropertyList(); > for(CTProperty ctp : propList) { > System.out.println(ctp.getName()); > if (ctp.getName().equals("LEGAL NAME")) { > ctp.setLpwstr("Hello World"); > } > } > props.commit(); //commenting out does not change docProps/*, > uncommenting gives duplicate tags > } > > Does anyone know how to get property changes to ripple up into the doc > without doing props.commit() ? > > app.xml > <?xml version="1.0" encoding="UTF-8"?> > <Properties xmlns=" > http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" > xmlns:vt=" > http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft > Office > Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company> > </Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties><?xml > version="1.0" encoding="UTF-8"?> > <Properties xmlns=" > http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" > xmlns:vt=" > http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Template>Normal.dotm</Template><TotalTime>0</TotalTime><Pages>3</Pages><Words>1002</Words><Characters>5712</Characters><Application>Microsoft > Office > Word</Application><DocSecurity>0</DocSecurity><Lines>47</Lines><Paragraphs>13</Paragraphs><ScaleCrop>false</ScaleCrop><Company> > </Company><LinksUpToDate>false</LinksUpToDate><CharactersWithSpaces>6701</CharactersWithSpaces><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0000</AppVersion></Properties> > > custom.xml > <?xml version="1.0" encoding="UTF-8"?> > <Properties xmlns=" > http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" > xmlns:vt=" > http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" > name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL > NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" > name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties><?xml > version="1.0" encoding="UTF-8"?> > <Properties xmlns=" > http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" > xmlns:vt=" > http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" > name="COUNTRY"><vt:lpwstr>[COUNTRY]</vt:lpwstr></property><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="3" name="LEGAL > NAME"><vt:lpwstr>Hello World</vt:lpwstr></property><property > fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="4" > name="NAME"><vt:lpwstr>INSERT NAME</vt:lpwstr></property></Properties> > >
