How to introduce rounded cells with a background color?

感情迁移 提交于 2019-12-11 09:35:55

问题


I have seen how to set rounded borders for a table cell in this question

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

But is it possible to make cell that will have no borders, but colored and rounded background?


回答1:


To achieve that, you need cell events. I've provided different examples in my book. See for instance calendar.pdf:

The Java code to create the white cells looks like this:

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

For the C# version of this code, go to Where do I find the C# examples? and click on the chapter that corresponds with the chapter of the Java version of the example.

For instance, 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();
  }
}

You can use this event like this:

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

Now the CellLayout() method will be executed the moment the cell is rendered to a page.



来源:https://stackoverflow.com/questions/26798850/how-to-introduce-rounded-cells-with-a-background-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!