Using Object Collections

Some context objects may be collections of other objects, like objects with IStringsCollection, IParagraphs, ITextBlocks, IAttributes etc. interfaces.

All objects that are collections of other objects have the following properties:

  • Count – Returns the number of items in a collection.
  • Item(index) – Returns the collection item with the specified index.

These properties can be used to access collection items and iterate through items using a for loop. Items can also be iterated using a foreach loop in scripting languages that support it.

Note.

  • An index always starts at 0 in all scripts.
  • In all languages except C#, you can use coll(index) instead of coll.Item(index), as Item is the default property.
  • In C#, the coll.Item(index) property cannot be accessed directly and coll[index] should be used instead.
  • In .NET languages, collections use the IEnumerable interface rather than IEnumerable<T>, so the type of the variable must be specified explicitly in foreach loops.

Below you can find sample code for working with the TextBlocks collection of the context object of a Separation script. Similar code can be used for other collections.

C#

for(int i = 0; i < page.TextBlocks.Count; i++){
   var block = page.TextBlocks[i]
}
// or
foreach (ITextBlock block in page.TextBlocks){
   // ...
}

Visual Basic .NET

For i As Integer = 0 To page.TextBlocks.Count-1
   Dim block = page.TextBlocks(i)
   ' or
   Dim block = page.TextBlocks.Item(i)
Next
' or
ForEach block As ITextBlock In page.TextBlocks
   ' ...
Next

JScript .NET

for(var i = 0; i < page.TextBlocks.Count; i++){
   var block = page.TextBlocks(i);
   // or
   var block = page.TextBlocks.Item(i);
}

JScript

for(var i = 0; i < this.TextBlocks.Count; i++){
   var block = this.TextBlocks(i);
   // or
   var block = this.TextBlocks.Item(i);
}

VBScript

Dim i, block
For i = 0 To Me.TextBlocks.Count-1
   Set block = Me.TextBlocks(i)
   ' or
   Set block = Me.TextBlocks.Item(i)
Next
' or
ForEach block In Me.TextBlocks
   ' ...
Next

29.08.2023 11:55:30

Please leave your feedback about this article

Usage of Cookies. In order to optimize the website functionality and improve your online experience ABBYY uses cookies. You agree to the usage of cookies when you continue using this site. Further details can be found in our Privacy Notice.