com.aspose.words

Class FieldCollection

  • java.lang.Object
    • com.aspose.words.FieldCollection
  • All Implemented Interfaces:
    java.lang.Iterable
    public class FieldCollection 
    extends java.lang.Object

A collection of Field objects that represents the fields in the specified range.
An instance of this collection contains fields which starts fall within the specified range.

Example:

Shows how to work with a document's field collection.
public void fieldCollection() throws Exception {
    // Open a document that has fields
    Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");

    // Get the collection that contains all the fields in a document
    FieldCollection fields = doc.getRange().getFields();
    Assert.assertEquals(fields.getCount(), 6);

    // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
    FieldVisitor fieldVisitor = new FieldVisitor();

    Iterator<Field> fieldEnumerator = fields.iterator();

    while (fieldEnumerator.hasNext()) {
        if (fieldEnumerator.next() != null) {
            Field currentField = fieldEnumerator.next();

            currentField.getStart().accept(fieldVisitor);
            if (currentField.getSeparator() != null) {
                currentField.getSeparator().accept(fieldVisitor);
            }
            currentField.getEnd().accept(fieldVisitor);
        } else {
            System.out.println("There are no fields in the document.");
        }
    }

    System.out.println(fieldVisitor.getText());

    // Get a field to remove itself
    fields.get(0).remove();
    Assert.assertEquals(fields.getCount(), 5);

    // Remove a field by reference
    Field lastField = fields.get(3);
    fields.remove(lastField);
    Assert.assertEquals(fields.getCount(), 4);

    // Remove a field by index
    fields.removeAt(2);
    Assert.assertEquals(fields.getCount(), 3);

    // Remove all fields from the document
    fields.clear();
    Assert.assertEquals(fields.getCount(), 0);
}

/// <summary>
/// Document visitor implementation that prints field info
/// </summary>
public static class FieldVisitor extends DocumentVisitor {
    public FieldVisitor() {
        mBuilder = new StringBuilder();
    }

    /// <summary>
    /// Gets the plain text of the document that was accumulated by the visitor.
    /// </summary>
    public String getText() {
        return mBuilder.toString();
    }

    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    public int visitFieldStart(final FieldStart fieldStart) {
        mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
        mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
        mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");

        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
        mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");

        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    public int visitFieldEnd(final FieldEnd fieldEnd) {
        mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");

        return VisitorAction.CONTINUE;
    }

    private StringBuilder mBuilder;
}

Property Getters/Setters Summary
intgetCount()
Returns the number of the fields in the collection.
Fieldget(int index)
Returns a field at the specified index.
 
Method Summary
voidclear()
Removes all fields of this collection from the document and from this collection itself.
java.util.Iterator<Field>iterator()
Returns an enumerator object.
voidremove(Field field)
Removes the specified field from this collection and from the document.
voidremoveAt(int index)
Removes a field at the specified index from this collection and from the document.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Returns the number of the fields in the collection.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }
      • get

        public Field get(int index)
        
        Returns a field at the specified 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.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }
    • Method Detail

      • clear

        public void clear()
                  throws java.lang.Exception
        Removes all fields of this collection from the document and from this collection itself.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }
      • iterator

        public java.util.Iterator<Field> iterator()
        Returns an enumerator object.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }
      • remove

        public void remove(Field field)
                   throws java.lang.Exception
        Removes the specified field from this collection and from the document.
        Parameters:
        field - A field to remove.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }
      • removeAt

        public void removeAt(int index)
                     throws java.lang.Exception
        Removes a field at the specified index from this collection and from the document.
        Parameters:
        index - An index into the collection.

        Example:

        Shows how to work with a document's field collection.
        public void fieldCollection() throws Exception {
            // Open a document that has fields
            Document doc = new Document(getMyDir() + "Document.ContainsFields.docx");
        
            // Get the collection that contains all the fields in a document
            FieldCollection fields = doc.getRange().getFields();
            Assert.assertEquals(fields.getCount(), 6);
        
            // Iterate over the field collection and print contents and type of every field using a custom visitor implementation
            FieldVisitor fieldVisitor = new FieldVisitor();
        
            Iterator<Field> fieldEnumerator = fields.iterator();
        
            while (fieldEnumerator.hasNext()) {
                if (fieldEnumerator.next() != null) {
                    Field currentField = fieldEnumerator.next();
        
                    currentField.getStart().accept(fieldVisitor);
                    if (currentField.getSeparator() != null) {
                        currentField.getSeparator().accept(fieldVisitor);
                    }
                    currentField.getEnd().accept(fieldVisitor);
                } else {
                    System.out.println("There are no fields in the document.");
                }
            }
        
            System.out.println(fieldVisitor.getText());
        
            // Get a field to remove itself
            fields.get(0).remove();
            Assert.assertEquals(fields.getCount(), 5);
        
            // Remove a field by reference
            Field lastField = fields.get(3);
            fields.remove(lastField);
            Assert.assertEquals(fields.getCount(), 4);
        
            // Remove a field by index
            fields.removeAt(2);
            Assert.assertEquals(fields.getCount(), 3);
        
            // Remove all fields from the document
            fields.clear();
            Assert.assertEquals(fields.getCount(), 0);
        }
        
        /// <summary>
        /// Document visitor implementation that prints field info
        /// </summary>
        public static class FieldVisitor extends DocumentVisitor {
            public FieldVisitor() {
                mBuilder = new StringBuilder();
            }
        
            /// <summary>
            /// Gets the plain text of the document that was accumulated by the visitor.
            /// </summary>
            public String getText() {
                return mBuilder.toString();
            }
        
            /// <summary>
            /// Called when a FieldStart node is encountered in the document.
            /// </summary>
            public int visitFieldStart(final FieldStart fieldStart) {
                mBuilder.append("Found field: " + fieldStart.getFieldType() + "\r\n");
                mBuilder.append("\tField code: " + fieldStart.getField().getFieldCode() + "\r\n");
                mBuilder.append("\tDisplayed as: " + fieldStart.getField().getResult() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldSeparator node is encountered in the document.
            /// </summary>
            public int visitFieldSeparator(final FieldSeparator fieldSeparator) {
                mBuilder.append("\tFound separator: " + fieldSeparator.getText() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            /// <summary>
            /// Called when a FieldEnd node is encountered in the document.
            /// </summary>
            public int visitFieldEnd(final FieldEnd fieldEnd) {
                mBuilder.append("End of field: " + fieldEnd.getFieldType() + "\r\n");
        
                return VisitorAction.CONTINUE;
            }
        
            private StringBuilder mBuilder;
        }