Aspose.Pdf

Achieve Minimum Column Width of a Table

Introduction

 

Aspose.Pdf tries to accommodate every smallest need of the developers to control PDF documents in almost all ways. One of the small yet powerful and handy feature of Aspose.Pdf is that it also allows developers to get the minimum column width for the Table . Developers don't need to calculate and set minimum column widths for the table columns by themselves because Aspose.Pdf is there to serve you.

 

How does it work?

 

When a Table has been created and data has been added (or imported) to it, developers can use the GetMinColumnWidth method of Table class. This method gets the minimum column width at which no hyphenation is needed for all cells in the column.

 

After you get the minimum column width by calling GetMinColumnWidth method, you can use SetColumnWidth method of Table class to adjust the column width to be minimized. You can simply pass the minimum column width obtained by GetMinColumnWidth method to the SetColumnWidth method of Table class. SetColumnWidth method is used to adjust column width after all cells have been added to the Table .

 

Code Snippet

 

[C#]

 

 

//Create a table object and add it to the paragraphs collection of the section       

Table tab1 = new Table(); 

sec1.Paragraphs.Add(tab1);

 

 

//Set the column widths and default cell border of the table

tab1.ColumnWidths = "60 100 100";

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

 

 

//Prepare an array of string values to be added to table

string[] darr = new string[]

        {"Owner/Marketing Assistant","dhasf hh ddt", "dhaosdha djsd dsads",

                "dsd dajd", "hdsah jj jj jdj", "ddfa jjj jhdusa"};

 

 

//Import the contents of the array created in above step

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

 

 

//Call GetMinColumnWidth and pass the column number whose minimum width is needed

float width = tab1.GetMinColumnWidth(0);

 

 

//Call SetColumnWidth and pass the column number with minimum width

tab1.SetColumnWidth(0,width);

 

 

[VB.NET]

 

 

'Create a table object and add it to the paragraphs collection of the section

Dim tab1 As Table = New Table()

sec1.Paragraphs.Add(tab1)

 

 

'Set the column widths and default cell border of the table

tab1.ColumnWidths = "60 100 100"

tab1.DefaultCellBorder = New BorderInfo(BorderSide.All)

 

 

'Prepare an array of string values to be added to table

Dim darr As String() = New String()

         {"Owner/Marketing Assistant", "dhasf hh ddt", "dhaosdha djsd dsads",

                 "dsd dajd", "hdsah jj jj jdj", "ddfa jjj jhdusa"}

 

 

'Import the contents of the array created in above step

tab1.ImportArray(darr, CType(0, Byte), CType(0, Byte), True)

 

 

'Call GetMinColumnWidth and pass the column number whose minimum width is needed

Dim width As Single = tab1.GetMinColumnWidth(0)

 

 

'Call SetColumnWidth and pass the column number with minimum width

tab1.SetColumnWidth(0, width)