Free Support Forum - aspose.com

Table with different widths


#1

i am trying to create a table with two columns of unequal widths i.e 70% for one column and 30% for another column but am unable to do it in pdf, have tried columnWidth=“70,30” but not able to get required result


#2

@Sam212111

Thank you for contacting support.

You may set width of columns with ColumnWidths property which is exposed by Table class. Below code snippet adds a table of width 300 where the columns have width as 210 and 90. You may also visit Add and Extract a Table for your kind reference.

// Load source PDF document
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add();
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));

// Set column widths of the table
table.ColumnWidths = "210 90";

// Create a loop to add 5 rows
for (int row_count = 1; row_count < 5; row_count++)
{
    // Add row to table
    Aspose.Pdf.Row row = table.Rows.Add();
    // Add table cells
    row.Cells.Add("Cell (" + row_count + ", 1)");
    row.Cells.Add("Cell (" + row_count + ", 2)");
}
// Add table object to first page of input document
doc.Pages[1].Paragraphs.Add(table);
dataDir = dataDir + "document_with_table_19.5.pdf";
// Save updated document containing table object
doc.Save(dataDir);

We hope this will be helpful. please feel free to contact us if you need any further assistance.