Creating PDF from DataGridView in Winforms

I am trying to create a PDF from a DataGridView populated from a database.

I just started trying to learn how to use iTextSharp for this.

The result of my code is a PDF that does not open. I get an error "File does not open"

Here is my PDF creation code

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 assume that my problem is with setting the width of the columns, but I'm not sure. Once and only once. I got an error when I tried to open a PDF that said β€œillegal floating point division by 0” or something in that direction.

Any help is greatly appreciated.

+4
source share
2 answers

This may seem obvious, but your program does not work and blocks the pdf file for its process, thereby preventing the Adobe Reader reader from reading it?

+2
source

private void jbtnPdf_Click (object sender, EventArgs e) {try {

  SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "All Files | *.* "; if (saveFileDialog.ShowDialog() == DialogResult.OK) { string path = saveFileDialog.FileName; Document pdfdoc = new Document(PageSize.A4); // Setting the page size for the PDF PdfWriter writer = PdfWriter.GetInstance(pdfdoc, new FileStream(path + ".pdf", FileMode.Create)); //Using the PDF Writer class to generate the PDF writer.PageEvent = new PDFFooter(); // Opening the PDF to write the data from the textbox PdfPTable table = new PdfPTable(jdgvChild.Columns.Count); //table.TotalWidth = GridView.Width; float[] widths = new float[] { jdgvChild.Columns[0].Width, jdgvChild.Columns[1].Width, jdgvChild.Columns[2].Width, jdgvChild.Columns[3].Width, jdgvChild.Columns[4].Width, jdgvChild.Columns[5].Width, jdgvChild.Columns[6].Width, jdgvChild.Columns[7].Width}; table.SetWidths(widths); table.HorizontalAlignment = 1; // 0 - left, 1 - center, 2 - right; table.SpacingBefore = 2.0F; PdfPCell cell = null; pdfdoc.Open(); //doc.Open(); // Phrase p = new Phrase(new Chunk(heading, titleFont)); // doc.Add(p); foreach (GridViewDataColumn c in jdgvChild.Columns) { cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText))); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; table.AddCell(cell); } if (jdgvChild.Rows.Count > 0) { for (int i = 0; i < jdgvChild.Rows.Count; i++) { PdfPCell[] objcell = new PdfPCell[jdgvChild.Columns.Count]; for (int j = 0; j < jdgvChild.Columns.Count-1; j++) { cell = new PdfPCell(new Phrase(jdgvChild.Rows[i].Cells[j].Value.ToString())); cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; cell.VerticalAlignment = PdfPCell.ALIGN_CENTER; // table.AddCell(cell); //lstCells.Add(cell); objcell[j] = cell; } PdfPRow newrow = new PdfPRow(objcell); table.Rows.Add(newrow); } } pdfdoc.Add(table); MessageBox.Show("Pdf Generation Successfully."); pdfdoc.Close(); } } catch (Exception ex) { MessageBox.Show("Error in pdf Generation."); } } 
-1
source

All Articles