Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jul 28, 2023 11:18 am

Greetings support team!

Today I have a question how to get the table that is placed somewhere in a document using a title of the table?
I can make no assumptions about where the table I used to find is placed in the document, except that the table is not placed within a comment.

Do I have to investigate all child objects of a document and for any child object iterate over their child objects (recursively)?
Here is my proposed solution:

Code: Select all
    private Table getTableFromPlaceholder(final Document document, final String placeholder) {
        Stack<DocumentObject> documentObjects = new Stack<>();
        documentObjects.addAll(asList(document.getChildObjects()));

        while (!documentObjects.isEmpty()) {
            DocumentObject documentObject = documentObjects.pop();
            if (documentObject instanceof Table) {
                Table table = (Table) documentObject;
                if (Objects.equals(table.getTitle(), placeholder)) {
                    return table;
                }
            }
            documentObjects.addAll(asList(documentObject.getChildObjects()));
        }

        return null;
    }


Is there another more efficient way?

Kind regards,
Marcus

mgattinger
 
Posts: 33
Joined: Fri May 21, 2021 9:03 am

Mon Jul 31, 2023 3:12 am

Hi Marcus,

Thanks for your inquiry.
To achieve this, you can utilize the "Document" object's section collection. Each "Section" object within the collection provides a method that allows you to access all the tables present in that section. By iterating through these table objects and comparing their titles, you can effectively locate the desired table. You may refer to the code snippet below as a reference:

Code: Select all
private Table getTableFromPlaceholder(final Document document, final String placeHolder) {
    // Loop through all sections
    SectionCollection sections = document.getSections();
    for (int i = 0; i < sections.getCount(); i++) {
        Section section = sections.get(i);
        // Get all tables and loop through them
        TableCollection tables = section.getTables();
        for (int j = 0; j < tables.getCount(); j++) {
            if (Objects.equals(placeHolder, tables.get(j).getTitle())) {
                return tables.get(j);
            }
        }
    }
    return null;
}


I hope this information helps you accomplish your goal. If you have any further questions or need additional assistance, please feel free to reach out to us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Mon Aug 14, 2023 9:32 am

Thanks for the code snippet. However, it will not work in case of nested tables, as
Code: Select all
section.getTables()
will provide only tables on the "first level". As I wrote, the table I have to find might be a nested table (or a nested table that itself contains a nested table).

So I guess the iteration method I've already posted is the only one that works for nested tables, too.

mgattinger
 
Posts: 33
Joined: Fri May 21, 2021 9:03 am

Tue Aug 15, 2023 3:53 am

Hi,

Thanks for your feedback.
If the table is nested, we need to iterate all tables and its child Objects and child’s child objects… to judge whether the found table is what we want. The method you posted is correct, I have prepared a similar method for your reference.
Code: Select all
public Table getTableFromPlaceholder(final Document document,final String placeHolder){
    // loop through all sections
    SectionCollection sections = document.getSections();
    for (int i = 0; i < sections.getCount(); i++) {
        Section section = sections.get(i);
        // get all tables and loop through them
        TableCollection tables = section.getTables();
        for (int j = 0; j < tables.getCount(); j++) {
            Table targetTable = getTargetTable(tables.get(j), placeHolder);
            if (!Objects.isNull(targetTable)) return targetTable;
        }
    }
    return null;
}
public Table getTargetTable(Table table,String placeholder){
    // judge if the found table is the target table
    if (Objects.equals(placeholder,table.getTitle())) return table;

    // if not, get rows-->get row-->get cells-->get cell
    RowCollection rows = table.getRows();
    for (int j = 0; j < rows.getCount(); j++) {
        TableRow tableRow = rows.get(j);
        CellCollection cells = tableRow.getCells();
        for (int k = 0; k < cells.getCount(); k++) {
            TableCell cell = cells.get(k);

            // get cell's child objects and judge its type
            DocumentObjectCollection cellChildObjects = cell.getChildObjects();
            for (int l = 0; l < cellChildObjects.getCount(); l++) {
                if (cellChildObjects.get(l).getDocumentObjectType() == DocumentObjectType.Table){
                    // if the type == table, recursive the process
                    Table targetTable = getTargetTable((Table) cellChildObjects.get(l), placeholder);
                    if (!Objects.isNull(targetTable)) return targetTable;
                }
            }
        }
    }
    return null;
}

Please feel free to modify this code according to your specific needs or continue using your previous implementation. If you have any further questions or require additional assistance, please don't hesitate to let me know.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc