I have a very simple issue that is driving me crazy. Basically I want to extract, via POI/DOCX4J libraries, docx paragraph structure and document outline. I did the same task with a normal doc document using the POI paragraph.getLvl() method. Is there a way to get the same result with a docx? How can I re-construct the entire TOC structure of the docx?
Solution:
I resolved in this way:
Map headingMap = new HashMap();
headingMap.put("heading 1", 1);
headingMap.put("heading 2", 2);
headingMap.put("heading 3", 3);
headingMap.put("heading 4", 4);
headingMap.put("heading 5", 5);
headingMap.put("heading 6", 6);
headingMap.put("heading 7", 7);
headingMap.put("heading 8", 8);
headingMap.put("heading 9", 9);
Iterator<XWPFParagraph> iterator = docx.getParagraphsIterator();
Styles styles = getStyle(completePath);
while(iterator.hasNext()){
XWPFParagraph p = iterator.next();
if( p != null && p.getStyleID() != null){
for (Style s : styles.getStyle()){
if (p.getStyleID().equals(s.getStyleId()) && headingMap.containsKey(s.getName().getVal())){
StringBuffer text = new StringBuffer();
for(XWPFRun run : p.getRuns()) {
text.append(run.toString());
}
}
}
}
}