com.aspose.words

Class SdtListItemCollection

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

Provides access to SdtListItem elements of a structured document tag.

Example:

Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
// Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
Document doc = new Document();
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
doc.getFirstSection().getBody().appendChild(tag);

// A drop down list needs elements, each of which will be a SdtListItem
SdtListItemCollection listItems = tag.getListItems();
listItems.add(new SdtListItem("Value 1"));

// Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
// When we initialize with one string, we are providing just the value
// Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());

// Add 3 more SdtListItems with non-empty strings passed to DisplayText
listItems.add(new SdtListItem("Item 2", "Value 2"));
listItems.add(new SdtListItem("Item 3", "Value 3"));
listItems.add(new SdtListItem("Item 4", "Value 4"));

// We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
// automatically have one of them pre-selected when we open the document in Microsoft Word
Assert.assertEquals(listItems.getCount(), 4);
listItems.setSelectedValue(listItems.get(3));

Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");

// We can enumerate over the collection and print each element
Iterator<SdtListItem> enumerator = listItems.iterator();
while (enumerator.hasNext()) {
    SdtListItem sdtListItem = enumerator.next();
    System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
}

// We can also remove elements one at a time
listItems.removeAt(3);
Assert.assertEquals(listItems.getCount(), 3);

// Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
listItems.setSelectedValue(listItems.get(1));

doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");

// We can clear the whole collection at once too
listItems.clear();
Assert.assertEquals(listItems.getCount(), 0);

Property Getters/Setters Summary
intgetCount()
Gets number of items in the collection.
SdtListItemgetSelectedValue()
void
           Specifies currently selected value in this list. Null value allowed, meaning that no currently selected entry is associated with this list item collection.
SdtListItemget(int index)
Returns a SdtListItem object given its zero-based index in the collection.
 
Method Summary
voidadd(SdtListItem item)
Adds an item to this collection.
voidclear()
Clears all items from this collection.
java.util.Iterator<SdtListItem>iterator()
Returns an iterator object that can be used to iterate over all items in the collection.
voidremoveAt(int index)
Removes a list item at the specified index.
 

    • Property Getters/Setters Detail

      • getCount

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

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
      • getSelectedValue/setSelectedValue

        public SdtListItem getSelectedValue() / public void setSelectedValue(SdtListItem value)
        
        Specifies currently selected value in this list. Null value allowed, meaning that no currently selected entry is associated with this list item collection.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
      • get

        public SdtListItem get(int index)
        
        Returns a SdtListItem object given its zero-based index in the collection.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
    • Method Detail

      • add

        public void add(SdtListItem item)
        Adds an item to this collection.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
      • clear

        public void clear()
        Clears all items from this collection.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
      • iterator

        public java.util.Iterator<SdtListItem> iterator()
        Returns an iterator object that can be used to iterate over all items in the collection.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);
      • removeAt

        public void removeAt(int index)
        Removes a list item at the specified index.
        Parameters:
        index - The zero-based index of the item to remove.

        Example:

        Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
        // Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
        Document doc = new Document();
        StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DROP_DOWN_LIST, MarkupLevel.BLOCK);
        doc.getFirstSection().getBody().appendChild(tag);
        
        // A drop down list needs elements, each of which will be a SdtListItem
        SdtListItemCollection listItems = tag.getListItems();
        listItems.add(new SdtListItem("Value 1"));
        
        // Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
        // When we initialize with one string, we are providing just the value
        // Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
        Assert.assertEquals(listItems.get(0).getValue(), listItems.get(0).getDisplayText());
        
        // Add 3 more SdtListItems with non-empty strings passed to DisplayText
        listItems.add(new SdtListItem("Item 2", "Value 2"));
        listItems.add(new SdtListItem("Item 3", "Value 3"));
        listItems.add(new SdtListItem("Item 4", "Value 4"));
        
        // We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
        // automatically have one of them pre-selected when we open the document in Microsoft Word
        Assert.assertEquals(listItems.getCount(), 4);
        listItems.setSelectedValue(listItems.get(3));
        
        Assert.assertEquals(listItems.getSelectedValue().getValue(), "Value 4");
        
        // We can enumerate over the collection and print each element
        Iterator<SdtListItem> enumerator = listItems.iterator();
        while (enumerator.hasNext()) {
            SdtListItem sdtListItem = enumerator.next();
            System.out.println(MessageFormat.format("List item: {0}, value: {1}", sdtListItem.getDisplayText(), sdtListItem.getValue()));
        }
        
        // We can also remove elements one at a time
        listItems.removeAt(3);
        Assert.assertEquals(listItems.getCount(), 3);
        
        // Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
        listItems.setSelectedValue(listItems.get(1));
        
        doc.save(getArtifactsDir() + "StructuredDocumentTag.ListItemCollection.docx");
        
        // We can clear the whole collection at once too
        listItems.clear();
        Assert.assertEquals(listItems.getCount(), 0);