如何为 iText Table 中的选定行着色?
How to Colorize Selected Rows in an iText Table?
我想根据 iText 中的内容在行之间进行视觉区分 table。
我找到了一个教程 here:,但是它是 Java 而不是 C#,而且我不知道它适用于哪个版本的 iText。
我正在使用 iText 7,它显然与以前的版本完全不同。
我尝试使用那篇文章中的这段代码:
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.SetBackgroundColor(Color.DARK_GRAY);
}
...但它甚至不会编译(颜色 class 没有 DARK_GRAY 的定义)。
如果可能的话,我更愿意一次处理一行,但我认为不可能。
有人知道如何使用 iText 7 一次更改一行的背景颜色,或者一次更改一个单元格吗?
更新
我尝试将 Java 示例转换为 C#,但到目前为止还没有成功。这是我的:
Color headerBg = new DeviceRgb(0xA6, 0xCB, 0x0B);
Color lineColor = new DeviceRgb(0xCC, 0xCC, 0xCC);
. . .
foreach (PairODocs pod in lstPairODocs.OrderByDescending(a => a.Doc1Prcntg).ThenByDescending(a => a.Doc2Prcntg))
{
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(Color.headerBg);
table.AddCell(cell);
// TODO: If get it to work, add for the other four vals below, too...
}
else // these work, but are plain jane
{
table.AddCell(pod.Whirred);
table.AddCell(pod.Doc1Count.ToString());
table.AddCell(pod.Doc1Prcntg.ToString());
table.AddCell(pod.Doc2Count.ToString());
table.AddCell(pod.Doc2Prcntg.ToString());
}
}
...编译器说,"Error CS0117 'Color' does not contain a definition for 'headerBg'" 在 cell.SetBackgroundColor(Color.headerBg);行。
更新 2
我在 "headerBg" 之前有一个无关的 "Color." 导致它无法编译。删除它后,它使用以下代码编译:
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(headerBg);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Count.ToString()));
cell.SetBackgroundColor(headerBg);
table.AddCell(cell);
... // etc.
}
实际上,如果您愿意坚持使用一些基本颜色,使用 iText.Kernel.Color class' ColorConstants class 会简单得多:
cell.SetBackgroundColor(ColorConstants.YELLOW);
iText.Kernel.Colors.Color bgColour = new DeviceRgb(169, 169, 169); //Create Darkgray color
cell.SetBackgroundColor(bgColour); // Set the color we created to the cell
https://www.rapidtables.com/web/color/gray-color.html
在这里您可以找到多种颜色,只需选择您喜欢的颜色并编辑上面示例中的'169, 169, 169'。
我想根据 iText 中的内容在行之间进行视觉区分 table。
我找到了一个教程 here:,但是它是 Java 而不是 C#,而且我不知道它适用于哪个版本的 iText。
我正在使用 iText 7,它显然与以前的版本完全不同。
我尝试使用那篇文章中的这段代码:
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.SetBackgroundColor(Color.DARK_GRAY);
}
...但它甚至不会编译(颜色 class 没有 DARK_GRAY 的定义)。
如果可能的话,我更愿意一次处理一行,但我认为不可能。
有人知道如何使用 iText 7 一次更改一行的背景颜色,或者一次更改一个单元格吗?
更新
我尝试将
Color headerBg = new DeviceRgb(0xA6, 0xCB, 0x0B);
Color lineColor = new DeviceRgb(0xCC, 0xCC, 0xCC);
. . .
foreach (PairODocs pod in lstPairODocs.OrderByDescending(a => a.Doc1Prcntg).ThenByDescending(a => a.Doc2Prcntg))
{
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(Color.headerBg);
table.AddCell(cell);
// TODO: If get it to work, add for the other four vals below, too...
}
else // these work, but are plain jane
{
table.AddCell(pod.Whirred);
table.AddCell(pod.Doc1Count.ToString());
table.AddCell(pod.Doc1Prcntg.ToString());
table.AddCell(pod.Doc2Count.ToString());
table.AddCell(pod.Doc2Prcntg.ToString());
}
}
...编译器说,"Error CS0117 'Color' does not contain a definition for 'headerBg'" 在 cell.SetBackgroundColor(Color.headerBg);行。
更新 2
我在 "headerBg" 之前有一个无关的 "Color." 导致它无法编译。删除它后,它使用以下代码编译:
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(headerBg);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Count.ToString()));
cell.SetBackgroundColor(headerBg);
table.AddCell(cell);
... // etc.
}
实际上,如果您愿意坚持使用一些基本颜色,使用 iText.Kernel.Color class' ColorConstants class 会简单得多:
cell.SetBackgroundColor(ColorConstants.YELLOW);
iText.Kernel.Colors.Color bgColour = new DeviceRgb(169, 169, 169); //Create Darkgray color
cell.SetBackgroundColor(bgColour); // Set the color we created to the cell
https://www.rapidtables.com/web/color/gray-color.html 在这里您可以找到多种颜色,只需选择您喜欢的颜色并编辑上面示例中的'169, 169, 169'。