Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Last active August 5, 2021 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/a658568680b93e8b33ba852f9959f898 to your computer and use it in GitHub Desktop.
Save bjoerntx/a658568680b93e8b33ba852f9959f898 to your computer and use it in GitHub Desktop.
public static class TableExtender {
public static void AdaptSize(this Table table, TextControl 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