fedeteka
fedeteka

Reputation: 963

Handle or intercept new page event on a PDF table to draw a line

I am creating a PDF table and I can manage what happens when the table need more than one page as posted here:

iTextsharp - draw a line at the end and start of a page with tables

If the table needs another page then I draw a final line on the actual page BEFORE the new page is inserted.

Now I need to draw a top line on the NEW page but I don't know to which method call. I'm trying something like this:

Imports iTextSharp.text.pdf

Public Class LineaBottom
    Implements IPdfPTableEvent

    Public Sub TableLayout(table As PdfPTable, widths As Single()(), heights() As Single, headerRows As Integer, rowStart As Integer, canvases() As PdfContentByte) Implements IPdfPTableEvent.TableLayout
        'Throw New NotImplementedException()

        Dim columns As Integer
        Dim footer As Integer = widths.Length - table.FooterRows
        Dim header As Integer = table.HeaderRows - table.FooterRows + 1
        Dim ultima As Integer = footer - 1

        If last <> -1 Then
            Dim line As PdfContentByte
            line = pdfWrite.DirectContent
            line.SetLineWidth(0.5)
            line.MoveTo(xStart, curY)
            line.LineTo(xEnd, curY)
            line.Stroke()

            'canvases(PdfPTable.BASECANVAS).Rectangle(rect)
        End If
    End Sub
End Class

Where xStart and xEnd are global variables with left and right margin plus or minus a value.

I don't know how to adapt the line

canvases(PdfPTable.BASECANVAS).Rectangle(rect)

because that line was from a Java sample drawing a Rectangle and I need just a line

and the line

If last <> -1 Then

detects the last row of a page, I need to detect the first row of the new page

Upvotes: 0

Views: 788

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

I'm not sure if I understand your question correctly, but:

  • If I need to add something when a table is being split and before iTextSharp moves to a new page, I would not use a IPdfPTableEvent. Instead I would use a IPdfPTableEventSplit
  • If I need to add something when the table is being split, after iTextSharp moves to a new page, and before the rest of the table is added, I would not use a IPdfPTableEvent. Instead I would use a IPdfPTableEventAfterSplit

When creating the events, I would pass the Document and PdfWriter object. I would get the coordinates I need by asking that Document object for the dimensions of the current page, and I would use the PdfWriter to add the lines.

However, that's not an answer to your question. You seem to know how to draw a rectangle, but you don't know how to draw a line. That's simple:

PdfContentByte cb = canvases(PdfPTable.BASECANVAS)
cb.MoveTo(x1, y1)
cb.LineTo(x2, y2)
cb.Stroke()

You can change the all kinds of properties, for instance line width, like this:

PdfContentByte cb = canvases(PdfPTable.BASECANVAS)
cb.SaveState()
cb.SetLineDash(8, 4, 0)
cb.SetLineWidth(2.0f)
cb.MoveTo(x1, y1)
cb.LineTo(x2, y2)
cb.Stroke()
cb.RestoreState()

As for the values of x1, y1, x2, and y2, you have to define them based on the values passed to you through the widths and heights parameters.

Upvotes: 1

Related Questions