Page 1 of 1

How to get the correct styles used in a docx document

PostPosted: Wed Feb 13, 2013 3:51 am
by zkoppanylist
The code below returns some internal style names (or whether) instead of the style names appear on Word's user interface.

public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(args[0]));

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

Map<String,String > styles = documentPart.getStylesInUse();
for (String styleKey : styles.keySet()) {
String value = styles.get(styleKey);

System.out.println("key <" + styleKey + "> value <" + value + ">");
}
}

I get values as below:

key <berschrift1> value <berschrift1>
key <berschrift3> value <berschrift3>
key <Textkrper> value <Textkrper>
key <berschrift2> value <berschrift2>
key <Blocktext> value <Blocktext>

Instead of I would like get the same what Word also displays for example: Überschrift 1. The information is obviously available in word/styles.xml:

<w:semiHidden/><w:unhideWhenUsed/></w:style><w:style w:type="character" w:customStyle="1" w:styleId="berschrift1Zeichen"><w:name w:val="Überschrift 1 Zeichen"/><w:basedOn w:val="Absatzstandardschriftart"/><w:link w:val="berschrift1"/>

As far as I understand the ID of the style is "berschrift1" and word displays ""Überschrift 1".

How can I get this information pragmatically?

Thanks,

Zsolt

Re: How to get the correct styles used in a docx document

PostPosted: Wed Feb 13, 2013 10:13 am
by jason
Something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
StyleDefinitionsPart sdp = mainDocumentPart.getStyleDefinitionsPart();
Styles styles = sdp.getJaxbElement();
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: How to get the correct styles used in a docx document

PostPosted: Wed Feb 13, 2013 10:30 pm
by zkoppanylist
Thank you :)