Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Export Databind control in PDF using iTextSharp library in VB.Net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 268
    Comment on it

    Export Databind control in PDF using iTextSharp

    Some time we need to display databind control like Gridview, Label, Textbox into PDF file. here we can use iTextSharp library.Firstly we need to add iTextSharp in our project reference. 

    Set contentType to PDF and add file name in response header as below :

    Response.ContentType = "application/pdf"
    
    Response.AddHeader("content-disposition", "attachment;filename=File.pdf")
    

    Set object for HtmlForm and HtmlTextWriter.

    Dim sw As StringWriter = New StringWriter()
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim frm As HtmlForm = New HtmlForm()

    Now add control in HtmlForm object like :

    frm.Controls.Add(Panel1) // Panel1 is our aspx page control 
    
    frm.Controls.Add(Gridview) 
    

    We can also set the style of our frm object :

    frm.Style.Add("font-size", "34px")

    Now render the HtmlTextWriter  object into frm

    frm.RenderControl(hw)

    Now create a object of StringReader and pass StringWriter into it and set Document object.

    Dim sr As StringReader = New StringReader(sw.ToString())
    Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 35.0F)

    Now call the PDFWriter class of iTextSharp and pass the document object and using the htmlparser, parse the stringreader object

    Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
     
    pdfDoc.Open()
    pdfDoc.Add(image)
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.End()

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: