er ser
er ser

Reputation: 41

itext color for cell data

I am using itexsharp to create PDF and have a table with header and rows. I can give colors to my header, but when i add colors to my rows(cell) i cant do that. how can I give colors to my rows.

example:

 ..................
:Header1 : Header2 :  //I have a sytle here allreadey
 ...................
:Row1    : Row2    : //I want to add style here?
....................

code:

private String WritePDF(DataTable dt)
        {
            String fileName = "";  

            //Creating iTextSharp Table from the DataTable data
            PdfPTable pdfTable = new PdfPTable(m_PDFColumnCount);

            pdfTable.DefaultCell.Padding = 1;
            pdfTable.WidthPercentage = 100;
            pdfTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
           // pdfTable.DefaultCell.BorderWidth = 1;
           //194 214 155


            this.BuildPDFHeader(pdfTable, "date");
            this.BuildPDFHeader(pdfTable, "time");
            this.BuildPDFHeader(pdfTable, "result");
            this.BuildPDFHeader(pdfTable, "fullname");
            this.BuildPDFHeader(pdfTable, "regarding");            




            //Adding DataRow
                for (int intIndex = 0; intIndex < dt.Rows.Count; intIndex++)
                {

                    dt.Rows[intIndex]["details"] = getplaintext(dt.Rows[intIndex]["details"].ToString());


                  //Font verdana = FontFactory.GetFont("Verdana", 10, new Color(125, 88, 15));
                  //cell.BackgroundColor = new iTextSharp.text.Color(51, 102, 102);


                  pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());
                  pdfTable.AddCell(dt.Rows[intIndex]["time"].ToString());
                  pdfTable.AddCell(dt.Rows[intIndex]["result"].ToString());
                  pdfTable.AddCell(dt.Rows[intIndex]["fullname"].ToString());
                  pdfTable.AddCell(dt.Rows[intIndex]["regarding"].ToString());

                  PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["details"].ToString()));
                  cell.Colspan = 5;
                  pdfTable.AddCell(cell);


                }

            String folderPath = "C:\\PDFs\\"; //should be in configfile.

            fileName =  String.Format("{0}{1}{2}",folderPath, dt.Rows[0]["id"].ToString(),".pdf" );

            //Exporting to PDF

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate ))
            {
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);               
                PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                pdfDoc.Add(pdfTable);
                pdfDoc.Close();
                stream.Close();
            }

            return fileName;

        }

        private void BuildPDFHeader( PdfPTable pdfTable, String strText)
        {
              PdfPCell cell = new PdfPCell(new Phrase(strText)); 
                    cell.BackgroundColor = new iTextSharp.text.Color(51, 102,102);
                    pdfTable.AddCell(cell);
        }

Upvotes: 1

Views: 15535

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

For your header row, you are creating the PdfPCell objects yourself:

PdfPCell cell = new PdfPCell(new Phrase(strText)); 
cell.BackgroundColor = new iTextSharp.text.BaseColor(51, 102,102);

For the rest of the rows, you are asking iTextSharp to create the PdfPCell objects:

pdfTable.AddCell(dt.Rows[intIndex]["date"].ToString());

This is a string: dt.Rows[intIndex]["date"].ToString()

iText will create a PdfPCell the same way you did when creating the PdfPCell objects for the header, so one option is that you create the PdfPCell objects for the other rows the same way:

PdfPCell cell = new PdfPCell(new Phrase(dt.Rows[intIndex]["date"].ToString())); 
cell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);
pdfTable.AddCell(cell);

However, there is a short cut. You can define the background color for the default cell:

pdfTable.DefaultCell.BackgroundColor = new iTextSharp.text.BaseColor(60, 60, 60);

Now, the background color of each cell that you add as a string with the addCell() method will have the background color of the default cell.

Important: I see that you are using an obsolete version of iTextSharp that is 6 years old: you are using the Color class instead of BaseColor. Please be aware that there are technical and legal issues with that version. Please upgrade to a more recent version.

Upvotes: 4

Related Questions