如何设置第一行table五颜六色?
How to set the first row of table colorful?
我有一个 table,我想将其 header 行设置为灰色。
我尝试了下面的方法,但它给出了整个 table 为灰色
PdfPTable table1 = new PdfPTable(4);
table1.SetTotalWidth(new float[] { 50f,80f,50f,330f });
table1.TotalWidth = 800f;//table size
table1.LockedWidth = true;
table1.HorizontalAlignment = 0;
table1.SpacingBefore = 5f;//both are used to mention the space from heading
table1.SpacingAfter = 5f;
table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("NO", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Date & Day", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Hr", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Topics to be Covered", time515));
Doc.add(table1);
请看我预期的输出图像
你有几个选择。如果您只想在需要时更改给定行的背景颜色,您可以这样做。下面的代码将写入十行,每行四个单元格,第一行是灰色的,其余行是蓝色的。
for (var i = 0; i < 10; i++) {
if (i == 0) {
table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
}
else {
table1.DefaultCell.BackgroundColor = BaseColor.BLUE;
}
table1.AddCell(new Phrase("NO"));
table1.AddCell(new Phrase("Date & Day"));
table1.AddCell(new Phrase("Hr"));
table1.AddCell(new Phrase("Topics to be Covered"));
}
然而,iText 中的 tables 实际上支持更具体的 "Headers" 概念,您只需告诉 PdfPTable
有多少行应被视为 [=26] 即可声明这些=]:
table1.HeaderRows = 1;
巧妙之处在于,如果您的 table 实际上跨越多个页面,那么您的页眉将自动在下一页重新绘制。但是,使用此方法还需要执行一些额外的工作。 iText 公开接口供您实现名为 IPdfPTableEvent
的接口,它有一个名为 TableLayout
的方法。下面是一个完整的实现示例,请参阅代码注释以获取更多详细信息:
private class MyTableEvent : IPdfPTableEvent {
public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
//Loop through each header row
for( var row = 0; row < headerRows; row++ ){
//Loop through each column in the current row
//NOTE: For n columns there's actually n+1 entries in the widths array.
for( var col = 0; col < widths[row].Length - 1; col++ ){
//Get the various coordinates
var llx = widths[row][col];
var lly = heights[row];
var urx = widths[row][col + 1];
var ury = heights[row + 1];
//Create a rectangle
var rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);
//Set whatever properties you want on it
rect.BackgroundColor = BaseColor.PINK;
//Draw it to the background canvas
canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect);
}
}
}
}
要使用它,您只需将一个实例绑定到您的 table 对象:
table1.TableEvent = new MyTableEvent();
我有一个 table,我想将其 header 行设置为灰色。 我尝试了下面的方法,但它给出了整个 table 为灰色
PdfPTable table1 = new PdfPTable(4);
table1.SetTotalWidth(new float[] { 50f,80f,50f,330f });
table1.TotalWidth = 800f;//table size
table1.LockedWidth = true;
table1.HorizontalAlignment = 0;
table1.SpacingBefore = 5f;//both are used to mention the space from heading
table1.SpacingAfter = 5f;
table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("NO", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Date & Day", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Hr", time515));
table1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
table1.AddCell(new Phrase("Topics to be Covered", time515));
Doc.add(table1);
请看我预期的输出图像
你有几个选择。如果您只想在需要时更改给定行的背景颜色,您可以这样做。下面的代码将写入十行,每行四个单元格,第一行是灰色的,其余行是蓝色的。
for (var i = 0; i < 10; i++) {
if (i == 0) {
table1.DefaultCell.BackgroundColor = BaseColor.LIGHT_GRAY;
}
else {
table1.DefaultCell.BackgroundColor = BaseColor.BLUE;
}
table1.AddCell(new Phrase("NO"));
table1.AddCell(new Phrase("Date & Day"));
table1.AddCell(new Phrase("Hr"));
table1.AddCell(new Phrase("Topics to be Covered"));
}
然而,iText 中的 tables 实际上支持更具体的 "Headers" 概念,您只需告诉 PdfPTable
有多少行应被视为 [=26] 即可声明这些=]:
table1.HeaderRows = 1;
巧妙之处在于,如果您的 table 实际上跨越多个页面,那么您的页眉将自动在下一页重新绘制。但是,使用此方法还需要执行一些额外的工作。 iText 公开接口供您实现名为 IPdfPTableEvent
的接口,它有一个名为 TableLayout
的方法。下面是一个完整的实现示例,请参阅代码注释以获取更多详细信息:
private class MyTableEvent : IPdfPTableEvent {
public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
//Loop through each header row
for( var row = 0; row < headerRows; row++ ){
//Loop through each column in the current row
//NOTE: For n columns there's actually n+1 entries in the widths array.
for( var col = 0; col < widths[row].Length - 1; col++ ){
//Get the various coordinates
var llx = widths[row][col];
var lly = heights[row];
var urx = widths[row][col + 1];
var ury = heights[row + 1];
//Create a rectangle
var rect = new iTextSharp.text.Rectangle(llx, lly, urx, ury);
//Set whatever properties you want on it
rect.BackgroundColor = BaseColor.PINK;
//Draw it to the background canvas
canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect);
}
}
}
}
要使用它,您只需将一个实例绑定到您的 table 对象:
table1.TableEvent = new MyTableEvent();