Problem
It's a common problem that whenever developers need to add a Table in the PDF document programmatically, they have to write many lines of code to set the format of each Cell in the Table . The most common practice by developers is to copy the lines of code for one cell and then paste for other cells to have the same format. But, it's still awful because if the number of cells are more, developers have to put more efforts. Moreover, this approach is error prone because a developer may make mistake while copy and paste. So, this approach puts more responsibility on the shoulders of a developer to be more careful.
Remedy by Aspose.Pdf
Aspose.Pdf provides the remedy for this problem. Using Aspose.Pdf , it is possible to set the default format for one Cell and then apply that format to all cells of the Table .
Some properties, which can be used to set default format settings for all cells in a Table , are described below:
Code Snippet
[C#]
//Create Table instance
Table tab1 = new Table();
//Add the table object in the paragraphs collection of the section
sec1.Paragraphs.Add(tab1);
//Set the column widths of the table
tab1.ColumnWidths = "50 50 50";
//Set default text color for the text contents of each cell in the table to blue
tab1.DefaultCellTextInfo.Color = new Aspose.Pdf.Color("Blue");
//Set default left side padding of the cell
tab1.DefaultCellPadding.Left = 5;
//Set default border of the cell using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All,0.1F);
//After setting default cell format information for the table, you can add rows
//and columns in the table
Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add("col3");
[VB.NET]
'Create Table instance
Dim tab1 As Table = New Table()
'Add the table object in the paragraphs collection of the section
sec1.Paragraphs.Add(tab1)
'Set the column widths of the table
tab1.ColumnWidths = "50 50 50"
'Set default text color for the text contents of each cell in the table to blue
tab1.DefaultCellTextInfo.Color = New Aspose.Pdf.Color("Blue")
'Set default left side padding of the cell
tab1.DefaultCellPadding.Left = 5
'Set default border of the cell using BorderInfo object
tab1.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.1F)
'After setting default cell format information for the table, you can add rows
'and columns in the table
Dim row1 As Row = tab1.Rows.Add()
row1.Cells.Add("col1")
row1.Cells.Add("col2")
row1.Cells.Add("col3")
[JAVA]
//Create Table instance
Table table = new Table(sec1);
//Add the table object in the paragraphs collection of the section
sec1.getParagraphs().add(table);
//Set with column widths of the table
table.setColumnWidths("50 50 50");
//Set default text color for the text contents of each cell in the table to blue
table.getDefaultCellTextInfo().setTextColor(Color.Blue);
//Set default left side padding of the cell
table.getDefaultCellPadding().setLeft(5);
//Set default border of the cell using BorderInfo object
table.setDefaultCellBorder( new BorderInfo(BorderSide.All, 0.1F));
//After setting default cell format information for the table, you can add rows
//and columns in the table
Row row1 = table.getRows().add();
row1.getCells().add("col1");
row1.getCells().add("col2");
row1.getCells().add("col3");
[XML]
<Table ColumnWidths="50 50 50" Color="Blue" DefaultCellPaddingLeft="5">
<DefaultCellBorder>
<All LineWidth="0.1"></All>
</DefaultCellBorder>
<Row>
<Cell>
<Text>
<Segment>col1</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col2</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col3</Segment>
</Text>
</Cell>
</Row>
</Table>