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. */
    }

introducir la descripción de la imagen aquí

Alguien podría indicarme que está mal en el código que provoca que se reviente en esta línea, gracias de antemano

2 respuestas 2

1

Utiliza este metodo para generar el documento aver si te funciona....

    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);
 }

Si tienes un boton de generar el documento...

    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);
       }
    }

Espero y te sirva.....

1

Aunque abriste el archivo y creaste una nueva pagina, el archivo todavia no tiene informacion. Puedes agregar algo asi:

pdfDoc.Open();
pdfDoc.NewPage();
htmlparser.Parse(sr);
pdfDoc.Add(new Paragraph("Hello World!"));
pdfDoc.Close();
2
  • Gracias por responder tan rápido, bueno al agregar la línea con el new Paragraph me muestra el "Hello World" pero no muestra el gridview que necesito de que forma puedo mostrarlo? el 7 may. 2018 a las 21:49
  • Intenta mover la linea del htmlparser despues del Hello World
    – alanfcm
    el 7 may. 2018 a las 22:10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.