Page 1 of 1

Accessing all styles from styles.xml

PostPosted: Wed May 31, 2017 8:47 pm
by Zanza
Hello,
I'm trying to access all of the style which are present in styles.xml.

Using :
Code: Select all
StyleDefinitionsPart sdp = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart();
List<Style> styleList = sdp.getJaxbElement().getStyle();
for (Style style : styleList) {
System.out.println(style.getName().getVal());
}


on a new almost empty docx, I've got this :
Code: Select all
Normal
Default Paragraph Font
Normal Table
No List


Whereas, in the styles.xml, I found
Code: Select all
<w:lsdException w:name="Normal" w:uiPriority="0" w:qFormat="1"/>
<w:lsdException w:name="heading 1" w:uiPriority="9" w:qFormat="1"/>
<w:lsdException w:name="heading 2" w:semiHidden="1" w:uiPriority="9" w:unhideWhenUsed="1" w:qFormat="1"/>
<w:lsdException w:name="heading 3" w:semiHidden="1" w:uiPriority="9" w:unhideWhenUsed="1" w:qFormat="1"/>
<w:lsdException w:name="heading 4" w:semiHidden="1" w:uiPriority="9" w:unhideWhenUsed="1" w:qFormat="1"/>
   .......
</w:latentStyles>


How can I access all the latent style?

Re: Accessing all styles from styles.xml

PostPosted: Wed May 31, 2017 9:27 pm
by jason
You are doing it right :-)

Those aren't styles as such; see http://webapp.docx4java.org/OnlineDemo/ ... ption.html

But if you want the latent styles, call getLatentStyles() instead of getStyle(); see https://github.com/plutext/docx4j/blob/ ... .java#L137

Re: Accessing all styles from styles.xml

PostPosted: Wed May 31, 2017 11:02 pm
by Zanza
(Thanks for your answer !)

Ok, so the latent style are stored in MS Office application, while listed in the styles.xml.
And with the getLatentStyles().getLsdException(), I can list them.

But how can I load them to use them after?

Re: Accessing all styles from styles.xml

PostPosted: Thu Jun 01, 2017 10:55 am
by jason
In general, you can create a docx in Word containing the style you want (so it is defined in styles.xml), then run that through the Docx4j Word AddIn or webapp to generate corresponding Java code.

But there is also https://github.com/plutext/docx4j/blob/ ... Styles.xml
accessed via https://github.com/plutext/docx4j/blob/ ... .java#L193