Page 1 of 1

get styles from existing document

PostPosted: Thu Apr 14, 2011 10:17 pm
by naked11
Hello, how I can get styles from existing docx document?
I need compare styles in docx document and dotx tempalte...
At first I need get styles, thanx a lot for advice!

Re: get styles from existing document

PostPosted: Thu Apr 14, 2011 10:29 pm
by Richard
You can find the styles in the StyleDefinitionPart of your MainDocumentPart. Just adept the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
WordprocessingMLPackage wml = WordprocessingMLPackage.load(new File("XYZ.docx"));
Styles s = wml.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();

for (Style style : styles.getStyle()) {
  if(style.getName.getVal().equals("Normal") {
    // ...
  }

  if(style.getName.getVal().equals("heading 1") {
    // ...
  }

  // prints the name of every style found in the document
  System.out.println(style.getName().getVal());
}
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4


In those styles you can find the formating by loading der RPr object, which contains information about font, fontsize, etc.:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
RPr properties = style.getRPr();
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4