使用iTextsharp将多个网格视图转换为单个pdf [英] Multiple gridviews to single pdf using iTextsharp

查看:85
本文介绍了使用iTextsharp将多个网格视图转换为单个pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试使用iTextSharp将多个网格视图导出为单个pdf。我循环遍历网格视图,然后循环遍历gridview的行。循环正常。但是在pdf下载之后,只能看到最后一个gridview。看起来网格视图正在相互覆盖,只剩下最后一个。这是我的代码。我做错了什么?



Hi All,

I am trying to export multiple gridviews into single pdf using iTextSharp. I am looping through the gridviews and then looping through the rows of the gridview. The looping is going ok. But after pdf download, only the last gridview can be seen. It seems the gridviews are overwriting each other and only last one remains. Here is my code. What am I doing wrong?

protected void btnExportToPDF_Click(object sender, EventArgs e)
        {
            GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
            

            for (int i = 0; i < gvExcel.Length; i++)
            {
                if (gvExcel[i].Visible)
                {

                    PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);

                    foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
                    {
                        Font font = new Font();
                        font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                        PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
                        pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                        pdfTbl.AddCell(pdfCell);
                    }


                    foreach (GridViewRow gvRow in gvExcel[i].Rows)
                    {
                        foreach (TableCell tblCell in gvRow.Cells)
                        {
                            Font font = new Font();
                            font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                            PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                            pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                            pdfTbl.AddCell(pdfCell);
                        }
                    }

                    //Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                    pdfDoc.Open();
                    pdfDoc.Add(pdfTbl);
                }
            }

            pdfDoc.Close();

            //Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AppendHeader("content-disposition", "attachment;filename=report_" + startDate + "-" + endDate + ".pdf");
            Response.Write(pdfDoc);
            Response.Flush();
            Response.End();
        }





我的尝试:



我确信gridviews正在某处重叠。但是我无法找到哪里。



What I have tried:

I am sure there is somewhere the gridviews are getting overlapped. But I am not able to find where.

推荐答案

OK ...以前我在里面使用GetInstance和Open方法进行循环。这是我修改过的代码。



OK...previously I was using GetInstance and Open methods inside for loop. This is my modified code.

protected void btnExportToPDF_Click(object sender, EventArgs e)
       {
           GridView[] gvExcel = new GridView[] { gridvw1,gridvw2,gridvw3 };
           Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
           PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
           pdfDoc.Open();


           for (int i = 0; i < gvExcel.Length; i++)
           {
               if (gvExcel[i].Visible)
               {

                   PdfPTable pdfTbl = new PdfPTable(gvExcel[i].HeaderRow.Cells.Count);

                   foreach (TableCell headerTblCell in gvExcel[i].HeaderRow.Cells)
                   {
                       Font font = new Font();
                       font.Color = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                       PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
                       pdfCell.BackgroundColor = new BaseColor(gvExcel[i].HeaderStyle.ForeColor);
                       pdfTbl.AddCell(pdfCell);
                   }


                   foreach (GridViewRow gvRow in gvExcel[i].Rows)
                   {
                       foreach (TableCell tblCell in gvRow.Cells)
                       {
                           Font font = new Font();
                           font.Color = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                           PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                           pdfCell.BackgroundColor = new BaseColor(gvExcel[i].RowStyle.ForeColor);
                           pdfTbl.AddCell(pdfCell);
                       }
                   }
                   pdfDoc.Add(pdfTbl);

               }
           }

           pdfDoc.Close();

           //Response.Clear();
           Response.ContentType = "application/pdf";
           Response.AppendHeader("content-disposition", "attachment;filename=report" + startDate + "-" + endDate + ".pdf");
           Response.Write(pdfDoc);
           Response.Flush();
           Response.End();
       }





虽然所有的网格视图都来了,但是它们被卡在一端到另一个前面制作它看起来像一张巨大的单人桌。



Though all the gridviews are coming, but they are stuck one end to other front making it look like a huge single table.


这篇关于使用iTextsharp将多个网格视图转换为单个pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆