Last active
August 5, 2021 16:23
-
-
Save bjoerntx/a658568680b93e8b33ba852f9959f898 to your computer and use it in GitHub Desktop.
This file contains 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
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