我遇到以下情况:我必须使用 iTextSharp 在 PDF 页脚中创建圆角表格,但我发现执行此操作有些困难。
首先,我创建了一个名为 PdfHeaderFooter 的类,它扩展了 PdfPageEventHelper iTextSharp 接口(interface)。
在这个类中,我实现了 OnEndPage() 方法,该方法在所有页面的末尾创建页脚,这是我的代码:
// Write on end of each page
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
PdfPTable tabFot = new PdfPTable(new float[] { 1F });
tabFot.TotalWidth = 300F;
tabFot.DefaultCell.Border = PdfPCell.NO_BORDER;
tabFot.DefaultCell.CellEvent = new RoundedBorder();
PdfPCell cell;
cell = new PdfPCell(new Phrase("Footer"));
tabFot.AddCell(cell);
tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
}
正如您在这段代码中看到的,我创建了一个名为 TabFoot 的表,宽度为 300 像素,并且包含一列。我还将此表格单元格的单元格事件处理程序设置为 RoundBorder 对象。
这是我的 RoundBorder 类的代码:
class RoundedBorder : IPdfPCellEvent
{
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvas)
{
PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
cb.RoundRectangle(
rect.Left + 1.5f,
rect.Bottom + 1.5f,
rect.Width - 3,
rect.Height - 3, 4
);
cb.Stroke();
}
}
问题是我的程序工作并且生成了 PDF,但页脚中的表格没有圆角,而是经典的方角,我得到以下结果:
为什么?我缺少什么?我能做什么来解决?
Tnx
最佳答案
它不起作用的原因很简单。您正在为 DefaultCell
定义 NO_BORDER
值和单元格事件。如文档所述,当您添加单元格而不自行创建单元格时,将使用默认单元格的属性。例如:
table.AddCell("Test 1");
在您的情况下,您没有使用默认单元格,您正在创建自己的 PdfPCell
实例:
PdfPCell cell = new PdfPCell(new Phrase("Footer"));
这个cell
实例有它自己的属性。它不会查看您为 DefaultCell
定义的内容(否则将无法引入与默认值不同的属性)。因此你需要:
cell.Border = PdfPCell.NO_BORDER;
cell.CellEvent = new RoundedBorder();
现在,特定单元格cell
将仅具有圆形边框。
关于c# - 为什么我无法在此 iText\iTextSharp 页脚表格中获取圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652856/