Search code examples
dynamicpdfdynamicpdf-table2

Does the width of a Table2 or a Cell2 include the border? For a Cell2, does it include the padding?


I'm working with the DynamicPDF library to typeset tabular data and I have an incomplete understanding of how the width property of Table2 and Cell2 objects works. Specifically, I don't know whether the width set by the property is an internal width, or an external one. For a Table2 object, I don't know whether it includes the left and right borders of the table. For a Cell2 object, I don't know whether it includes the left and right borders or whether it includes the left and right padding. For that matter, I have an incomplete understanding of why we are permitted to set the width of the table at all - surely the width follows logically from the widths of the table columns, in which case the proper way to modify the table's width is by modifying the column widths?

It would be useful to know whether the table width in particular includes its borders, because I want to typeset tables that are the width of the page (or rather, the width of the area inside the page margins). If the Table2's width propery includes the border width, then the correct way to do this is to set the width to the page width. If not, then the correct way is to subtract the left and right border widths first. If I do it wrong, then the table will be either too wide or too narrow.

These pages seem to be up-to-date documentation, but they don't specify whether the borders/padding are included (they just say "Gets or sets the width of the table." and "Gets the width of the cell.") This page has examples of use of the Table2 class, but all of them use hard-coded widths.


Solution

  • The table width is independent of column widths. Table width cannot be modified by modifying the column widths. When generating the tables with dynamic data, setting table width allows you to specify a region in which the content is rendered and anything that falls outside of these boundaries is overflowed into a new table. For example if the sum of the column widths is greater than the table width, the columns that do not fall within the table boundaries are pushed out into the overflow table which can be accessed using GetOverflowColumns().

    Border width is not included in the table width. On the other hand, cell width includes the cell border width and padding of the cell. Here is the code that demonstrates how different widths are set.

    Document document = new Document();
    Page page = new Page(); 
    
    Table2 table = new Table2(0, 0, 300, 500);
    table.Border.Width = 5;
    table.Border.Color = RgbColor.Red;
    table.Columns.Add(100);
    table.Columns.Add(100);
    table.Columns.Add(100);
    
    Row2 row = table.Rows.Add(); 
    
    Cell2 cell = row.Cells.Add("Cell 1");
    cell.Padding = 5;
    cell.Border.Width = 5;
    cell.Border.Color = RgbColor.Blue;
    
    Cell2 cell2 = row.Cells.Add("Cell 2");
    cell2.Padding = 5;
    cell2.Border.Width = 5;
    cell2.Border.Color = RgbColor.Green;
    
    Cell2 cell3 = row.Cells.Add("Cell 3");
    cell3.Padding = 5;
    cell3.Border.Width = 5;
    cell3.Border.Color = RgbColor.DarkOrange;
    
    page.Elements.Add(table);
    
    page.Elements.Add(new LayoutGrid());
    page.Elements.Add(new Label("Table Width: " + table.Width.ToString(), 0, 50, 200, 20));
    
    document.Pages.Add(page);
    
    document.Draw("Table2.pdf");
    

    Disclaimer: I work for ceTe Software, the company that develops DynamicPDF libraries.