Why I can't obtain rounded table corners in this iText\iTextSharp footer table?

拟墨画扇 提交于 2019-12-24 14:13:11

问题


I have the following situation: I have to create a round corner table into the footer of my PDF using iTextSharp but I am finding some difficulties to do it.

First of all I have create a class named PdfHeaderFooter that extends the PdfPageEventHelper iTextSharp interface.

In this class I have implemented the OnEndPage() method that create the footer on the end of all pages, this is my code:

    // Write on end of each page
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        PdfPTable tabFot = new PdfPTable(new float[] { 1F });
        tabFot.TotalWidth = 300F;

        tabFot.DefaultCell.Border = PdfPCell.NO_BORDER;
        tabFot.DefaultCell.CellEvent = new RoundedBorder();

        PdfPCell cell;
        cell = new PdfPCell(new Phrase("Footer"));
        tabFot.AddCell(cell);

        tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent);
    }

As you can see in this code I create a table named TabFoot that is 300px wisth and that contains a single column. I also setted the cell event handler for this table cells as a RoundBorder object.

And this is the code of my RoundBorder class:

class RoundedBorder : IPdfPCellEvent
{
    public void CellLayout(PdfPCell cell, iTextSharp.text.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.Stroke();
    }
}

The problem is that my program work and the PDF is generated but the table in the footer have not the rounded corners but the classic square corners, I obtain this result:

Why? What am I missing? what can I do to solve?

Tnx


回答1:


The reason why it doesn't work is very simple. You are defining the NO_BORDER value and the cell event for the DefaultCell. As documented, the properties of the default cell are used when you add a cell without creating a cell yourself. For instance:

table.AddCell("Test 1");

In your case, you are not using the default cell, you are creating your own PdfPCell instance:

PdfPCell cell = new PdfPCell(new Phrase("Footer"));

This cell instance has its properties of its own. It doesn't look at what you defined for the DefaultCell (otherwise there would be no way to introduce properties that are different from the default). Hence you need:

cell.Border = PdfPCell.NO_BORDER;
cell.CellEvent = new RoundedBorder();

Now the specific cell cell will only have a rounded border.



来源:https://stackoverflow.com/questions/23652856/why-i-cant-obtain-rounded-table-corners-in-this-itext-itextsharp-footer-table

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