【问题标题】:How to introduce rounded cells with a background color?如何引入带有背景颜色的圆形单元格?
【发布时间】:2014-11-07 10:13:18
【问题描述】:

我已经看到如何在这个问题中为表格单元格设置圆角边框

How to create a rounded corner table using iText\iTextSharp?

但是是否可以制作没有边框但有彩色和圆形背景的单元格?

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    为此,您需要cell events。我在书中提供了不同的例子。例如见calendar.pdf:

    创建白色单元格的 Java 代码如下所示:

    class CellBackground implements PdfPCellEvent {
    
        public void cellLayout(PdfPCell cell, Rectangle rect,
                PdfContentByte[] canvas) {
            PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
            cb.roundRectangle(
                rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
                rect.getHeight() - 3, 4);
            cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);
            cb.fill();
        }
    }
    

    此代码的C#版本,请前往Where do I find the C# examples?并点击与示例的Java版本章节对应的章节。

    例如PdfCalendar.cs:

    class CellBackground : IPdfPCellEvent {
      public void CellLayout(
        PdfPCell cell, Rectangle rect, PdfContentByte[] canvas
    ) {
        PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
        cb.RoundRectangle(
          rect.Left + 1.5f, 
          rect.Bottom + 1.5f, 
          rect.Width - 3,
          rect.Height - 3, 4
        );
        cb.SetCMYKColorFill(0x00, 0x00, 0x00, 0x00);
        cb.Fill();
      }
    }
    

    你可以这样使用这个事件:

    CellBackground cellBackground = new CellBackground();
    cell.CellEvent = cellBackground;
    

    现在CellLayout() 方法将在单元格呈现到页面的那一刻被执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2018-11-14
      • 2023-01-12
      • 2022-12-12
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多