ASPOSE - Word document headings not detected

Hi Adam,

In our app, we have code to check the headings in a particular word document. But for some type of headings (eg: Heading 1_0) it’s not working as expected. For example, in the attached document we have several headings, but code not detecting those and giving zero headings. Pls guide us.

PFB the piece of code we have written and PFA the document we are facing issue.

final NodeCollection paragraphColl = doc.getChildNodes(NodeType.PARAGRAPH, true);
int maxhdr = 1;
int prevStyleId = 0;
for (Iterator iterator = paragraphColl.iterator(); iterator.hasNext();) {
    final Paragraph paragraph = (Paragraph)iterator.next();
    String nodeStyle = paragraph.getParagraphFormat().getStyle().getName();
    String tocText = paragraph.toTxt().trim();
    if (tocText == null || tocText.equals("")) { continue; }
    int styleId = paragraph.getParagraphFormat().getStyleIdentifier();
    if (paragraph.getParagraphFormat().isHeading()) { // looks like not detecting here
        TemplateSection ts = null;
        if (styleId - prevStyleId > 1) {
            for (int i = prevStyleId + 1; i < styleId; i++) {
                ts = new TemplateSection();
                ts.setStyleId(i);
                ts.setDummy(true);
                headingSections.add(ts);
            }
        }
        ts = new TemplateSection();
        ts.setNodeStyle(nodeStyle);
        ts.setStyleId(styleId);
        ts.setSectionName(tocText);
        ts.setDummy(false);
        headingSections.add(ts);
        if (styleId > maxhdr) maxhdr = styleId;
        prevStyleId = styleId;
    }
}

This code works fine for Heading 1 type. Tested and verified.

This message was posted using Email2Forum by aske012. (private)

Hi Anvar,

Thanks for your inquiry.

I checked your source documents and it the current behaviour is correct. The styles with the suffix consisting of an underscore and a number are created when you append documents with ImportFormatMode.KeepSourceFormatting. The heading style from the source document is copied over as a new style so no formatting is lost. However this means that it is no longer a built in heading style which is why your code is not working.

There are a few options you can use to fix this:

  1. If applicable, you can use ImportFormatMode.UseDestinationStyles instead.

  2. You can check if the paragraph style begins with “Heading” instead.

if(paragraph.getParagraphFormat().getStyle().getName().startsWith("Heading"))
{
}
  1. You can check if the outline level is not body text, this should work correctly even with heading styles imported from other documents but it may be a bit unreliable. Any paragraph, not just heading can be made into an Outline level 1 - 9. However most of the time only headings have this outline.
if ( paragraph.getParagraphFormat().getOutlineLevel() <= OutlineLevel.Level9)
{
}

If we can help with anything else, please feel free to ask.

Thanks,