不会为第一个单元格调用此事件
public class pdfCellBorder : IPdfPCellEvent
{
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.SetLineDash(0.5F, 2.2F, 0 );
canvas.Stroke();
}
}
评论代码:
PdfPTable table = new PdfPTable(1);
pdfCellBorder pdfcell = new pdfCellBorder();
table.DefaultCell.CellEvent = pdfcell;
table.DefaultCell.Border = PdfPCell.NO_BORDER;
PdfPCell cell = new PdfPCell(new Phrase("product"));
cell.Border = Rectangle.BOTTOM_BORDER;
cell.CellEvent = pdfcell;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("product"));
// cell.BorderWidthBottom = 1f;
cell.Border = Rectangle.BOTTOM_BORDER;
cell.CellEvent = pdfcell;
table.AddCell(cell);
public class pdfCellBorder : IPdfPCellEvent {
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
//canvas.SetLineWidth(0.5F);
canvas.SetLineDash(0.5F, 2.2F, 0);
canvas.Rectangle(position.Left, position.Bottom, position.Width, 0);
canvas.Stroke();
}
}
答案 0 :(得分:0)
你可以用两件事来解决这个问题。首先,在您的活动中设置实际Rectangle
:
public class pdfCellBorder : IPdfPCellEvent {
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.SetLineDash(0.5F, 2.2F, 0);
//Set the rectangle to draw on
canvas.Rectangle(position.Left, position.Bottom, position.Width, position.Height);
canvas.Stroke();
}
}
其次,禁用默认单元格上的边框:
var t = new PdfPTable(2);
t.DefaultCell.CellEvent = new pdfCellBorder();
//Disable the border
t.DefaultCell.Border = PdfPCell.NO_BORDER;
t.AddCell("Hello");
t.AddCell("World");
doc.Add(t);
修改强>
根据您的评论,您需要一条线,并且只需在每个单元格的底部绘制一条线。您只想使用Rectangle
和MoveTo
绘制一条线而不是LineTo
:
public class pdfCellBorder : IPdfPCellEvent {
public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle position, PdfContentByte[] canvases) {
PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
canvas.SetLineDash(0.5F, 2.2F, 0);
//Move the "pen" to where we want to start drawing
canvas.MoveTo(position.Left, position.Bottom);
//Draw a straight line
canvas.LineTo(position.Right, position.Bottom);
canvas.Stroke();
}
}
但是,您仍需要在所有单元格上禁用自动边框。如果你设置下面的任何边框,你指示iTextSharp为你绘制它们。此外,由于您致电new PdfPCell()
,我们可以删除与DefaultCell
PdfPTable table = new PdfPTable(1);
pdfCellBorder pdfcell = new pdfCellBorder();
PdfPCell cell = new PdfPCell(new Phrase("product"));
cell.Border = PdfPCell.NO_BORDER;
cell.CellEvent = pdfcell;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("product"));
cell.Border = PdfPCell.NO_BORDER;
cell.CellEvent = pdfcell;
table.AddCell(cell);