C# – Generating PDF from a DataGridView in Winforms

citextsharppdf-generation

I'm attempting to create a PDF from a DataGridView populated from a database.

I have just started trying to learn how to use iTextSharp to accomplish this.

The result of my code is a PDF that will not open. I get an error saying "File cannot be opened"

Here is my code to generate the PDF

void SendToPDF(string heading, string filename)
    {
        try
        {
            Document doc = new Document(PageSize.A4.Rotate(), 30, 30, 20, 20);

            string myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (!Directory.Exists(myDocs + @"\Production Reports"))
                Directory.CreateDirectory(myDocs + @"\Production Reports");

            PdfWriter.GetInstance(doc, new FileStream(myDocs + @"\Production Reports\" + filename + ".pdf", FileMode.Append, FileAccess.Write));

            iTextSharp.text.Font titleFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 14.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK);

            iTextSharp.text.Font tableFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            iTextSharp.text.Font headerfont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK);

            PdfPTable table = new PdfPTable(GridView.Columns.Count);
            //table.TotalWidth = GridView.Width;

            //There are ALWAYS 10 columns

            float[] widths = new float[] { GridView.Columns[0].Width, GridView.Columns[1].Width, GridView.Columns[2].Width, 
                                           GridView.Columns[3].Width, GridView.Columns[4].Width, GridView.Columns[5].Width,
                                           GridView.Columns[6].Width, GridView.Columns[7].Width, GridView.Columns[8].Width,
                                           GridView.Columns[9].Width };
            table.SetWidths(widths);
            table.HorizontalAlignment = 1; // 0 - left, 1 - center, 2 - right;
            table.SpacingBefore = 2.0F;

            PdfPCell cell = null;

            doc.Open();
            Phrase p = new Phrase(new Chunk(heading, titleFont));
            doc.Add(p);

            foreach (DataGridViewColumn c in GridView.Columns)
            {
                cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText, headerfont)));
                cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
                table.AddCell(cell);
            }

            if (GridView.Rows.Count > 0)
            {
                for (int i = 0; i < GridView.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < GridView.Columns.Count - 1; j++)
                    {
                        cell = new PdfPCell(new Phrase(GridView.Rows[i].Cells[j].Value.ToString(), tableFont));
                        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                        cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
                        table.AddCell(cell);
                    }
                }
            }
            doc.Add(table);
            doc.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, "Error Generating PDF", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

I'm guessing my problem has to do with setting column widths, but I'm not sure. One time, and only one time..I saw an error when I tried to open the PDF that said "illegal floating point division by 0" or something along those lines.

Any help is greatly appreciated.

Best Answer

It may sound obvious, but your program isn't running and locking the pdf file to its process thus preventing adobe pdf reader from reading it is it?