com.aspose.words
Class VariableCollection

java.lang.Object
    extended by com.aspose.words.VariableCollection
All Implemented Interfaces:
java.lang.Iterable

public class VariableCollection 
extends java.lang.Object

A collection of document variables.

Variable names and values are strings.

Variable names are case-insensitive.


Property Getters/Setters Summary
intgetCount()
           Gets the number of elements contained in the collection.
java.lang.Stringget(int index)
voidset(int index, java.lang.String value)
           Gets or sets a document variable at the specified index. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.
java.lang.Stringget(java.lang.String name)
voidset(java.lang.String name, java.lang.String value)
           Gets or a sets a document variable by the case-insensitive name. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.
 
Method Summary
voidadd(java.lang.String name, java.lang.String value)
           Adds a document variable to the collection.
voidclear()
           Removes all elements from the collection.
booleancontains(java.lang.String name)
           Determines whether the collection contains a document variable with the given name.
intindexOfKey(java.lang.String name)
           Returns the zero-based index of the specified document variable in the collection.
java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>>iterator()
           Returns an enumerator object that can be used to iterate over all variable in the collection.
voidremove(java.lang.String name)
           Removes a document variable with the specified name from the collection.
voidremoveAt(int index)
           Removes a document variable at the specified index.
 

Property Getters/Setters Detail

getCount

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

Example:

Shows how to clear all document variables from a document.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

doc.getVariables().clear();
System.out.println(doc.getVariables().getCount()); // 0

get/set

public java.lang.String get(int index) / public void set(int index, java.lang.String value)
Gets or sets a document variable at the specified index. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.
Parameters:
index - Zero-based index of the document variable.

get/set

public java.lang.String get(java.lang.String name) / public void set(java.lang.String name, java.lang.String value)
Gets or a sets a document variable by the case-insensitive name. null values are not allowed as a right hand side of the assignment and will be replaced by empty string.

Method Detail

add

public void add(java.lang.String name, java.lang.String value)
Adds a document variable to the collection.
Parameters:
name - The case-insensitive name of the variable to add.
value - The value of the variable. The value cannot be null, if value is null empty string will be used instead.

Example:

Shows how to create document variables and add them to a document's variable collection.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Word processing document");
// Duplicate values can be stored but adding a duplicate name overwrites the old one
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

clear

public void clear()
Removes all elements from the collection.

Example:

Shows how to clear all document variables from a document.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

doc.getVariables().clear();
System.out.println(doc.getVariables().getCount()); // 0

contains

public boolean contains(java.lang.String name)
Determines whether the collection contains a document variable with the given name.
Parameters:
name - Case-insensitive name of the document variable to locate.
Returns:
True if item is found in the collection; otherwise, false.

Example:

Shows how to check if a collection of document variables contains a key.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");

System.out.println(doc.getVariables().contains("doc")); // True
System.out.println(doc.getVariables().contains("Word processing document")); // False

indexOfKey

public int indexOfKey(java.lang.String name)
Returns the zero-based index of the specified document variable in the collection.
Parameters:
name - The case-insensitive name of the variable.
Returns:
The zero based index. Negative value if not found.

Example:

Shows how to get the index of a key.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

System.out.println(doc.getVariables().indexOfKey("bmp")); // 0
System.out.println(doc.getVariables().indexOfKey("txt")); // 4

iterator

public java.util.Iterator<java.util.Map.Entry<java.lang.String, java.lang.String>> iterator()
Returns an enumerator object that can be used to iterate over all variable in the collection.

Example:

Shows how to obtain an enumerator from a collection of document variables and use it.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

Iterator enumerator = doc.getVariables().iterator();

while (enumerator.hasNext()) {
    Map.Entry de = (Map.Entry) enumerator.next();
    System.out.println(MessageFormat.format("Name: {0}, Value: {1}", de.getKey(), de.getValue()));
}

remove

public void remove(java.lang.String name)
Removes a document variable with the specified name from the collection.
Parameters:
name - The case-insensitive name of the variable.

Example:

Shows how to remove an element from a document's variable collection by key.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

doc.getVariables().remove("bmp");
System.out.println(doc.getVariables().getCount()); // 4

removeAt

public void removeAt(int index)
Removes a document variable at the specified index.
Parameters:
index - The zero based index.

Example:

Shows how to remove an element from a document's variable collection by index.
Document doc = new Document(getMyDir() + "Document.docx");

doc.getVariables().add("doc", "Word processing document");
doc.getVariables().add("docx", "Word processing document");
doc.getVariables().add("txt", "Plain text file");
doc.getVariables().add("bmp", "Image");
doc.getVariables().add("png", "Image");

int index = doc.getVariables().indexOfKey("bmp");
doc.getVariables().removeAt(index);
System.out.println(doc.getVariables().getCount()); // 4

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.