表嵌套在itextsharp中的PDFPCELL中

时间:2015-10-14 12:17:21

标签: 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中,以便我可以间接地在该表格上实现边界...

你能帮忙解决这个问题吗?如果你不理解我的方法,那么问题......我会在评论部分更清楚地解释......

1 个答案:

答案 0 :(得分:5)

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);