We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to determine # of lists in word doc

I would like to determine;
1) the number of actual (visible) lists in a word document,
2) the number of entries in each list,
3) the depth of each list

Example my work doc looks like

Things to do;

  • logon to aspose
    • need to get my password
  • enter something in forum
This is a second arbitrary list;
  • Hello World list two
I should get 2 lists, with list one having 3 entries, a depth of 2
list two has one entry, a depth of 1.

I have attached a java file that I have code that uses doc.getLists and iterates over the paragraphs, printing the list the paragraph belongs to. I have attached the console output from the run of the java file (it is a junit test). I have attached the word document I am using as input.

Any help is appreciated.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I tested your code and it seems that returned result is correct.

Regarding the following:

*** A paragraph belongs to list 2

list3 level 1 entry 1 (why does aspose show this as part of list 2?)

*** A paragraph belongs to list 2

list3 level 2 entry 1 (why does aspose show this as part of list 2?)

If you open your document using MS Words and click on an item of second list you will see that “list3 level 1 entry” and “list3 level 2 entry 1” are members of second list.

Best regards.

So in regard to the 1st of my 3 questions is there a way to determine the number of lists (actual lists, not templates or whatever you would call them) on a word document. For my example the answer should be 2 (as you pointed out). However, due to creating lists using certain styles and then removing them, the doc.getLists().getCount() does not return 2.
Would I need to figure that out on my own by iterating over the paragraphs, and counting for each distinct listid that a paragraph has for its’ list format when the paragraph is a list? Something like the below or is there an api in aspose that would return this info?

private void accumulateListStats(Document doc) throws Exception {
NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (int i = 0; i < paras.getCount(); i++)
{
Paragraph para = (Paragraph)paras.get(i);
if (para.getListFormat().isListItem())
{
Integer listId = para.getListFormat().getList().getListId();
listStatistics.append(MessageFormat.format("*** A paragraph belongs to list {0}", getListNumber(listId)) + “, list id for list is " + listId + “\n”);
listStatistics.append(”\t\t" + para.toTxt() + “\n”);
}
}
}

HashMap<Integer, Integer> listIdToListOccuranceNbr = new HashMap<Integer, Integer>();
private int getListNumber(Integer listId) {
Integer listOccurranceNumber = listIdToListOccuranceNbr.get(listId);
if (listOccurranceNumber == null) {
Collection occurNbrs = listIdToListOccuranceNbr.values();
listOccurranceNumber = Integer.valueOf(1);
if (occurNbrs != null && occurNbrs.size() > 0) {
for (Integer occurNbr:occurNbrs){
if (occurNbr != null && occurNbr.intValue() > listOccurranceNumber.intValue()) {
listOccurranceNumber = occurNbr;
}
}
listOccurranceNumber = Integer.valueOf(listOccurranceNumber.intValue() + 1);
}
listIdToListOccuranceNbr.put(listId, listOccurranceNumber);
}
return listOccurranceNumber;
}

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. No, there is no API to determine actual count of list inside document. Aspose.Words API allows determining count of defined lists in the template. So you should loop through Paragraphs and calculate lists count.

Best regards,