Save Gridview as PDF - ItextSharp

0
    protected void ExportarPDF(object sender, EventArgs e)
    {

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=SearchBooking.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        GridViewGeneral.AllowPaging = false;
        GridViewGeneral.DataBind();
        GridViewGeneral.RenderControl(hw);

        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

        pdfDoc.Open();
        pdfDoc.NewPage();
        htmlparser.Parse(sr);
        pdfDoc.Close();

        Response.Write(pdfDoc);
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

Someone could tell me what is wrong with the code that causes it to burst on this line, thanks in advance

    
asked by Kattyleja Jaramillo 07.05.2018 в 23:41
source

2 answers

1

Although you opened the file and created a new page, the file still has no information. You can add something like this:

pdfDoc.Open();
pdfDoc.NewPage();
htmlparser.Parse(sr);
pdfDoc.Add(new Paragraph("Hello World!"));
pdfDoc.Close();
    
answered by 07.05.2018 в 23:45
0

Use this method to generate the aver document if it works ....

    public void GenerarDocumentos(Document document)
    {
    Phrase objP = new Phrase("A", fuente);
    //SE GENERA EL ENCABEZADO DE LA TABLA EN EL PDF
    PdfPTable datatable = new PdfPTable(TuDataGrid.ColumnCount);
    for (int i = 0; i < TuDataGrid.ColumnCount; i++)
    {
        objP = new Phrase(TuDataGrid.Columns[i].HeaderText, fuente);
        datatable.HorizontalAlignment = Element.ALIGN_CENTER;

        datatable.AddCell(objP);
    }

    //SE GENERA EL CUERPO DEL PDF
    for (int i = 0; i < TuDataGrid.RowCount - 1; i++)
    {
        for (int j = 0; j < TuDataGrid.ColumnCount; j++) 
        {
            objP = new Phrase(TuDataGrid[j,i].Value.ToString());
            datatable.AddCell(objP);
        }
        datatable.CompleteRow();
    }
    document.Add(datatable);
 }

If you have a button to generate the document ...

    private void btn_pdf_Click(object sender, EventArgs e)
    {
       if(TuDatagrid.Rows == 0)
       {
          MessageBox.Show("No hay datos para realizar un reporte");
       }
       else //Aqui pones todo lo de tu documento
       {
         Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
         //Llamas el metodo GenerarDocumentos
         GenerarDocumentos(pdfDoc);
       }
    }

I hope and serve you .....

    
answered by 04.09.2018 в 16:49