Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have write this code to generate pdf table using ITextSharp library..All work is done good but one problem occur the problem at this line..table.SetWidths(tblwidth); the error is this The best overload method for iTextSharp.text.Table.SetWidths(int[]).
iTextSharp.text.Table table = new iTextSharp.text.Table(dt.Columns.Count);
table.Cellpadding = 2;
table.Width = 100;
//Transfer rows from GridView to table

int[] widths = new int[dt.Columns.Count];

for (int i = 0; i < dt.Columns.Count; i++)
{

string cellText = Server.HtmlDecode(dt.Columns[i].ToString());
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);

float[] tblwidth = new float[3];
if (i == 0)
{
tblwidth[0] = 15f;
}
if (i == 1)
{


tblwidth[1] = 200f;
}
if (i == 2)
{

tblwidth[2] = 15f;

}


table.AddCell(cell);
table.SetWidths(tblwidth); error occur here

}
any one help me ????
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-14 16:30pm    
What part of error message could possibly unclear?
—SA

In will compile if you write
C#
table.SetWidths(new int[] { tblwidth, });

But it's likely that you don't understand what you are doing yourself. The array should represent not one width, but a set of width of the columns. Do you think you need a table with just one column? Why?

Please see: http://itextpdf.com/examples/iia.php?id=76[^].

—SA
 
Share this answer
 
Comments
LLLLGGGG 28-Sep-14 17:09pm    
I am actually a bit confused about this line (found in the link you have provided)

table.setWidths(new float[]{2, 1, 1});

The method actually needs a float array, but the compiler asks for an integer one... How is it possible?
Sergey Alexandrovich Kryukov 28-Sep-14 17:39pm    
If a compiler says it should be integer array, it should by such array. Look at the method signature.
—SA
LLLLGGGG 29-Sep-14 5:05am    
Actually, there are two overloads of that method... I do not understand why the compiler asks for an integer array.


 void setWidths(float[] relativeWidths)
          Sets the relative widths of the table.
 void setWidths(int[] relativeWidths)
          Sets the relative widths of the table.


Source: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPTable.html#setWidths(float[])

And in the code I would have added the cells inside the for loop and, since you need to set the widths once, I would have put the array declaration and the table.setwidths outside the loop...
Sergey Alexandrovich Kryukov 29-Sep-14 9:53am    
Maybe tblwidth is integer. But you can use any of the suitable methods, depending on what you want.
—SA
Hi,

TBLWidth is a float array. Change

float[] tblwidth = new float[3];

to

int[] tblwidth = new int[3];

and use integers (maybe with explicit conversions (int)) in the array!

The error occurs because the method accepts as parameter an integer array, not a float one.
If you have problems with float-int conversion put the operator (int) before any float value

I.E.
float f= whateveryouwant
int i = (int)f

You can use Math.Round if you need rounding the float value and not truncating it.
LG

PS: useful piece of advice for next questions: try to be more specific in writing the title, otherwise less people will watch them. :-)
 
Share this answer
 
v3
Comments
mudi lala 28-Sep-14 16:31pm    
but with int isnot working well
LLLLGGGG 28-Sep-14 16:45pm    
If you mean that with int you get some other error, try to convert values.

But as I could see, int is the only type accepted by the method. You have now two choices: either find another method in the same library which does the same thing with floats or write your personalized version of it...

Unfortunately, the method you have posted here does not work with floats value. It is a limitation of the method and you can do nothing with it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900