Table 嵌套到 itextsharp 中的 PDFPCELL
Table nested into PDFPCELL in itextsharp
我想为 table 提供圆角边框,但经过研究我发现它无法完成,但我们可以为单元格提供圆角边框..
所以我做了这样的事情
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);
现在我可以给单元格设置边框,所以我想做的是将我完整的嵌套 table 放入这个 pdfpcell 中,这样我就可以在那个 table 上间接获得边框。 ..
你能帮忙吗?如果你不明白我的方法..问问题..我会在评论部分解释得更清楚...
使用 kuujinbo's code verbatim 作为 RoundRectangle
class:
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();
}
}
然后你只需要一个 "outer" 和一个 "inner" table 并且只把 CellEvent
放在外面 table.
//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);
我想为 table 提供圆角边框,但经过研究我发现它无法完成,但我们可以为单元格提供圆角边框..
所以我做了这样的事情
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);
现在我可以给单元格设置边框,所以我想做的是将我完整的嵌套 table 放入这个 pdfpcell 中,这样我就可以在那个 table 上间接获得边框。 ..
你能帮忙吗?如果你不明白我的方法..问问题..我会在评论部分解释得更清楚...
使用 kuujinbo's code verbatim 作为 RoundRectangle
class:
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();
}
}
然后你只需要一个 "outer" 和一个 "inner" table 并且只把 CellEvent
放在外面 table.
//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);