簡體   English   中英

itextsharp中的顏色和邊框設置

[英]colors and border setting in itextsharp

我正在嘗試為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));

我可以用這種編碼實現我想要的,但是我想問問有沒有一種更有效的方法來實現這一目標? 因為這些代碼僅對這個小東西來說太長了。

您可以使用for循環:

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

將參數放入數組或字典中,例如:

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

然后,您可以使用一個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