iTextsharp - draw a line at the end and start of a

2019-03-03 12:01发布

问题:

Selecting records from a table I create iTextsharp's Tables one for every first letter of the records

On the picture a table for the "G" letter:

"G" is a row of 6 cells

Then a row of 6 cells with the "headers"

and then rows with records

The cells of the rows only need left and right border.

But I need to draw or "close" the line for the last row of the page and also draw or "open" the line of the first row of the next page.

I read a lot of post but I can't figure it out the solution

I know how to draw a graphic line and how to find the coords or how to set bottom or top border, but I don´t know how to detect the page break or if I can manage this situation with forced footers or headers only on cases like the one of the picture.

The code of the class adapated to VB Thanks to COeDev for the support

Now I only need to resolve the Rectangle (or draw a line) because is not the same on VB.NET (Lines marked as comment)

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 rect As Rectangle
    Dim footer As Integer = widths.Length - table.FooterRows
    Dim header As Integer = table.HeaderRows - table.FooterRows + 1
    Dim ultima As Integer = footer - 1

    If ultima <> -1 Then

        columns = widths(ultima).Length - 1
        rect = New Rectangle(widths(ultima)(0), heights(ultima), widths(footer - 1)(columns), heights(ultima + 1))
        'rect.BorderColor = BaseColor.BLACK
        'rect.BorderWidth = 1
        'rect.Border = Rectangle.TOP_BORDER
        'canvases(PdfPTable.BASECANVAS).Rectangle(rect)

    End If
End Sub

I hope this code will serve other people because there is not much information on the Internet

回答1:

This should be helpful for you: itextsharp: how to show the bottom line of a table with property HeaderRows=1 if border bottom of the row is not set?

You will need to add some code to draw an additional header line, too e.g.:

columns = widths[0].Length - 1;
rect = new Rectangle(widths[0][0], heights[0], widths[0][columns], heights[0]);
rect.BorderColor = Color.BLACK;
rect.BorderWidth = 1;
rect.Border = Rectangle.TOP_BORDER;
canvases[PdfPTable.BASECANVAS].Rectangle(rect);

4.1.6.0



回答2:

I found the solution, no new class required

Dim heightActualLetter, verticalSpaceAvailable As Integer
heightActualLetter = table.TotalHeight
verticalSpaceAvailable = pdfWrite.GetVerticalPosition(False) - pdfDoc.BottomMargin
If heightActualLetter > verticalSpaceAvailable Then

    Dim finalLine As PdfContentByte
    finalLine = pdfWrite.DirectContent

    Dim curY As Int32
    curY = pdfWrite.GetVerticalPosition(False)

    finalLine.SetLineWidth(0.5)
    finalLine.MoveTo(xStart, curY)
    finalLine.LineTo(xEnd + 1, curY)
    finalLine.Stroke()
End If

I don´t know why I need the +1 on xEnd + 1 but maybe is because of the other lines being 0.5 I need to rounded up



标签: vb.net itext