0

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
Tairoc
  • 635
  • 1
  • 6
  • 18
  • I think u should use crystal view to show image in pdf or any other way in pdf – aditya shrivastava Nov 02 '16 at 19:13
  • You are using `HTMLWorker`. That class has been abandoned a long time ago. You need to use XML Worker instead! – Bruno Lowagie Nov 02 '16 at 19:22
  • @BrunoLowagie, if you would be kind to show the existing question you referred to, perhaps, it will solve my problem. Thank you much. – Tairoc Nov 02 '16 at 19:38
  • @BrunoLowagie, yes, you are correct, it is awefully close although that solution would not have worked for me. In any case, I have changed the framework of the question because right now, my issue is how to set the image to fit nicely. Thank you – Tairoc Nov 03 '16 at 02:43
  • You have changed a closed question into a totally different question. Maybe you should have created a new question instead. It's not clear if you are still using `HTMLWorker` (which is a dead end), or if you are now using XML Worker (your comment seems to say so, but your edit question still mentions `HTMLWorker`). – Bruno Lowagie Nov 03 '16 at 07:03
  • @BrunoLowagie, I tried switching to XMLWorker() but that appears to have created several more errors in the code. Also, I didn't realize that the question was closed. I thought you said to change it, not come up with something totally different. – Tairoc Nov 03 '16 at 13:41
  • I resolved this problem last night, I should mention. – Tairoc Nov 03 '16 at 13:48

0 Answers0