我想为表格提供圆角边框,但经过研究我发现它无法完成,但我们可以为单元格提供圆角边框..
所以我做了这样的事情
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/