Export to PDF using VB.NET

Unknown | 10:04 AM |

Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try
            Dim Stream As MemoryStream = pdf()
            Response.Clear()
            Response.AddHeader("content-type", "application/pdf")
            Response.AddHeader("cache-control", "no-cache")
            Response.AddHeader("accept-ranges", "none")
            Dim filename As String = "BOLDetails_" & Request.QueryString("ID").ToString() & ".pdf"
            saveFile(Stream, filename)
            Response.AddHeader("content-disposition", "attachment; filename=" & filename)
            Response.OutputStream.Write(Stream.GetBuffer(), 0, Stream.GetBuffer().Length)
            Response.OutputStream.Flush()
            'Response.Write("<script type='text/javascript'>window.close();</script>")
            Response.OutputStream.Close()
            Response.End()
            'Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "close", "<script type='text/javascript'>window.close();</script>")
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
        End Try
    End Sub


 Private Function pdf() As MemoryStream
        Dim content As String = createHTML()
        Dim reader As TextReader = New StringReader(content)
        Dim doc As New Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10)
        Dim Stream As New MemoryStream()
        Dim wri As PdfWriter
        wri = PdfWriter.GetInstance(doc, Stream)
        doc.Open()
        Dim htmlarraylist As List(Of IElement) = html.simpleparser.HTMLWorker.ParseToList(reader, Nothing)
        For k As Integer = 0 To htmlarraylist.Count - 1
            doc.Add(DirectCast(htmlarraylist(k), IElement))
        Next
        doc.Close()
        Return Stream
    End Function

    Protected Function createHTML() As String
        Dim html As String = "<table cellspacing='0' cellpadding='0' border='0'><tbody>"
        If chkCreatedDate.Checked = True Then
            html = html & "<tr>" &
                "<td valign='top' align='left'>" &
                "Created Date:" &
                "</td>" &
                "<td valign='top' align='left'>" &
                txtCreatedDate.Text &
                "</td>" &
                "</tr>"
        End If
        If chkClearDate.Checked = True Then
            html = html & "<tr>" &
                "<td valign='top' align='left'>" &
                "Clear Date:" &
                "</td>" &
                "<td valign='top' align='left'>" &
                txtClearDate.Text &
                "</td>" &
                "</tr>"
        End If
        If chkDoor.Checked = True Then
            html = html &
                "<tr>" &
                "<td valign='top' align='left'>" &
                "Door #:" &
                "</td>" &
                "<td valign='top' align='left'>" &
                txtDoor.Text &
                "</td>" &
                "</tr>"
        End If
        If chkName.Checked = True Then
            html = html &
                "<tr>" &
                "<td valign='top' align='left'>" &
                "Name:" &
                "</td>" &
                "<td valign='top' align='left'>" &
                txtName.Text &
                "</td>" &
                "</tr>"
        End If
        If chkDirection.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "Direction:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtDirection.Text &
                    "</td>" &
                    "</tr>"
        End If
        If chkTrailer.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "Trailer:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtTrailer.Text &
                    "</td>" &
                    "</tr>"
        End If
        If chkCarrier.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "Carrier:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtCarrier.Text &
                    "</td>" &
                    "</tr>"
        End If
        If chkToFrom.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "To/From:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtToFrom.Text &
                    "</td>" &
                    "</tr>"
        End If
        If chkCompany.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "Company:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtCompany.Text &
                    "</td>" &
                    "</tr>"
        End If
        If chkCompanyLoc.Checked = True Then
            html = html &
                    "<tr>" &
                    "<td valign='top' align='left'>" &
                    "Company Location:" &
                    "</td>" &
                    "<td valign='top' align='left'>" &
                    txtCompanyLoc.Text &
                    "</td>" &
                    "</tr>"
        End If
        html = html & "</tbody></table>"
        Return html
    End Function

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....