共用方式為


如何:透過 Blocks 屬性管理非固定格式內容項目

這些範例示範可透過 Blocks 屬性在流程內容元素上執行的一些較常見的作業。 這個屬性是用來從 新增和移除專案 BlockCollection 。 具有 Blocks 屬性的 流程內容元素包括:

這些範例會當做流程內容元素使用 Section ,但這些技術適用于裝載流程內容元素集合的所有元素。

建立新的區段

下列範例會建立新的 Section ,然後使用 Add 方法將新的 Paragraph 新增至 Section 內容。

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...")))

建立新的 Paragraph 元素

下列範例會建立新的 Paragraph 專案,並將它插入 到 的 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)

取得區段中的最上層區塊元素

下列範例會取得 中包含的 Section 最上層 Block 專案數目。

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

刪除區段中的最後一個 Block 元素

下列範例會刪除 中的 Section 最後一個專案 Block

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

從區段清除所有 Block 元素內容

下列範例會清除 Section 中的所有內容( Block 專案)。

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

另請參閱