14

How can I set cell width and height in itextsharp pdf cell ceration using c#. I just use

cell.width = 200f;

But it should display the error message.

width can not be set.

What should I do?..

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Fernando
  • 358
  • 3
  • 8
  • 21

5 Answers5

39

You don't set the width of a cell.

you should set the width of the columns. And you can do that by applying them on the table object:

float[] widths = new float[] { 1f, 2f };
table.SetWidths(widths);

The answer from Neha is to set the width of the table object

more reference material here: http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
14

http://indaravind.blogspot.in/2009/02/itextsharp-table-column-width.html

VB:

Dim intTblWidth() As Integer = {12, 10, 26, 10}

C#:

int[] intTblWidth = { 12, 10, 26, 10 };
Max
  • 12,622
  • 16
  • 73
  • 101
Neha
  • 2,933
  • 3
  • 19
  • 23
1
int  count=Gridview1.Columns.Count
PdfPTable table = new PdfPTable(count);
float[] columnWidths = new float[count];
for (int v = 0; v < count; v++)
{
    if (v == 0) { 
    columnWidths[v] = 10f;
    }
    else if (v == 2)
    {
        columnWidths[v] = 30f;
    }
    else if(v == 3)
    {
        columnWidths[v] = 15f;
    }
    else if(v == 4)
    {
        columnWidths[v] = 18f;
    }
    else if(v == 5|| v == 6|| v == 7)
    {
        columnWidths[v] = 22f;
    }
    else
    {
        columnWidths[v] = 20f;
    }
}

table.SetWidths(columnWidths);
mkl
  • 90,588
  • 15
  • 125
  • 265
  • 3
    Whilst this may or may not be the answer it would be beneficial to explain what you are doing here, for example what are all of these abitrary numbers that you're plucking out of nowhere. Is there a resource you used for this? Maybe you can link to it to be a little more helpful? – Mark Davies Jan 22 '20 at 11:59
  • I set column width in itextsharp pdf cell ceration using c#. I just use the above code it is helpful for dynamic columns – vijay sekhar reddy Jan 23 '20 at 05:55
0
#region Name..!!

            PdfPTable tblName = new PdfPTable(3);
            tblName.WidthPercentage = 98f;
            float[] colWidthsaccing4 = { 100, 500, 700 };
            tblName.SetWidths(colWidthsaccing4);
            PdfPCell celladdingo4;
            celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
            celladdingo4.HorizontalAlignment = 1;
            celladdingo4.BorderWidth = 0;
            celladdingo4.Colspan = 2;
            tblHeader6.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase("1.", TableFontmini_ARBold8Nor));
            celladdingo4.HorizontalAlignment = 1;
            
            celladdingo4.PaddingBottom = 5f;
            celladdingo4.BorderWidth = 0.5f;
            tblName.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase("  Name :", TableFontmini_ARBold8Nor));
            celladdingo4.HorizontalAlignment = 0;
            celladdingo4.PaddingBottom = 5f;
            celladdingo4.BorderWidth = 0.5f;
            tblName.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase(" " +dt.Rows[0]["EmpName"].ToString(), TableFontmini_ARBold8Nor));
            celladdingo4.HorizontalAlignment = 0;
            celladdingo4.PaddingBottom = 5f;
            celladdingo4.BorderWidth = 0.5f;
            tblName.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
            celladdingo4.HorizontalAlignment = 1;
            celladdingo4.BorderWidth = 0;
            celladdingo4.Colspan = 2;
            tblHeader6.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
            celladdingo4.HorizontalAlignment = 1;
            celladdingo4.BorderWidth = 0;
            celladdingo4.Colspan = 2;
            tblHeader6.AddCell(celladdingo4);

            celladdingo4 = new PdfPCell(new Phrase("  ", Smallspace));
            celladdingo4.HorizontalAlignment = 1;
            celladdingo4.BorderWidth = 0;
            celladdingo4.Colspan = 2;
            
            tblHeader6.AddCell(celladdingo4);


            doc.Add(tblName);

            #endregion
Code
  • 679
  • 5
  • 9
-3

cell.width = 200f; you have to put capital W on width correct is cell.Width = 200f;

IKavanagh
  • 6,089
  • 11
  • 42
  • 47