单元格数据的itext颜色 [英] itext color for cell data

查看:241
本文介绍了单元格数据的itext颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itexsharp来创建PDF并拥有一个包含标题和行的表。我可以为我的标题添加颜色,但是当我为我的行(单元格)添加颜色时,我无法做到这一点。
如何为行添加颜色。

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.

示例:

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

代码:

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);
        }


推荐答案

对于标题行,你正在创建 PdfPCell 对象:

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);

对于其余行,您要求iTextSharp创建 PdfPCell 对象:

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

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

这是字符串 dt.Rows [intIndex] [date]。ToString()

iText将创建 PdfPCell 与为标题创建 PdfPCell 对象时的方法相同,因此一个选项是创建 PdfPCell 其他行的对象采用相同的方式:

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);

现在,您添加为字符串的每个单元格的背景颜色使用 addCell()方法将具有默认单元格的背景颜色。

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.

重要提示:我发现您使用的是6年前已过时的iTextSharp版本:您使用的是 Color 类,而不是 BaseColor 。请注意,该版本存在技术和法律问题。请升级到更新版本。

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.

这篇关于单元格数据的itext颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆