Aspose.Pdf

Set Row and Column Format

Aspose.Pdf adds another flexible control over the Table format settings for developers. Sometimes, it's possible that developers would need to set text format settings for a particular Row or a column only. For example: Set the text in the first row to be bold or set the text in the last column of the Table to be right aligned.

 

To apply settings for a particular Row or a column of the Table , Aspose.Pdf provides DefaultCellTextInfo property in Row class and SetColumnTextInfo in Table class.

 

 

An example is given below that shows how to set the text format of rows and columns. In this example, an array is imported into a Table then the background color of the first row is set to blue and text in the last column is set to be right aligned.

 

Example:

 

[C#]

 

//Instantiate Pdf document object

Pdf pdf1 = new Pdf();

 

//Create a section in the Pdf

Section sec1 = pdf1.Sections.Add();

 

//Create a table

Table tab1 = new Table();

 

//Add table in the paragraphs collection of the section

sec1.Paragraphs.Add(tab1);

 

//set the column widths of the table

tab1.ColumnWidths = "60 100 100";

 

//Create a TextInfo instance

TextInfo tinfo = new TextInfo();

 

//Set the font name to "Courier" for the TextInfo object

tinfo.FontName = "Courier";

 

//Set default table border using the BorderInfo object

tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All,0.1F);

 

//Apply the text format settings in TextInfo object to table cells

tab1.DefaultCellTextInfo = tinfo;

 

//Create an array of double values

double[] darr = new Double[]{1.5,3.1415926,100000,20,4000,30.4512,45.67,890,23.45};

 

//Import the values in array to table

tab1.ImportArray(darr,0,0,false);

 

//Set background color for the first row

TextInfo tinfo1 = tinfo.Clone() as TextInfo;

tinfo1.BackgroundColor = new Color("#0000ff");

tab1.Rows[0].DefaultCellTextInfo = tinfo1;

 

//Align text in the last column to right hand side

TextInfo tinfo2 = tinfo.Clone() as TextInfo;

tinfo2.Alignment = AlignmentType.Right;

tab1.SetColumnTextInfo(2,tinfo2);

 

//We have to reset text format settings of the upper right cell

tinfo1.Alignment = AlignmentType.Right;

tab1.Rows[0].Cells[2].DefaultCellTextInfo = tinfo1;

 

[VB.NET]

 

'Instantiate Pdf document object

Dim pdf1 As Pdf = New Pdf()

 

'Create a section in the Pdf

Dim sec1 As Section = pdf1.Sections.Add()

 

'Create a table

Dim tab1 As Table = New Table()

 

'Add table in the paragraphs collection of the section

sec1.Paragraphs.Add(tab1)

 

'set the column widths of the table

tab1.ColumnWidths = "60 100 100"

 

'Create a TextInfo instance

Dim tinfo As TextInfo = New TextInfo()

 

'Set the font name to "Courier" for the TextInfo object

tinfo.FontName = "Courier"

 

'Set default table border using the BorderInfo object

tab1.DefaultCellBorder = New BorderInfo(CType(BorderSide.All, Integer), 0.1)

 

'Apply the text format settings in TextInfo object to table cells

tab1.DefaultCellTextInfo = tinfo

 

'Create an array of double values

Dim darr() As Double = New Double(){1.5,3.1415926,100000,20,4000,30.4512,45.67,890,23.45}

 

'Import the values in array to table

tab1.ImportArray(darr, 0, 0, False)

 

'Set background color for the first row

Dim tinfo1 As TextInfo = tinfo.Clone()

tinfo1.BackgroundColor = New Aspose.Pdf.Color("#0000ff")

tab1.Rows(0).DefaultCellTextInfo = tinfo1

 

'Align text in the last column to right hand side

Dim tinfo2 As TextInfo = tinfo.Clone()

tinfo2.Alignment = AlignmentType.Right

tab1.SetColumnTextInfo(2, tinfo2)

 

'We have to reset text format settings of the upper right cell

tinfo1.Alignment = AlignmentType.Right

tab1.Rows(0).Cells(2).DefaultCellTextInfo = tinfo1