Removing empty Pages

Cleaning up documents might be necessary when building documents dynamically. Various document sections are added, forced page breaks are inserted at different positions and end-users can modify the resulting document. A typical task is to remove all empty pages from a document that consist only of a page break or section break character.

The following method uses the PageEnumerator to loop through all pages. If the Length of a page equals 1, the page is removed.

[C#]
private void RemoveEmptyPages()
{ 
    TXTextControl.PageCollection.PageEnumerator pageEnum =
                    textControl1.GetPages().GetEnumerator();
    pageEnum.MoveNext();
    int pageCounter = textControl1.GetPages().Count;
    for (int i = 0; i < pageCounter; i++)
    {
        TXTextControl.Page curPage = (TXTextControl.Page)pageEnum.Current;
        if (curPage.Length == 1)
        {
            textControl1.Select(curPage.Start - 1, 1);
            textControl1.Selection.Text = "";
        }
        else
            pageEnum.MoveNext();
    }
}
[Visual Basic]
Private Sub RemoveEmptyPages()
    Dim pageEnum As TXTextControl.PageCollection.PageEnumerator =
                    textControl1.GetPages().GetEnumerator()
    pageEnum.MoveNext()
    Dim pageCounter As Integer = textControl1.GetPages().Count
    For i As Integer = 0 To pageCounter - 1
        Dim curPage As TXTextControl.Page =
                    DirectCast(pageEnum.Current, TXTextControl.Page)
        If curPage.Length = 1 Then
            textControl1.[Select](curPage.Start - 1, 1)
            textControl1.Selection.Text = ""
        Else
            pageEnum.MoveNext()
        End If
    Next
End Sub

<< Back