Monday, May 19, 2008

iTextSharp table is deprecated use PdfPTable instead

I was looking today for the function TableFitsPage, because it wasn't working as i expected it would be. Google turned up with a few results, this was one of them:

http://sourceforge.net/mailarchive/message.php?msg_id=003b01c7e0b1%24ad8869a0%246801a8c0%40abraxas.local

This post says: "Use a PdfPTable instead of a Table. Table is not supported anymore"

I never heard of PdfPTable even in the tutorials the dont mention it. Think they should make it deprecated in the library so people will know it's not supported anymore. Anyways the example from my last article uses the Table class.

This is the same example but with PdfPTable:


protected void Page_Load(object sender, EventArgs e)
{
//Create document A4
Document document = new Document(PageSize.A4);
//Start the writer
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c:\\test.pdf", FileMode.Create));
//Open the document
document.Open();


#region Example2
//Start table with 2 columns
//Set the withs for the columns to 10% and 90%
PdfPTable table = new PdfPTable( new float[] { 10, 90 });
table.WidthPercentage = 100;

//Add first cell with colspan2
PdfPCell firstCell = new PdfPCell(new Phrase("Colspan Cell", FontFactory.GetFont(FontFactory.HELVETICA, 14, Color.BLACK)));
firstCell.Colspan = 2;
firstCell.VerticalAlignment = Element.ALIGN_TOP;
firstCell.BorderWidthBottom = 0.5f;
table.AddCell(firstCell);

//Add more cells
for (int i = 0; i < 10; i++)
{
PdfPCell moreCells = new PdfPCell(new Phrase("Cell " + i.ToString(), FontFactory.GetFont(FontFactory.HELVETICA_OBLIQUE, 12)));
moreCells.VerticalAlignment = Element.ALIGN_TOP;
moreCells.Colspan = 1;
moreCells.BorderWidth = 0.5f;
table.AddCell(moreCells);
}
//Add table to document
document.Add(table);
#endregion


//Close the document
document.Close();
}


Cell -> PdfPCell
Chunk -> Phrase
Table - > PdfPTable


Goodluck.

No comments: