Created
June 10, 2025 10:13
-
-
Save bjoerntx/f1d3ea14a83aa81d3db2c812da26d408 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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