我正在使用 iText PDF 生成一个 PDF 文件,其中包含一个应按如下布局的表格:
------------------------------------------------
| HEADER |
------------------------------------------------
| some data goes here | more data here |
------------------------------------------------
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 |
------------------------------------------------
| 1 | SDF wer qwerwq | weqr | WERQW |
------------------------------------------------
| | | | |
| | | | |
| | | | |
| | | | |
------------------------------------------------
| footer information |
------------------------------------------------
然而,表格绘制如下:
------------------------------------------------
| HEADER |
------------------------------------------------
| some data goes here | more data here |
------------------------------------------------
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 |
------------------------------------------------
| 1 | SDF | wer | qwerwq | weqr | WERQW |
------------------------------------------------
| | | | |
------------------------------------------------
| | | | |
------------------------------------------------
| | | | |
------------------------------------------------
| | | | |
------------------------------------------------
| footer information |
------------------------------------------------
我已经尝试按照示例进行操作,但它们是用 Java 编写的,并且 C# 的对象模型似乎略有不同。“Col 1”值为 1 的行下方的行被拆分为第 2、3 和 4 列。
注意事项:
- 对于标题单元格,我通过调用 cell.SetHorizontalAlignment(HorizontalAlignment.CENTER) 设置水平对齐方式
- 我需要将一些文本的颜色设置为红色
- 我正在使用 table.AddCell 方法添加单元格
- 我将表格的边框(根据文档,这是默认单元格)设置为 Border.NO_BORDER。
- 这是一个用 C# 编写的 Web 应用程序
- 我已经下载了最新版本的 iText(版本 7.0.1)
- 我创建了一个自定义 CellRender,但这似乎没有效果。
- 最初我使用的是 iText 5,但我需要更好地控制表格的呈现,因为我需要知道我们到达的页面有多远。
这是我用来创建单元格的代码:
PdfFont cellFont = font; if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD && (fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) { cellFont = fontBoldItalic; } else if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD) { cellFont = fontBold; } else if ((fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) { cellFont = fontItalic; } Color fontColor = Color.BLACK; if ((fontStyle & FONT_STYLE_RED) == FONT_STYLE_RED) { fontColor = Color.RED; } Text text = new Text(content); text.SetFont(cellFont); text.SetFontColor(fontColor); text.SetFontSize(fontSize); if ((fontStyle & FONT_STYLE_UNDERLINE) == FONT_STYLE_UNDERLINE) { text.SetUnderline(); } Cell cell = new Cell(rowspan, colspan); cell.Add(new Paragraph(text)); //cell.SetNextRenderer(new CellBorders(cell, borders)); return cell;
这是创建表格并将表格添加到 web 方法末尾的文档的方式:
Table table = new Table(6);
table.SetWidthPercent(100);
table.SetPadding(3);
table.SetSpacingRatio(1);
table.SetBorder(Border.NO_BORDER);