locked
iTEXTSharp Query :-( RRS feed

  • Question

  • User-556137015 posted

    Hi Guys

    i have been reading some of articles regarding iTEXTSharp, and i was wondering if you can help me.

    I have a requirement to convert my aspx page to pdf. In my aspx page i have 2 textboxes (using the ajax calender extenders) 1 dropdownlist and 2 buttons, and a table that is generated dynamically from code behind file.

    The coding works fine, just how i want to it work. After the page is displayed in the browser. I have a button that says [Convert TO PDF], when that button is clicked i want everything on my page to be converted to PDF with a border and i need to add a image which is in my image folder.

    So i have read some articles and downloaded the examples for iTEXTSharp, but i still cannot get this to work!!!!!!!!!!!!

    I am using visual studio 2008, heres my code.

    Imports System
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Web
    Imports System.Web.Security
    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Text
    Imports MySql.Data.MySqlClient
    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    Imports System.Drawing
    
    Partial Class Post_Export_Quality_Reporting_QualityReporting
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            'get the connection string
            Dim Conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString)
            Try
                Dim Adapter As MySqlDataAdapter
                Dim ConnStr As String = ""
                ConnStr = "Select ID,name from agent"
                Adapter = New MySqlDataAdapter(ConnStr, Conn)
                Dim myTable As Data.DataTable = New Data.DataTable
                Conn.Open()
                Adapter.Fill(myTable)
                DropDownList1.DataSource = myTable
                DropDownList1.DataBind()
                DropDownList1.Items.Insert(0, "")
            Catch ex As Exception
                Response.Write(ex.Message)
            Finally
                'close the connection to the database
                Conn.Close()
            End Try
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'get the connection string
            Dim Conn As New MySqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString)
            Try
                Dim Reader As MySqlDataReader
                Dim adapter As MySqlDataAdapter
                Dim SqlStatement As String = ""
    
                SqlStatement = "I HAVE REMOVED MY SQL STATEMENT"
    
                adapter = New MySqlDataAdapter(SqlStatement, Conn)
                Dim myTable As Data.DataTable = New Data.DataTable()
                adapter.Fill(myTable)
                Conn.Open()
                Reader = adapter.SelectCommand.ExecuteReader()
                Dim r As String = Nothing
                'fill the reader
                If Reader.HasRows = True Then
    
                    r = r & "<table id='tab' width='100%' border='1' cellspacing='0' cellpadding='0'>"
                    r = r & "<tr>"
                    r = r & "<td>Reason</td>"
                    r = r & "<td>ID</td>"
                    r = r & "<td>Name</td>"
                    r = r & "<td>Description</td>"
                    r = r & "<td>Varierty Name</td>"
                    r = r & "<td>Value</td>"
                    r = r & "<td>Units</td>"
                    r = r & "<td>Price</td>"
                    r = r & "<td>Symbol</td>"
                    r = r & "</tr>"
    
                    While Reader.Read
                        'place the tags on the page inside the <div> tag
                        r = r & "<tr>"
                        r = r & "<td>" & Reader.Item("reason") & "</td>"
                        r = r & "<td>" & Reader.Item("ID") & "</td>"
                        r = r & "<td>" & Reader.Item("name") & "</td>"
                        r = r & "<td>" & Reader.Item("description") & "</td>"
                        r = r & "<td>" & Reader.Item("variety_name") & "</td>"
                        r = r & "<td>" & Reader.Item("value") & "</td>"
                        r = r & "<td>" & Reader.Item("units") & "</td>"
                        r = r & "<td>" & Reader.Item("price") & "</td>"
                        r = r & "<td>" & Reader.Item("symbol") & "</td>"
                        r = r & "</tr>"
    
                    End While
                    r = r & "</table>"
                End If
                data.InnerHtml = r
            Catch ex As Exception
                Response.Write(ex.Message)
            Finally
                Conn.Close()
            End Try
    
        End Sub
    
        Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
            'here we will convert to pdf
            Dim doc As Document = New Document
            Try
                PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\1.pdf", FileMode.Create))
                doc.Open()
                doc.AddTitle("ASPX TO PDF")
                'show the selection of the report for when it was generated for
                doc.Add(New Paragraph("From Date :- " & FromDate.Text & " " & " To Date :- " & ToDate.Text))
                doc.Add(New Paragraph("Agent :-" & DropDownList1.SelectedItem.ToString))
                'Here we should have the table converted to pdf (the table is generated in the button_click event above)
                'Also after the PDF is generated, when the user closes the window, the pdf must be deleted and not kept on the server on client machine.
                'The pdf should open in a new window, currently its opening the same window (which is wrong)
                doc.Close()
                Response.Redirect("~/1.pdf")
                Response.ContentType = "Application/pdf"
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End Sub
    End Class
    


    Thanks guys!

    Wednesday, January 20, 2010 8:16 AM

All replies