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

Free Support Forum - aspose.com

Can I Extract a List from a Cell of a Table?

Hello,

I have been looking for the documentation and I have read this entry https://docs.aspose.com/display/wordsjava/Working+with+Lists.
But I only see the option of taking out the lists of the entire document. I need to get the list with its format, from within a cell of a table. Is there any way?

Thank you very much and greetings.

@josgobo,

You can meet this requirement by using the following code:

Table table = doc.getFirstSection().getBody().getTables().get(0);
Cell cell = table.getLastRow().getLastCell();

for (Paragraph para : (Iterable<Paragraph>) cell.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem())
    {
        ListFormat listFormat = para.getListFormat();
        // your further code
    }
}

A lot of thanks.

Actually I am trying to know the position of each part of the list.

For example in this list, I need to know something that tells me when the item is on another level or with another indentation.

• Item 1
1) List Item 1
2) List Item 2
3) List Item 3
4) List Item 4
• Item 2
• Item 3

I have this code:

// Iterate through all cells in the row
            for (int j = 0; j < cellNumber; j++) {
                Cell cell = row.getCells().get(j);

                Entry entry = new Entry();
                entry.setNamest("C0" + (j+1));

                NodeCollection cellChildNodeCollection = cell.getChildNodes();

                for(Object cellChildNode : cellChildNodeCollection){
                    if(cellChildNode instanceof Paragraph){
                        Paragraph paragraph = (Paragraph) cellChildNode;

                        if(paragraph.isListItem()){

                            //--- Pruebas Listas ---
                            String paraphText = paragraph.toString(SaveFormat.TEXT).trim();

                            ListFormat listFormat = paragraph.getListFormat();
                            com.aspose.words.List lisfFormatList = listFormat.getList();
                            int listLevelN = listFormat.getListLevelNumber();
                            com.aspose.words.ListLevel listLevel = listFormat.getListLevel();
                            double ident = listLevel.getTabPosition();
                            String listFormatString = listFormat.toString();

But nothing works.

Tanks and best regards!

@josgobo,

Please ZIP and attach your simplified input Word document here for testing. We will then investigate the scenario on our end and provide you more information.

Description_Table_With_Lists.zip (26.6 KB)

Here is my compressed word document.

A lot of thanks!

@josgobo

Thank you for sharing document. We are working on your query and will get back to you soon.

@josgobo

Thank you for patience. Please use the following code to get desired results. Hope, this helps.

try {

 String inputFile = "D:\\Temp\\Description_Table_With_Lists.docx";

 Document doc = new Document(inputFile);
 Table table = doc.getFirstSection().getBody().getTables().get(0);

 for (Row row: (Iterable < Row > ) table.getRows()) {
  for (Cell cell: (Iterable < Cell > ) row.getCells()) {
   for (Paragraph para: (Iterable < Paragraph > ) cell.getChildNodes(NodeType.PARAGRAPH, true)) {
    if (para.isListItem()) {
     ListFormat listFormat = para.getListFormat();
     ListLevel listLevel = listFormat.getListLevel();
     System.out.println("Number Position" + listLevel.getNumberPosition());
     System.out.println("Number Format" + listLevel.getNumberFormat());
     System.out.println("Number Style" + listLevel.getNumberStyle());
     System.out.println("Text Position" + listLevel.getTextPosition());

    }
   }

  }
 }
} catch (Exception e) {
 e.printStackTrace();
}