Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created June 10, 2025 10:13
Show Gist options
  • Save bjoerntx/f1d3ea14a83aa81d3db2c812da26d408 to your computer and use it in GitHub Desktop.
Save bjoerntx/f1d3ea14a83aa81d3db2c812da26d408 to your computer and use it in GitHub Desktop.
using TXTextControl;
public static class TableExtender
{
public static void AdaptSize(this Table table, ServerTextControl TextControl)
{
if (table == null)
return;
TextControl.PageUnit = MeasuringUnit.Twips;
// calculate the max available width
var maxWidth = TextControl.Sections.GetItem().Format.PageSize.Width -
TextControl.Sections.GetItem().Format.PageMargins.Left -
TextControl.Sections.GetItem().Format.PageMargins.Right;
// loop through all cells
for (int row = 1; row <= table.Rows.Count; row++)
{
// reset the calculated width
var currentWidth = 0;
// row by row
for (int col = 1; col <= table.Columns.Count; col++)
{
// calculate the complete width of the row
currentWidth += table.Cells[row, col].Width;
}
// calculate the missing gap
var destinationWidth = currentWidth - maxWidth;
// loop through all columns in current row
for (int col = 1; col <= table.Columns.Count; col++)
{
// calculate the ratio percentage for each cell
float percentage = (float)table.Cells[row, col].Width /
(float)currentWidth;
// resize the actual cell by it's calculated ratio
table.Cells[row, col].Width = (int)(table.Cells[row, col].Width -
(destinationWidth * percentage));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment