0

Tengo un Form que me genera un DataGridView en el que muestro campos de una tabla.

El problema que tengo es que me exporta el pdf de la siguiente manera:

introducir la descripción de la imagen aquí

No sé que esté pasando. He aquí mi código que genera el .pdf:

private void button4_Click(object sender, EventArgs e)
        {
            //Creating iTextSharp Table from the DataTable data
            PdfPTable pdfTable = new PdfPTable(datagridview1.ColumnCount);
            pdfTable.DefaultCell.Padding = 3;
            pdfTable.WidthPercentage = 30;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;

            //Adding Header row
            foreach (DataGridViewColumn column in datagridview1.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText));
                cell.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
                pdfTable.AddCell(cell);
            }

            if (pdfTable == null)
            {
                //Adding DataRow
                foreach (DataGridViewRow row in datagridview1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        pdfTable.AddCell(cell.Value.ToString());
                    }
                }
            }

            //Exporting to PDF
            string folderPath = "C:\\Users\\maury\\Desktop";
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
            {
                Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
                PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                pdfDoc.Add(pdfTable);
                pdfDoc.Close();
                stream.Close();
            }
        }

Cabe destacar que en ésta linea en especial:

if (pdfTable == null)
            {
                //Adding DataRow
                foreach (DataGridViewRow row in datagridview1.Rows)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        pdfTable.AddCell(cell.Value.ToString());
                    }
                }
            }

He agregado el if, ya que me generaba un NRE (NullReferenceException) Pero mehe cerciorado de instanciar todos mis objetos de manera correcta. Mi datagridview lo genero directamente en el Form, no invoco ningún objeto de ninguna otra clase.

1 respuesta 1

2

Intenta con algo así para generar el pdf

    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(dgv_listaActividades.ColumnCount);
        for (int i = 0; i < dgv_listaActividades.ColumnCount; i++)
        {
            objP = new Phrase(dgv_listaActividades.Columns[i].HeaderText, fuente);
            datatable.HorizontalAlignment = Element.ALIGN_CENTER;

            datatable.AddCell(objP);
        }

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

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.