com.aspose.words

Class RunCollection

  • All Implemented Interfaces:
    java.lang.Iterable
    public class RunCollection 
    extends NodeCollection

Provides typed access to a collection of Run nodes.

Property Getters/Setters Summary
intgetCount()
Gets the number of nodes in the collection.
Runget(int index)
Retrieves a Run at the given index.
 
Method Summary
voidadd(Node node)
Adds a node to the end of the collection.
voidclear()
Removes all nodes from this collection and from the document.
booleancontains(Node node)
Determines whether a node is in the collection.
intindexOf(Node node)
Returns the zero-based index of the specified node.
voidinsert(int index, Node node)
Inserts a node into the collection at the specified index.
java.util.Iterator<Node>iterator()
voidremove(Node node)
Removes the node from the collection and from the document.
voidremoveAt(int index)
Removes the node at the specified index from the collection and from the document.
Run[]toArray()
Copies all runs from the collection to a new array of runs.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the number of nodes in the collection.

        Example:

        Shows how to enumerate immediate children of a CompositeNode using indexed access.
        NodeCollection children = paragraph.getChildNodes();
        for (int i = 0; i < children.getCount(); i++)
        {
            Node child = children.get(i);
        
            // Paragraph may contain children of various types such as runs, shapes and so on.
            if (child.getNodeType() == NodeType.RUN)
            {
                // Say we found the node that we want, do something useful.
                Run run = (Run) child;
                System.out.println(run.getText());
            }
        }

        Example:

        Shows how to find out if a table contains another table or if the table itself is nested inside another table.
        public void calculateDepthOfNestedTables() throws Exception
        {
            Document doc = new Document(getMyDir() + "Table.NestedTables.doc");
            int tableIndex = 0;
        
            for (Table table : (Iterable<Table>) doc.getChildNodes(NodeType.TABLE, true))
            {
                // First lets find if any cells in the table have tables themselves as children.
                int count = getChildTableCount(table);
                System.out.println(MessageFormat.format("Table #{0} has {1} tables directly within its cells", tableIndex, count));
        
                // Now let's try the other way around, lets try find if the table is nested inside another table and at what depth.
                int tableDepth = getNestedDepthOfTable(table);
        
                if (tableDepth > 0)
                    System.out.println(MessageFormat.format("Table #{0} is nested inside another table at depth of {1}", tableIndex, tableDepth));
                else
                    System.out.println(MessageFormat.format("Table #{0} is a non nested table (is not a child of another table)", tableIndex));
        
                tableIndex++;
            }
        }
        
        /**
         * Calculates what level a table is nested inside other tables.
         *
         * @returns An integer containing the level the table is nested at.
         * 0 = Table is not nested inside any other table
         * 1 = Table is nested within one parent table
         * 2 = Table is nested within two parent tables etc..
         */
        private static int getNestedDepthOfTable(Table table)
        {
            int depth = 0;
        
            int type = table.getNodeType();
            // The parent of the table will be a Cell, instead attempt to find a grandparent that is of type Table
            Node parent = table.getAncestor(type);
        
            while (parent != null)
            {
                // Every time we find a table a level up we increase the depth counter and then try to find an
                // ancestor of type table from the parent.
                depth++;
                parent = parent.getAncestor(type);
            }
        
            return depth;
        }
        
        /**
         * Determines if a table contains any immediate child table within its cells.
         * Does not recursively traverse through those tables to check for further tables.
         *
         * @returns Returns true if at least one child cell contains a table.
         * Returns false if no cells in the table contains a table.
         */
        private static int getChildTableCount(Table table)
        {
            int tableCount = 0;
            // Iterate through all child rows in the table
            for (Row row : table.getRows())
            {
                // Iterate through all child cells in the row
                for (Cell Cell : row.getCells())
                {
                    // Retrieve the collection of child tables of this cell
                    TableCollection childTables = Cell.getTables();
        
                    // If this cell has a table as a child then return true
                    if (childTables.getCount() > 0) tableCount++;
                }
            }
        
            // No cell contains a table
            return tableCount;
        }
      • get

        public Run get(int index)
        
        Retrieves a Run at the given index.

        The index is zero-based.

        Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.

        If index is greater than or equal to the number of items in the list, this returns a null reference.

        If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.

        Parameters:
        index - An index into the collection.
    • Method Detail

      • add

        public void add(Node node)
        Adds a node to the end of the collection.

        The node is inserted as a child into the node object from which the collection was created.

        If the newChild is already in the tree, it is first removed.

        If the node being inserted was created from another document, you should use DocumentBase.importNode(com.aspose.words.Node,boolean,int) to import the node to the current document. The imported node can then be inserted into the current document.

        Parameters:
        node - The node to be added to the end of the collection.

        Example:

        Shows how to add a section to the end of the document.
        Document doc = new Document(getMyDir() + "Document.doc");
        Section sectionToAdd = new Section(doc);
        doc.getSections().add(sectionToAdd);
      • clear

        public void clear()
        Removes all nodes from this collection and from the document.

        Example:

        Shows how to remove all sections from a document.
        Document doc = new Document(getMyDir() + "Document.doc");
        doc.getSections().clear();
      • contains

        public boolean contains(Node node)
        Determines whether a node is in the collection.

        This method performs a linear search; therefore, the average execution time is proportional to Count.

        Parameters:
        node - The node to locate.
        Returns:
        True if item is found in the collection; otherwise, false.
      • indexOf

        public int indexOf(Node node)
        Returns the zero-based index of the specified node.

        This method performs a linear search; therefore, the average execution time is proportional to Count.

        Parameters:
        node - The node to locate.
        Returns:
        The zero-based index of the node within the collection, if found; otherwise, -1.

        Example:

        Shows how to iterate through all tables in the document and display the content from each cell.
        Document doc = new Document(getMyDir() + "Table.Document.doc");
        
        // Here we get all tables from the Document node. You can do this for any other composite node
        // which can contain block level nodes. For example you can retrieve tables from header or from a cell
        // containing another table (nested tables).
        NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
        
        // Iterate through all tables in the document
        for (Table table : (Iterable<Table>) tables)
        {
            // Get the index of the table node as contained in the parent node of the table
            int tableIndex = table.getParentNode().getChildNodes().indexOf(table);
            System.out.println(MessageFormat.format("Start of Table {0}", tableIndex));
        
            // Iterate through all rows in the table
            for (Row row : table.getRows())
            {
                int rowIndex = table.getRows().indexOf(row);
                System.out.println(MessageFormat.format("\tStart of Row {0}", rowIndex));
        
                // Iterate through all cells in the row
                for (Cell cell : row.getCells())
                {
                    int cellIndex = row.getCells().indexOf(cell);
                    // Get the plain text content of this cell.
                    String cellText = cell.toString(SaveFormat.TEXT).trim();
                    // Print the content of the cell.
                    System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", cellIndex, cellText));
                }
                //Console.WriteLine();
                System.out.println(MessageFormat.format("\tEnd of Row {0}", rowIndex));
            }
            System.out.println(MessageFormat.format("End of Table {0}", tableIndex));
            System.out.println();
        }

        Example:

        Retrieves the index of a table in the document.
        NodeCollection allTables = doc.getChildNodes(NodeType.TABLE, true);
        int tableIndex = allTables.indexOf(table);
      • insert

        public void insert(int index, Node node)
        Inserts a node into the collection at the specified index.

        The node is inserted as a child into the node object from which the collection was created.

        If the index is equal to or greater than Count, the node is added at the end of the collection.

        If the index is negative and its absolute value is greater than Count, the node is added at the end of the collection.

        If the newChild is already in the tree, it is first removed.

        If the node being inserted was created from another document, you should use DocumentBase.importNode(com.aspose.words.Node,boolean,int) to import the node to the current document. The imported node can then be inserted into the current document.

        Parameters:
        index - The zero-based index of the node. Negative indexes are allowed and indicate access from the back of the list. For example -1 means the last node, -2 means the second before last and so on.
        node - The node to insert.
      • iterator

        public java.util.Iterator<Node> iterator()
      • remove

        public void remove(Node node)
        Removes the node from the collection and from the document.
        Parameters:
        node - The node to remove.
      • removeAt

        public void removeAt(int index)
        Removes the node at the specified index from the collection and from the document.
        Parameters:
        index - The zero-based index of the node. Negative indexes are allowed and indicate access from the back of the list. For example -1 means the last node, -2 means the second before last and so on.

        Example:

        Shows how to add/remove sections in a document.
        // Open the document.
        Document doc = new Document(getMyDir() + "Section.AddRemove.doc");
        
        // This shows what is in the document originally. The document has two sections.
        System.out.println(doc.getText());
        
        // Delete the first section from the document
        doc.getSections().removeAt(0);
        
        // Duplicate the last section and append the copy to the end of the document.
        int lastSectionIdx = doc.getSections().getCount() - 1;
        Section newSection = doc.getSections().get(lastSectionIdx).deepClone();
        doc.getSections().add(newSection);
        
        // Check what the document contains after we changed it.
        System.out.println(doc.getText());
      • toArray

        public Run[] toArray()
        Copies all runs from the collection to a new array of runs.
        Returns:
        An array of runs.