C# 表嵌套在itextsharp中的PDFPCELL中

C# 表嵌套在itextsharp中的PDFPCELL中,c#,itextsharp,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

我想给一张桌子加上圆形边框,但经过研究,我发现这是不可能的,但我们可以给一个单元格加上圆形边框

所以我做了类似的事情

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中,这样我就可以间接地在那个表格上加上边框

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

用于
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);
//创建一个单列表
var outerTable=新的PdfPTable(1);
//为该外部表创建单个单元格
var outerCell=new PdfPCell();
//关闭该单元格的边框,因为我们将手动绘制边框
outerCell.Border=iTextSharp.text.Rectangle.NO_Border;
//绑定自定义类以绘制边界
outerCell.CellEvent=新的RoundRectangle();
//我们想用内桌做什么就做什么
var innerTable=新的PdfPTable(3);
对于(变量i=0;i<9;i++){
AddCell(“Hello”);
}
//完成后,将内部表格添加到外部单元格
附加元素(内表);
//将外部单元格添加到外部表中
outerTable.AddCell(outerCell);
//将外部表添加到文档中
文件添加(可外接);

@ ANKITHSARMA,如果它是对你的问题的一个很好的答案,考虑一下投票和接受它。@克里斯,我怎样才能强制内表来填充父单元?