Inserting Columns programmatically using TX Text Control .NET

Generally, inserting page columns is a very easy task. Using the SectionFormat.Columns property, you can define the number of columns. TX Text Control will then automatically adjust the width of the columns based on the page size and number of columns.

But if the columns should have a different size and should not be averaged and spread out on the page, we need to set the width of each column. This can be done in the SectionFormat constructor:

[C#]
public SectionFormat(int columns, int[] columnWidths, int[] columnDistances);
[Visual Basic]
Public Sub New(ByVal columns As Integer, columnWidths As Integer(), columnDistances As Integer())

The second column width is calculated based on the first width, the distance between the columns, the page size and margins. This formula shows the calculation:

SecondColumnWidth = PageSize - PageMargins - FirstColumnWidth - ColumnDistance

The following code shows how to insert a new section with two columns where you just need to pass the first column width:

[C#]
private void InsertColumns(int FirstColumnWidth, int ColumnDistance)
{
    textControl1.Sections.Add(TXTextControl.SectionBreakKind.BeginAtNewLine);
    TXTextControl.SectionFormat currentFormat =
        textControl1.Sections.GetItem().Format;

    int iSecondColumnWidth = (int)(
        (currentFormat.PageSize.Width * 14.4) -
        (currentFormat.PageMargins.Left * 14.4) -
        (currentFormat.PageMargins.Right * 14.4) -
        FirstColumnWidth -
        ColumnDistance
        );

    TXTextControl.SectionFormat newSectionFormat =
        new TXTextControl.SectionFormat(
        2,
        new int[] { FirstColumnWidth, iSecondColumnWidth },
        new int[] { ColumnDistance });

    textControl1.Sections.GetItem().Format = newSectionFormat;
}
[Visual Basic]
Private Sub InsertColumns(FirstColumnWidth As Integer, ColumnDistance As Integer)
    textControl1.Sections.Add(TXTextControl.SectionBreakKind.BeginAtNewLine)
    Dim currentFormat As TXTextControl.SectionFormat = textControl1.Sections.GetItem().Format
    Dim iSecondColumnWidth As Integer = CInt(Math.Truncate(
            (currentFormat.PageSize.Width * 14.4) -
            (currentFormat.PageMargins.Left * 14.4) -
            (currentFormat.PageMargins.Right * 14.4) -
            FirstColumnWidth -
            ColumnDistance))

            Dim newSectionFormat As New TXTextControl.SectionFormat(
            2,
            New Integer() {FirstColumnWidth, iSecondColumnWidth},
            New Integer() {ColumnDistance})

    textControl1.Sections.GetItem().Format = newSectionFormat
End Sub

<< Back