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

Free Support Forum - aspose.com

List in Aspose

I use the following code to add a heading at the end of the document and add couple of lists under the heading.
When I read the document using aspose I find an extra paragraph at the end of the document, but if I replace line builder.writeln(“25 Dec 2018: Christmas updates”) to builder.write(“25 Dec 2018: Christmas updates”); then the extra paragraph disappears but the saved document does not has a item number for the list item. How can I get this to work?

Blockquote
builder.moveToDocumentEnd();
builder.insertParagraph(); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln(“Revision”); builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT); builder.getListFormat().setList(docCon.getLists().add(ListTemplate.NUMBER_ARABIC_DOT));
builder.getListFormat().setListLevelNumber(0);
builder.writeln(“15 Mar 2018: Made fun edits”);
builder.writeln(“25 Dec 2018: Christmas updates”);
builder.getListFormat().setList(null);

Thanks, my follow up question is that I have the following heading block that has a list. How can I find (programatically read) all the listed items under the heading (which I’m able to determine using the code you shared)
C. Revision //Heading 1

  1. 15 Mar 2018: Made fun edits
  2. 25 Dec 2018: Christmas updates

The above has been created using Aspose

Blockquote
builder.moveToDocumentEnd();
builder.insertParagraph();
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
builder.writeln(“Revision”);
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT); builder.getListFormat().setList(docCon.getLists().add(ListTemplate.NUMBER_ARABIC_DOT));
builder.getListFormat().setListLevelNumber(0);
builder.writeln(“15 Mar 2018: Made fun edits”);
builder.writeln(“25 Dec 2018: Christmas updates”);
builder.getListFormat().setList(null);

i can read it using the crude approach like the following, but these appear as separate paragraphs, I was expectng to get all the bullets under the heading Revision. What would be right way of doing that?

Blockquote
for (Paragraph para : (Iterable<Paragraph>) document.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1 ||
para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_2) {
String headingTitle = para.toString(SaveFormat.TEXT).trim();
System.out.println("HEADING: " + para.toString(SaveFormat.TEXT).trim());
if(heading.equals(headingTitle)){
headingFound = true;
}
} else if(para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.BODY_TEXT) {
System.out.println("BODY: " + para.toString(SaveFormat.TEXT).trim());
} else if (para.getListFormat().isListItem()){
System.out.println("LIST: " + para.toString(SaveFormat.TEXT).trim());
}
}

@aspose1212,

Please ZIP and upload your input Word documents (you want to extract list items from) here for testing. We will then investigate the issue on our end and provide you more information.

Happy to attach the document if needed, but the only question is how to I read all the list items under a particular section?

For e.g. given the following document

Heading 1-1

  • List item 1 in heading 1-1
  • List item 2 in heading 1-1

Heading 2-1

  • List item 1 in heading 2-1
  • List item 2 in heading 2-1

How do I read all lists under Heading 2-1?
I can iterate all document using this code but need to get hold of all listed items under a particular section:

Blockquote
private static void iterateDoc(Document document) throws Exception{
for (Paragraph para : (Iterable<Paragraph>) document.getChildNodes(NodeType.PARAGRAPH, true)) {
if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1) {
System.out.println("HEADING: " + para.toString(SaveFormat.TEXT).trim());
//how to find all the lists under a particular section?
} else if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.BODY_TEXT) {
System.out.println("BODY: " + para.toString(SaveFormat.TEXT).trim());
} else if (para.getListFormat().isListItem()){
System.out.println("LIST: " + para.toString(SaveFormat.TEXT).trim());
} else {
System.out.println(“ELSE: " + para.getParagraphFormat().getStyleIdentifier() + " ->” + para.toString(SaveFormat.TEXT).trim());
}
}
}

@aspose1212,

You can print lists in document by using the following code:

for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true)) {
    if (para.isListItem()){
        System.out.println(" This is list item --> " + para.toString(SaveFormat.TEXT));
    }
}

Hope, this helps.

Thanks, I was already able to print all the list items.

I however and trying to just print the list items under a given heading
So for example in the text below, how do I read all lists under just Heading 2-1?

Heading 1-1

  • List item 1 in heading 1-1
  • List item 2 in heading 1-1

Heading 2-1

  • List item 1 in heading 2-1
  • List item 2 in heading 2-1

How do I read all lists under Heading 2-1?

@aspose1212,

Please check this sample Word document (input.zip (10.7 KB)) and try running the following code:

Document doc = new Document("E:\\temp\\input.docx");

NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (int i =0; i< paras.getCount(); i++) {
    Paragraph para = (Paragraph) paras.get(i);
    if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_1) {
        Node currentNode = para.getNextSibling();
        boolean isContinue = true;
        while (currentNode != null && isContinue) {
            if (currentNode.getNodeType() == NodeType.PARAGRAPH) {
                Paragraph tempPara = (Paragraph) currentNode;
                if (tempPara.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_2) {
                    isContinue = false;
                    break;
                }
                if (tempPara.isListItem()) {
                    System.out.println(" This is list item --> " + para.toString(SaveFormat.TEXT));
                }
            }

            currentNode = currentNode.getNextSibling();
        }
        if (!isContinue)
            break;
    }
} 

Hope, this helps.

Thanks again for your prompt responses. This is exactly what I was looking for.