I have the following code that creates PDF in memory using ITextSharp and emails the PDF as an attachment.
This works very well.
We can also successfully insert a logo into the generated PDF file.
We would like this logo at the top of the PDF. However, that is not working correctly.
There are two things wrong.
One, there is a line across the logo; not sure why that is happening.
Second, the logo appears larger on the PDF file than the actual size of the logo.
Any ideas how to make the logo fit nicely or how to make it normal size on a PDF file?
Thanks in advance.
Private Sub SendPDFEmail(dtb As DataTable)
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
Dim FullName As String = txtFullName.Text
Dim user As String = txtUserName.Text
Dim signedName As String = txtSignature.Text
Dim lDate As String = txtDate.Text
Dim sb As New StringBuilder()
sb.Append("<table border = '0'>")
sb.Append("<tr>")
For Each column As DataColumn In dtb.Columns
sb.Append("<th style = 'background-color: #ffffff;color:#000000;margin-top:150px'>")
sb.Append(column.ColumnName)
sb.Append("</th>")
Next
sb.Append("</tr>")
For Each row As DataRow In dtb.Rows
sb.Append("<tr>")
For Each column As DataColumn In dtb.Columns
sb.Append("<td>")
sb.Append(row(column))
sb.Append("</td>")
Next
sb.Append("</tr>")
Next
sb.Append("</table>")
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>")
sb.Append("<tr><td align='center' style='background-color: #ffffff' colspan = '1'></td></tr>")
sb.Append("<tr><td colspan = '2'></td></tr>")
sb.Append("<tr><td><b>Employee Name:</b>")
sb.Append(FullName)
sb.Append("</td></tr><tr><td><b>Employee ID: </b>")
sb.Append(user)
sb.Append("</td></tr><tr><td><b>Signature: </b>")
sb.Append(signedName)
sb.Append(" </td></tr>")
sb.Append("<tr><td colspan = '2'><b>Date :</b> ")
sb.Append(lDate)
sb.Append("</td></tr>")
sb.Append("</table>")
sb.Append("<br />")
Dim sr As New StringReader(sb.ToString())
' Load a sample image.
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
Using memoryStream As New MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
htmlparser.Parse(sr)
'insert image into pdf
Dim logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/fc3.png"))
logo.SetAbsolutePosition(40, 750)
pdfDoc.Add(logo)
pdfDoc.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Dim mm As New MailMessage("NoReply@domain.com", "domain.com")
mm.Subject = "From: " & txtFullName.Text
mm.Body = "Peronnel Policies and Procedures"
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), user & ".pdf"))
mm.IsBodyHtml = True
Dim smtp As New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As New System.Net.NetworkCredential()
NetworkCred.UserName = "myuser"
NetworkCred.Password = "mypass"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
End Using
End Using
End Using
End Sub