Center a Table

Center a Table

Forum member · Sep 17, 2019
Hello, I have a table of fixed width and height with dynamic data being passed into it. Is there any way I can center this table dynamically? At the minute I'am simply eye balling what looks to be center on the page however if there was a better way of centering the table I would love to know. Thanks in advance!

Re: Center a Table

Support · Sep 17, 2019
Hello, Yes, you can dynamically calculate the X position to middle the table in the page. You can get the midpoint of the page and table by dividing the width of respective elements by 2. Then calculate the X position for the table using these values. Below is the code sample. Document document = new Document(); Page page = new Page(); document.Pages.Add(page); Table2 table = new Table2(0, 0, 210, 100); Column2 col1 = table.Columns.Add(100); Column2 col2 = table.Columns.Add(100); Row2 row1 = table.Rows.Add(); Cell2 cellR1C1 = row1.Cells.Add("Some text in R1C1"); Cell2 cellR1C2 = row1.Cells.Add("Some text in R1C2"); Row2 row2 = table.Rows.Add(); Cell2 cellR2C1 = row2.Cells.Add("Some text in R2C1"); Cell2 cellR2C2 = row2.Cells.Add("Some text in R2C2"); table.Height = table.GetRequiredHeight(); float pageMid = page.Dimensions.Body.Width / 2; float tableMd = table.Width/2; float xPos = pageMid - tableMd; table.X = xPos; page.Elements.Add(table); document.Draw(@"C:\Temp\Mydocument.pdf"); Thanks, ceTe Software Support Team