How to: Manipulate Flow Content Elements through the Blocks Property

These examples demonstrate some of the more common operations that can be performed on flow content elements through the Blocks property. This property is used to add and remove items from BlockCollection. Flow content elements that feature a Blocks property include:

These examples happen to use Section as the flow content element, but these techniques are applicable to all elements that host a flow content element collection.

Create a new Section

The following example creates a new Section and then uses the Add method to add a new Paragraph to the Section contents.

Section secx = new Section();
secx.Blocks.Add(new Paragraph(new Run("A bit of text content...")));
Dim secx As New Section()
secx.Blocks.Add(New Paragraph(New Run("A bit of text content...")))

Create a new Paragraph element

The following example creates a new Paragraph element and inserts it at the beginning of the Section.

Paragraph parx = new Paragraph(new Run("Text to insert..."));
secx.Blocks.InsertBefore(secx.Blocks.FirstBlock, parx);
Dim parx As New Paragraph(New Run("Text to insert..."))
secx.Blocks.InsertBefore(secx.Blocks.FirstBlock, parx)

Get the top-level Block elements in the Section

The following example gets the number of top-level Block elements contained in the Section.

int countTopLevelBlocks = secx.Blocks.Count;
Dim countTopLevelBlocks As Integer = secx.Blocks.Count

Delete the last Block element in the Section

The following example deletes the last Block element in the Section.

secx.Blocks.Remove(secx.Blocks.LastBlock);
secx.Blocks.Remove(secx.Blocks.LastBlock)

Clear all the Block element content from the Section

The following example clears all of the contents (Block elements) from the Section.

secx.Blocks.Clear();
secx.Blocks.Clear()

See also