Center a Table

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v10)  /  Center a Table

DynamicPDF CoreSuite for .NET (v10) Forum

 Sep 17 2019 10:32 AM
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!
 Sep 17 2019 12:52 PM
Posted by a ceTe Software moderator
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


All times are US Eastern Standard time. The time now is 9:42 PM.