Вопрос

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:

enter image description here

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

Tnx

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top