-1

我的页面中有 11 个网格视图我想将它导出到单个 pdf 文件中,我搜索了这个查询并实现了我得到的代码,但它没有工作并生成带有黑色背景的 pdf,只有标题不显示内容。请提供将所有 10 个网格视图导出到单个 pd 的代码

protected void btnExportToPDF_Click(object sender, EventArgs e) 
    { 
    GridView[] gvpdf = new GridView[] { GV1,GV2,GV3,GV4,GV5,GV6,GV7,GV8,GV9,GV10,GV11}; 
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); 
    MemoryStream ms = new MemoryStream(); 
    PdfWriter.GetInstance(pdfDoc, ms); 
    pdfDoc.Open();
    
    
            for (int i = 0; i < gvpdf.Length; i++)
            {
                if (gvpdf[i].Visible)
                {
    
                    PdfPTable pdfTbl = new PdfPTable(gvpdf[i].HeaderRow.Cells.Count);
                    pdfTbl.SpacingAfter = 20f;
    
                    foreach (TableCell headerTblCell in gvpdf[i].HeaderRow.Cells)
                    {
                        System.Drawing.Font font = new System.Drawing.Font("Arial", 24, FontStyle.Bold);
                        font.Color = new BaseColor(gvpdf[i].HeaderStyle.ForeColor);
                        PdfPCell pdfCell = new PdfPCell(new Phrase(headerTblCell.Text));
                        pdfCell.BackgroundColor = new BaseColor(gvpdf[i].HeaderStyle.ForeColor);
                        pdfTbl.AddCell(pdfCell);
                    }
    
    
                    foreach (GridViewRow gvRow in gvpdf[i].Rows)
                    {
                        foreach (TableCell tblCell in gvRow.Cells)
                        {
                            System.Drawing.Font font = new System.Drawing.Font("Arial", 24, FontStyle.Bold);
                            font.Color = new BaseColor(gvpdf[i].RowStyle.ForeColor);
                            PdfPCell pdfCell = new PdfPCell(new Phrase(tblCell.Text));
                            pdfCell.BackgroundColor = new BaseColor(gvpdf[i].RowStyle.ForeColor);
                            pdfTbl.AddCell(pdfCell);
                        }
                    }
                    pdfDoc.Add(pdfTbl);
                }
            }
    
            pdfDoc.Close();
    
    
            byte[] content = ms.ToArray();
            Response.ContentType = "application/pdf";
            Response.AppendHeader("content-disposition", "attachment;filename=report_" + ".pdf");
            Response.BinaryWrite(content);
            Response.Flush();
            Response.End();
        }
4

1 回答 1

0

您可能想尝试另一种方法。只需从提到的网格中读取所有数据,将其存储在 POCO 类中,然后使用一些库将此信息转换为具有所需格式的 PDF 文件。

就个人而言,我推荐一个名为QuestPDF的免费开源库(请注意,我是它的创建者)。它提供了一个易于使用的 fluent API、详细的文档一个入门教程,展示了如何生成发票文档。

于 2021-04-12T10:35:50.170 回答