Generate Table and set cell color using iTextSharp in ASP.Net

Last Reply 11 months ago By dharmendr

Posted 11 months ago

Hi friend,

i designed table using itextsharp.. but i need to set cell color individually with row by row.. i have attached sample code for reference.. how to split cells row by row? and set color?

        PdfPTable table = new PdfPTable(3);
        table.TotalWidth = 400f;
        //fix the absolute width of the table
        table.LockedWidth = true;
        //relative col widths in proportions - 1/3 and 2/3
        float[] widths = new float[] { 2f, 4f, 6f };
        table.SetWidths(widths);
        table.HorizontalAlignment = 0;
        //leave a gap before and after the table
        table.SpacingBefore = 20f;
        table.SpacingAfter = 30f;

        PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right    
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 2 Row 1");
        table.AddCell("Col 1 Row 2");
        table.AddCell("Col 2 Row 2");
        table.AddCell("Col 3 Row 2");

 

Posted 11 months ago

Hi sureshMGR,

Check this example. Now please take its reference and correct your code.

Namespaces

C#

using iTextSharp.text;
using iTextSharp.text.pdf;

VB.Net

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
        PdfPCell cell = null;
        PdfPTable table = null;

        document.Open();

        table = new PdfPTable(3);
        table.TotalWidth = 400f;
        table.LockedWidth = true;
        table.SetWidths(new float[] { 4f, 4f, 4f });
        table.HorizontalAlignment = 0;
        table.SpacingBefore = 20f;
        table.SpacingAfter = 30f;

        cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
        cell.Colspan = 3;
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.Red);
        cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right   
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("Col 1 Row 1"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
        table.AddCell(cell);
        cell = new PdfPCell(new Phrase("Col 2 Row 1"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray);
        table.AddCell(cell);
        cell = new PdfPCell(new Phrase("Col 3 Row 1"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen);
        table.AddCell(cell);

        cell = new PdfPCell(new Phrase("Col 1 Row 2"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
        table.AddCell(cell);
        cell = new PdfPCell(new Phrase("Col 2 Row 2"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.Gray);
        table.AddCell(cell);
        cell = new PdfPCell(new Phrase("Col 3 Row 2"));
        cell.BackgroundColor = new BaseColor(System.Drawing.Color.LimeGreen);
        table.AddCell(cell);

        document.Add(table);
        document.Close();
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf");
        Response.ContentType = "application/pdf";
        Response.Buffer = true;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite(bytes);
        Response.End();
        Response.Close();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim document As Document = New Document(PageSize.A4, 88.0F, 88.0F, 10.0F, 10.0F)
    Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
        Dim cell As PdfPCell = Nothing
        Dim table As PdfPTable = Nothing
        document.Open()
        table = New PdfPTable(3)
        table.TotalWidth = 400.0F
        table.LockedWidth = True
        table.SetWidths(New Single() {4.0F, 4.0F, 4.0F})
        table.HorizontalAlignment = 0
        table.SpacingBefore = 20.0F
        table.SpacingAfter = 30.0F
        cell = New PdfPCell(New Phrase("Header spanning 3 columns"))
        cell.Colspan = 3
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.Red)
        cell.HorizontalAlignment = 1
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 1 Row 1"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow)
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 2 Row 1"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray)
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 3 Row 1"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen)
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 1 Row 2"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.Yellow)
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 2 Row 2"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.Gray)
        table.AddCell(cell)
        cell = New PdfPCell(New Phrase("Col 3 Row 2"))
        cell.BackgroundColor = New BaseColor(System.Drawing.Color.LimeGreen)
        table.AddCell(cell)
        document.Add(table)
        document.Close()
        Dim bytes As Byte() = memoryStream.ToArray()
        memoryStream.Close()
        Response.Clear()
        Response.ContentType = "application/pdf"
        Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf")
        Response.ContentType = "application/pdf"
        Response.Buffer = True
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.BinaryWrite(bytes)
        Response.End()
        Response.Close()
    End Using
End Sub

Screenshot