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 iterates fields which start fall within the specified range.

The FieldCollection collection does not own the fields it contains, rather, is just a selection of fields.

The FieldCollection collection is "live", i.e. changes to the children of the node object that it was created from are immediately reflected in the fields returned by the FieldCollection properties and methods.

Example:

Shows how to remove fields from a field collection.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
builder.insertField(" TIME ");
builder.insertField(" REVNUM ");
builder.insertField(" AUTHOR  \"John Doe\" ");
builder.insertField(" SUBJECT \"My Subject\" ");
builder.insertField(" QUOTE \"Hello world!\" ");
doc.updateFields();

// This collection stores all of a document's fields.
FieldCollection fields = doc.getRange().getFields();

Assert.assertEquals(6, fields.getCount());

// Below are four ways of removing fields from a field collection.
// 1 -  Get a field to remove itself:
fields.get(0).remove();
Assert.assertEquals(5, fields.getCount());

// 2 -  Get the collection to remove a field that we pass to its removal method:
Field lastField = fields.get(3);
fields.remove(lastField);
Assert.assertEquals(4, fields.getCount());

// 3 -  Remove a field from a collection at an index:
fields.removeAt(2);
Assert.assertEquals(3, fields.getCount());

// 4 -  Remove all the fields from the collection at once:
fields.clear();
Assert.assertEquals(0, fields.getCount());

Example:

Shows how to work with a collection of fields.
public void fieldCollection() throws Exception
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
    builder.insertField(" TIME ");
    builder.insertField(" REVNUM ");
    builder.insertField(" AUTHOR  \"John Doe\" ");
    builder.insertField(" SUBJECT \"My Subject\" ");
    builder.insertField(" QUOTE \"Hello world!\" ");
    doc.updateFields();

    // This collection stores all of a document's fields.
    FieldCollection fields = doc.getRange().getFields();

    Assert.assertEquals(6, fields.getCount());

    // 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());
}

/// <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 /*final*/ 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 remove fields from a field collection.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
        builder.insertField(" TIME ");
        builder.insertField(" REVNUM ");
        builder.insertField(" AUTHOR  \"John Doe\" ");
        builder.insertField(" SUBJECT \"My Subject\" ");
        builder.insertField(" QUOTE \"Hello world!\" ");
        doc.updateFields();
        
        // This collection stores all of a document's fields.
        FieldCollection fields = doc.getRange().getFields();
        
        Assert.assertEquals(6, fields.getCount());
        
        // Below are four ways of removing fields from a field collection.
        // 1 -  Get a field to remove itself:
        fields.get(0).remove();
        Assert.assertEquals(5, fields.getCount());
        
        // 2 -  Get the collection to remove a field that we pass to its removal method:
        Field lastField = fields.get(3);
        fields.remove(lastField);
        Assert.assertEquals(4, fields.getCount());
        
        // 3 -  Remove a field from a collection at an index:
        fields.removeAt(2);
        Assert.assertEquals(3, fields.getCount());
        
        // 4 -  Remove all the fields from the collection at once:
        fields.clear();
        Assert.assertEquals(0, fields.getCount());

        Example:

        Shows how to work with a collection of fields.
        public void fieldCollection() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
            builder.insertField(" TIME ");
            builder.insertField(" REVNUM ");
            builder.insertField(" AUTHOR  \"John Doe\" ");
            builder.insertField(" SUBJECT \"My Subject\" ");
            builder.insertField(" QUOTE \"Hello world!\" ");
            doc.updateFields();
        
            // This collection stores all of a document's fields.
            FieldCollection fields = doc.getRange().getFields();
        
            Assert.assertEquals(6, fields.getCount());
        
            // 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());
        }
        
        /// <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 /*final*/ 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 remove fields from a field collection.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
        builder.insertField(" TIME ");
        builder.insertField(" REVNUM ");
        builder.insertField(" AUTHOR  \"John Doe\" ");
        builder.insertField(" SUBJECT \"My Subject\" ");
        builder.insertField(" QUOTE \"Hello world!\" ");
        doc.updateFields();
        
        // This collection stores all of a document's fields.
        FieldCollection fields = doc.getRange().getFields();
        
        Assert.assertEquals(6, fields.getCount());
        
        // Below are four ways of removing fields from a field collection.
        // 1 -  Get a field to remove itself:
        fields.get(0).remove();
        Assert.assertEquals(5, fields.getCount());
        
        // 2 -  Get the collection to remove a field that we pass to its removal method:
        Field lastField = fields.get(3);
        fields.remove(lastField);
        Assert.assertEquals(4, fields.getCount());
        
        // 3 -  Remove a field from a collection at an index:
        fields.removeAt(2);
        Assert.assertEquals(3, fields.getCount());
        
        // 4 -  Remove all the fields from the collection at once:
        fields.clear();
        Assert.assertEquals(0, fields.getCount());
    • 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 remove fields from a field collection.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
        builder.insertField(" TIME ");
        builder.insertField(" REVNUM ");
        builder.insertField(" AUTHOR  \"John Doe\" ");
        builder.insertField(" SUBJECT \"My Subject\" ");
        builder.insertField(" QUOTE \"Hello world!\" ");
        doc.updateFields();
        
        // This collection stores all of a document's fields.
        FieldCollection fields = doc.getRange().getFields();
        
        Assert.assertEquals(6, fields.getCount());
        
        // Below are four ways of removing fields from a field collection.
        // 1 -  Get a field to remove itself:
        fields.get(0).remove();
        Assert.assertEquals(5, fields.getCount());
        
        // 2 -  Get the collection to remove a field that we pass to its removal method:
        Field lastField = fields.get(3);
        fields.remove(lastField);
        Assert.assertEquals(4, fields.getCount());
        
        // 3 -  Remove a field from a collection at an index:
        fields.removeAt(2);
        Assert.assertEquals(3, fields.getCount());
        
        // 4 -  Remove all the fields from the collection at once:
        fields.clear();
        Assert.assertEquals(0, fields.getCount());
      • iterator

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

        Example:

        Shows how to work with a collection of fields.
        public void fieldCollection() throws Exception
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
            builder.insertField(" TIME ");
            builder.insertField(" REVNUM ");
            builder.insertField(" AUTHOR  \"John Doe\" ");
            builder.insertField(" SUBJECT \"My Subject\" ");
            builder.insertField(" QUOTE \"Hello world!\" ");
            doc.updateFields();
        
            // This collection stores all of a document's fields.
            FieldCollection fields = doc.getRange().getFields();
        
            Assert.assertEquals(6, fields.getCount());
        
            // 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());
        }
        
        /// <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 /*final*/ 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 remove fields from a field collection.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
        builder.insertField(" TIME ");
        builder.insertField(" REVNUM ");
        builder.insertField(" AUTHOR  \"John Doe\" ");
        builder.insertField(" SUBJECT \"My Subject\" ");
        builder.insertField(" QUOTE \"Hello world!\" ");
        doc.updateFields();
        
        // This collection stores all of a document's fields.
        FieldCollection fields = doc.getRange().getFields();
        
        Assert.assertEquals(6, fields.getCount());
        
        // Below are four ways of removing fields from a field collection.
        // 1 -  Get a field to remove itself:
        fields.get(0).remove();
        Assert.assertEquals(5, fields.getCount());
        
        // 2 -  Get the collection to remove a field that we pass to its removal method:
        Field lastField = fields.get(3);
        fields.remove(lastField);
        Assert.assertEquals(4, fields.getCount());
        
        // 3 -  Remove a field from a collection at an index:
        fields.removeAt(2);
        Assert.assertEquals(3, fields.getCount());
        
        // 4 -  Remove all the fields from the collection at once:
        fields.clear();
        Assert.assertEquals(0, fields.getCount());
      • 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 remove fields from a field collection.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        builder.insertField(" DATE \\@ \"dddd, d MMMM yyyy\" ");
        builder.insertField(" TIME ");
        builder.insertField(" REVNUM ");
        builder.insertField(" AUTHOR  \"John Doe\" ");
        builder.insertField(" SUBJECT \"My Subject\" ");
        builder.insertField(" QUOTE \"Hello world!\" ");
        doc.updateFields();
        
        // This collection stores all of a document's fields.
        FieldCollection fields = doc.getRange().getFields();
        
        Assert.assertEquals(6, fields.getCount());
        
        // Below are four ways of removing fields from a field collection.
        // 1 -  Get a field to remove itself:
        fields.get(0).remove();
        Assert.assertEquals(5, fields.getCount());
        
        // 2 -  Get the collection to remove a field that we pass to its removal method:
        Field lastField = fields.get(3);
        fields.remove(lastField);
        Assert.assertEquals(4, fields.getCount());
        
        // 3 -  Remove a field from a collection at an index:
        fields.removeAt(2);
        Assert.assertEquals(3, fields.getCount());
        
        // 4 -  Remove all the fields from the collection at once:
        fields.clear();
        Assert.assertEquals(0, fields.getCount());