加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从Winforms中的DataGridView生成PDF

发布时间:2020-12-15 21:43:22 所属栏目:百科 来源:网络整理
导读:我正在尝试从数据库中填充的DataGridView创建PDF. 我刚开始尝试学习如何使用iTextSharp来实现这一目标. 我的代码的结果是一个无法打开的PDF.我收到错误消息“文件无法打开” 这是我生成PDF的代码 void SendToPDF(string heading,string filename) { try { Do
我正在尝试从数据库中填充的DataGridView创建PDF.

我刚开始尝试学习如何使用iTextSharp来实现这一目标.

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

这是我生成PDF的代码

void SendToPDF(string heading,string filename)
    {
        try
        {
            Document doc = new Document(PageSize.A4.Rotate(),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,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 + "rn" + ex.StackTrace,"Error Generating PDF",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
    }

我猜我的问题与设置列宽有关,但我不确定.有一次,只有一次..当我试图打开“非法浮点除0”或类似的东西时,我看到了一个错误.

任何帮助是极大的赞赏.

解决方法

听起来很明显,但你的程序没有运行并将pdf文件锁定到它的进程,从而阻止了adobe pdf阅读器的阅读呢?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读