c# - 如何为 iText 表中的选定行着色?
问题描述
我想根据 iText 表中的内容在行之间进行视觉区分。
我在这里找到了一个教程:,但它是用 Java 而不是 C# 编写的,我不知道它适用于哪个版本的 iText。
我正在使用 iText 7,它显然与以前的版本完全不同。
我尝试使用该文章中的这段代码:
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.SetBackgroundColor(Color.DARK_GRAY);
}
...但它甚至不会编译(Color 类没有 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());
}
}
...并且编译器在 cell.SetBackgroundColor(Color.headerBg) 上说“错误 CS0117 'Color' 不包含 'headerBg' 的定义” 线。
更新 2
我有一个无关的“颜色”。前面的“headerBg”导致它无法编译。删除它后,它使用以下代码编译:
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 类的 ColorConstants 类要简单得多,如下所示:
cell.SetBackgroundColor(ColorConstants.YELLOW);
推荐阅读
- ios - 防止 SwiftUI 动画覆盖嵌套的 withAnimation 块
- python - 如何根据数据框的行值作为子字符串/包含在另一个数据框的行值中来连接两个数据框?
- python - 删除所有子域并仅获取根域 pyspark
- python-3.x - Python Card & Deck OOP
- php - 在 api 调用的结果中匹配 Urls
- javascript - 裁剪图像 url 作为道具 React
- docker - 卷不适用于 Datapower 和 docker
- computational-geometry - 在 OpenMesh 中哪里存储 VertexHandle 的 id?
- c++ - 8 各种棋子的皇后象棋问题
- java - 当我将 fx:controller 添加到 fxml 文件时出现错误