Reputation: 25
I'm trying to use ItextSharp to create pdf from my web app.
to create a Header section on every page of my pdf, I create a partial class where I override the OnEndPage
method.
Everything works fine, with just one exception.
I designed my header as a table with 2 columns, in the first I put a logo, and in the second I want to show some text on multiple lines; so in the second cell I created a subtable with 1 column and several rows, but this subtable always shows the external black border which I'm not able to remove.
Here is the code:
Public Overrides Sub OnEndPage(writer As PdfWriter, document As Document)
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = 0
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = 0
subTable.AddCell(myCell)
Next
subTable.DefaultCell.BorderWidth = 0
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
Anyone can help? Thanks a lot
Upvotes: 1
Views: 898
Reputation: 77606
Several things are wrong in your code. For instance: you create a new headerIMG
object for each page. This means that the same image bytes will be added to the PDF over and over again. You should declare headerIMG
outside the OnStartPage
method.
Furthermore: you are defining a BorderWidth
of 0. As defined in the PDF specification, a line width of 0 doesn't mean that there is no line. Please read ISO-32000-1, section 8.4.3.2 "Line Width":
A line width of 0 shall denote the thinnest line that can be rendered at device resolution: 1 device pixel wide.
If you don't want a border, tell iText that you don't want a border:
Dim headerIMG As Image = Image.GetInstance(HttpContext.Current.Server.MapPath(logoPath))
Public Overrides Sub OnStartPage(writer As PdfWriter, document As Document)
Dim pageSize As Rectangle = document.PageSize
Dim headerTbl As New PdfPTable(2)
headerTbl.TotalWidth = 600
headerTbl.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(headerIMG)
cell.Border = PdfPCell.NO_BORDER
cell.PaddingLeft = 10
cell.PaddingBottom = 10
headerTbl.AddCell(cell)
Dim subTable = New PdfPTable(1)
For Each s As String In HeaderText
Dim myCell As New PdfPCell(New Paragraph(s))
myCell.Border = PdfPCell.NO_BORDER
subTable.AddCell(myCell)
Next
subTable.DefaultCell.Border = PdfPCell.NO_BORDER
headerTbl.AddCell(subTable)
headerTbl.WriteSelectedRows(0, -1, 0, pageSize.GetTop(5), writer.DirectContent)
End Sub
Do you see what I changed?
Also, please read the comment provided by nelek. Why do you need a sub table? You can easily define a rowspan for the cell containing the image.
Upvotes: 1