简体   繁体   English

itextsharp中的颜色和边框设置

[英]colors and border setting in itextsharp

I am trying to set the border and color for my pdf cells. 我正在尝试为pdf单元格设置边框和颜色。

PdfPCell[] headingcell = new PdfPCell[] {
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc_abc_abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
                new PdfPCell(new Phrase("abc", font9)),
            };

            headingcell[0].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[1].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[2].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[3].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[4].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[5].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[6].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[7].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[8].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[9].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[10].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[11].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[12].BackgroundColor = BaseColor.DARK_GRAY;
            headingcell[13].BackgroundColor = BaseColor.DARK_GRAY;

            headingcell[0].BorderWidth = 0;                
            headingcell[1].BorderWidth = 0;
            headingcell[2].BorderWidth = 0;
            headingcell[3].BorderWidth = 0;
            headingcell[4].BorderWidth = 0;
            headingcell[5].BorderWidth = 0;
            headingcell[6].BorderWidth = 0;
            headingcell[7].BorderWidth = 0;
            headingcell[8].BorderWidth = 0;
            headingcell[9].BorderWidth = 0;
            headingcell[10].BorderWidth = 0;
            headingcell[11].BorderWidth = 0;
            headingcell[12].BorderWidth = 0;
            headingcell[13].BorderWidth = 0;
            table.Rows.Add(new PdfPRow(headingcell));

I am able to achieve what I want with this coding, but I would like to ask is there a more efficient way to achieve this? 我可以用这种编码实现我想要的,但是我想问问有没有一种更有效的方法来实现这一目标? Because these codes is so long only for this small thing. 因为这些代码仅对这个小东西来说太长了。

You can use a for loop: 您可以使用for循环:

for (var i = 0; i <= 13; i++)
{
    headingcell[i].BackgroundColor = BaseColor.DARK_GRAY;
    headingcell[i].BorderWidth = 0;
}

Put your parameters into an array or an dictionary, for example: 将参数放入数组或字典中,例如:

      List<object[]> data= new List<object[]>
        {
            new object[] {"abc", "font9"},
            new object[] {"abc", "font9"},
            new object[] {"abc", "font9"},
            new object[] {"abc", "font9"},
            new object[] {"abc_abc_abc", "font9"},
            // etc...
        };

Then you can do all of your work with a single LINQ statement: 然后,您可以使用一个LINQ语句完成所有工作:

        PdfCell[] cells = data.Select(x =>
        {
           return new PdfCell(new Phrase(x[0], x[1]))
            {
                BackgroundColor = BaseColor.DARK_GRAY,
                BorderWidth = 0
            };
        }).ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM