Click here to Skip to main content
15,901,205 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Need HRMS Application Pin
Richard MacCutchan8-May-17 21:08
mveRichard MacCutchan8-May-17 21:08 
GeneralRe: Need HRMS Application Pin
naveen.prasad1523-Nov-17 7:18
naveen.prasad1523-Nov-17 7:18 
GeneralRe: Need HRMS Application Pin
geeta goel4-Nov-17 22:53
geeta goel4-Nov-17 22:53 
GeneralRe: Need HRMS Application Pin
naveen.prasad1525-Nov-17 19:24
naveen.prasad1525-Nov-17 19:24 
GeneralRe: Need HRMS Application Pin
geeta goel6-Nov-17 6:36
geeta goel6-Nov-17 6:36 
AnswerRe: Need HRMS Application Pin
Richard MacCutchan8-May-17 8:53
mveRichard MacCutchan8-May-17 8:53 
AnswerRe: Need HRMS Application Pin
David Mujica8-May-17 9:19
David Mujica8-May-17 9:19 
QuestionProblem converting itextsharp from htmlWorker to xmlWorker Pin
samflex6-May-17 12:33
samflex6-May-17 12:33 
We have a spec to generate PDF from HTML inputs and send as email attachments.

We were able to use the examples on the article below to accomplish that:

https://www.aspsnippets.com/Articles/iTextSharp-Generate-PDF-in-Memory-and-send-as-Email-Attachment-using-C-VBNet-and-ASPNet.aspx

The issue is that because the article uses HTMLWorker, it is extremely difficult to format data with HTMLWorker.

So, I attempted to convert HTMLWorker to XMLWorker.

Right now, it works by inserting records into the database but the email is not getting sent.

Any ideas what I am doing wrong?

PHP
Imports iTextSharp.tool.xml
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Text
Imports System.Data
Imports System.Net
Imports System.Net.Mail
Imports iTextSharp.text.html.simpleparser
Imports System.Data.SqlClient
Imports System.Runtime.CompilerServices
Partial Class _Default
    Inherits System.Web.UI.Page
    Private strConn As String = ConfigurationManager.ConnectionStrings("constr").ToString()
    Private sqlCon As SqlConnection

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then

        End If

    End Sub
    Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
        System.Threading.Thread.Sleep(5000)
        Try
            Dim table As New DataTable()
            Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
                Using cmd = New SqlCommand("INSERT INTO Policies(userid,entrydate,signature, email) VALUES(@empID, @lDate,@signature, @email)", con)
                    Using da = New SqlDataAdapter(cmd)
                        cmd.CommandType = CommandType.Text
                        cmd.Parameters.AddWithValue("@empID", txtUserName.Text)
                        cmd.Parameters.AddWithValue("@lDate", txtDate.Text)
                        cmd.Parameters.AddWithValue("@signature", txtSignature.Text)
                        cmd.Parameters.AddWithValue("@email", txtEmail.Text)
                        da.Fill(table)
                    End Using
                End Using
            End Using
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(0) {New DataColumn(" ")})
            '  dt.Rows.Add(txtaffirmation.Text)
             SendPDFEmail(dt)
            'If the message failed at some point, let the user know
            Label1.ForeColor = System.Drawing.Color.Blue
            Label1.Text = "Success!<br> A copy of the completed form has been sent to your inbox. If you do not receive an email within 5 minutes (usually immediately), check your spam box."

            'Reset markup controls
            txtFullName.Text = ""
            txtUserName.Text = ""
            txtSignature.Text = ""
            txtDate.Text = ""
            txtEmail.Text = ""

        Catch ex As Exception

            'If the message failed at some point, let the user know
            Label1.ForeColor = System.Drawing.Color.Red
            Label1.Text = ex.Message
        End Try

        ''reset markup controls
        'ClearTextBoxes(Page)


    End Sub
    Private Sub SendPDFEmail(ByVal dtb As DataTable)
        Dim strHtml As String
        Dim memStream As New MemoryStream()
        Dim strWriter As New StringWriter()
        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 body As String = "<br />"

        body = body & "<table style=""background-color:#F8F8F8"" cellspadding=""2"" border=""0"" width=""100%"" >"

        For Each column As DataColumn In dtb.Columns
            body = body & "<td style=""color: black"" align=""Left"">" & txtaffirmation.Text & "</td></tr></table><br>"
        Next
        body = body & " <br><br>"

        body = body & "<table style=""background-color:#F8F8F8"" cellspadding=""2"" border=""0"" width=""100%"" >"
        body = body & "<tr><td align=""left"">" & FullName & "</td>"
        body = body & "<td align=""left"">" & user & "</td></tr>"
        body = body & "<tr><td align=""left"">Employee Name</td>"
        body = body & "<td align=""left"">Employee ID</td></tr>"
        body = body & "<tr><td style=""font-family:Brush Script Std"" align=""left"">" & signedName & "               </td><td align=""left"">" & " " & "</td>"
        body = body & "<td align=""left"">Employee Signature</td><td align=""left"">" & " " & "</td></tr>"
        body = body & "<tr><td align=""left"">" & lDate & "</td><td align=""left"">" & " " & "</td></tr>"
        body = body & "<td align=""left"">Date</td><td align=""left"">" & " " & "</td></tr></table>"

        strHtml = strWriter.ToString()
        strWriter.Close()
        strWriter.Dispose()
        Dim strFileShortName As String = "test" & DateTime.Now.Ticks & ".pdf"
        Dim strFileName As String = HttpContext.Current.Server.MapPath("reports\" & strFileShortName)
        Dim docWorkingDocument As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 1, 1, 0, 0)
        Dim srdDocToString As StringReader = Nothing
        Try
            Dim pdfWrite As PdfWriter
            pdfWrite = PdfWriter.GetInstance(docWorkingDocument, New FileStream(strFileName, FileMode.Create))
            srdDocToString = New StringReader(strHtml)
            docWorkingDocument.Open()
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString)
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            If Not docWorkingDocument Is Nothing Then
                docWorkingDocument.Close()
                docWorkingDocument.Dispose()
            End If
            If Not srdDocToString Is Nothing Then
                srdDocToString.Close()
                srdDocToString.Dispose()
            End If
        End Try

        Using memoryStream As New MemoryStream()
            Dim writer As PdfWriter = PdfWriter.GetInstance(docWorkingDocument, memoryStream)
            docWorkingDocument.Open()

            'insert image into pdf
            Dim logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/fc3.png"))
            logo.SetAbsolutePosition(29, 680) ' insert top left
            'logo.ScaleToFit(350.0F, 300.0F) ' reduce image size
            docWorkingDocument.Add(logo)

            docWorkingDocument.Close()
            Dim bytes As Byte() = memoryStream.ToArray()
            memoryStream.Close()

            Dim mm As New MailMessage("NoReply@email.com", txtEmail.Text)
            mm.Subject = "From: " & txtFullName.Text
            mm.Body = "Please see attached copy of your completed form. "
            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 = False
            Dim NetworkCred As New System.Net.NetworkCredential()
            NetworkCred.UserName = "myemail@gmail.com"
            NetworkCred.Password = "mpassword"
            smtp.UseDefaultCredentials = True
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
        End Using

    End Sub
End Class

AnswerRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
Richard Deeming8-May-17 2:06
mveRichard Deeming8-May-17 2:06 
GeneralRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
samflex9-May-17 7:10
samflex9-May-17 7:10 
GeneralRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
Richard Deeming10-May-17 0:38
mveRichard Deeming10-May-17 0:38 
GeneralRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
samflex12-May-17 9:26
samflex12-May-17 9:26 
GeneralRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
samflex12-May-17 9:02
samflex12-May-17 9:02 
GeneralRe: Problem converting itextsharp from htmlWorker to xmlWorker Pin
Richard Deeming12-May-17 9:15
mveRichard Deeming12-May-17 9:15 
QuestionText in a Div is pushing icon to the next line. How do I stop this? Pin
McTartan3-May-17 11:53
professionalMcTartan3-May-17 11:53 
AnswerRe: Text in a Div is pushing icon to the next line. How do I stop this? Pin
John C Rayan11-May-17 5:47
professionalJohn C Rayan11-May-17 5:47 
GeneralRe: Text in a Div is pushing icon to the next line. How do I stop this? Pin
McTartan12-May-17 9:40
professionalMcTartan12-May-17 9:40 
PraiseRe: Text in a Div is pushing icon to the next line. How do I stop this? Pin
John C Rayan14-May-17 22:24
professionalJohn C Rayan14-May-17 22:24 
QuestionDataGrid with CHECKBOX and RADIOBUTTON in the column. Pin
VinKot27-Apr-17 5:02
VinKot27-Apr-17 5:02 
AnswerRe: DataGrid with CHECKBOX and RADIOBUTTON in the column. Pin
Afzaal Ahmad Zeeshan27-Apr-17 5:33
professionalAfzaal Ahmad Zeeshan27-Apr-17 5:33 
AnswerRe: DataGrid with CHECKBOX and RADIOBUTTON in the column. Pin
User 418025427-Apr-17 11:14
User 418025427-Apr-17 11:14 
QuestionForce Browser to use updated pdf file instead of cache version Pin
Blikkies26-Apr-17 6:51
professionalBlikkies26-Apr-17 6:51 
AnswerRe: Force Browser to use updated pdf file instead of cache version Pin
jkirkerx26-Apr-17 9:37
professionaljkirkerx26-Apr-17 9:37 
GeneralRe: Force Browser to use updated pdf file instead of cache version Pin
Blikkies26-Apr-17 9:43
professionalBlikkies26-Apr-17 9:43 
AnswerRe: Force Browser to use updated pdf file instead of cache version Pin
ZurdoDev26-Apr-17 10:21
professionalZurdoDev26-Apr-17 10:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.