4

我正在尝试从从数据库填充的 DataGridView 创建 PDF。

我刚刚开始尝试学习如何使用 iTextSharp 来完成此任务。

我的代码的结果是一个无法打开的 PDF。我收到一条错误消息“无法打开文件”

这是我生成 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);
        }
    }

我猜我的问题与设置列宽有关,但我不确定。一次,只有一次..当我尝试打开 PDF 时看到一个错误,上面写着“非法浮点除以 0”或类似的内容。

任何帮助是极大的赞赏。

4

2 回答 2

2

这听起来很明显,但是您的程序没有运行并将 pdf 文件锁定到其进程,从而阻止 adobe pdf reader 读取它是吗?

于 2012-11-27T15:55:04.233 回答
-1

私人无效jbtnPdf_Click(对象发送者,EventArgs e){尝试{

            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.");
        }
    }
于 2014-03-05T12:06:34.120 回答