c# - 表格嵌套在 itextsharp 中的 PDFPCELL 中

标签 c# itextsharp

我想为表格提供圆角边框,但经过研究我发现它无法完成,但我们可以为单元格提供圆角边框..

所以我做了这样的事情

PdfPCell cell = new PdfPCell()
{
     CellEvent = rr, // rr is RoundRectangle object
     Border = PdfPCell.NO_BORDER,
     Padding = 4,
     Phrase = new Phrase("test")
};
table.AddCell(cell);
document.Add(table);

现在我可以给单元格添加边框,所以我想做的是将完整的嵌套表格放入这个 pdfpcell 中,这样我就可以在该表格上间接获得边框...

你能帮忙吗?如果您不理解我的方法..提出问题..我会在评论部分更清楚地解释...

最佳答案

使用 kuujinbo's code verbatim对于 RoundRectangle 类:

public class RoundRectangle : IPdfPCellEvent {
    public void CellLayout(
      PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvas
    ) {
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.RoundRectangle(
          rect.Left,
          rect.Bottom,
          rect.Width,
          rect.Height,
          4 // change to adjust how "round" corner is displayed
        );
        cb.SetLineWidth(1f);
        cb.SetCMYKColorStrokeF(0f, 0f, 0f, 1f);
        cb.Stroke();
    }
}

然后您只需要一个“外部”表和一个“内部”表,并且只将 CellEvent 放在外部表上。

//Create a one column table
var outerTable = new PdfPTable(1);

//Create a single cell for that outer table
var outerCell = new PdfPCell();

//Turn the border off for that cell because we're manually going to draw one
outerCell.Border = iTextSharp.text.Rectangle.NO_BORDER;

//Bind our custom class for drawing the borders
outerCell.CellEvent = new RoundRectangle();


//Do whatever we want with the inner table
var innerTable = new PdfPTable(3);
for (var i = 0; i < 9; i++) {
    innerTable.AddCell("Hello");
}

//When done, add the inner table to the outer cell
outerCell.AddElement(innerTable);

//Add the outer cell to the outer table
outerTable.AddCell(outerCell);

//Add the outer table to the document
doc.Add(outerTable);

关于c# - 表格嵌套在 itextsharp 中的 PDFPCELL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125071/

相关文章:

c# - 具有任意列和行的 Wpf DataGrid 数据绑定(bind)

c# - 在模板 WPF 中编辑滚动条的宽度/高度

c# - 如何在不使用编程语言的装箱支持的情况下将值设为 "box"?

c# - 使用文档哈希比较签名的 PDF 和未签名的 PDF

c# - Itextsharp - 检查添加元素是否会创建新页面

c# - iTextSharp XMLWorkerHelper 和用于 HTML 到 PDF 的图像

itextsharp - 如何调整字体大小以填充 iTextSharp 中固定高度的表格单元格

c# - 将新项目添加到导航属性会导致 "Collection navigation properties must > implement ICollection<> of the target type"错误

c# - 在循环内部或外部使用 unsafe 有什么区别吗?

c# - ITextSharp PDFTemplate FormFlattening 去除填充数据