使用itextsharp以pdf动态创建网格视图

时间:2014-01-30 12:50:02

标签: c# itextsharp

 protected void ExportToPdf(DataTable dt, string str)
        {
            ArrayList ADA = new ArrayList();
            ADA.Add(FirstAssignment.SelectedItem);
            ADA.Add(SecondAssignment.SelectedItem);
            GridView GridView1 = new GridView();
            GridView GridView2 = new GridView();
            GridView1.ShowHeaderWhenEmpty = true;
            GridView1.AllowPaging = false;
            GridView1.DataSource = dt;

            GridView1.DataBind();
            int i1 = GridView1.Columns.Count;
            GridView1.HeaderRow.Cells[1].Text = ADA[0].ToString();
            GridView1.HeaderRow.Cells[2].Text = ADA[1].ToString();
            GridView1.HeaderRow.Cells[3].Text = "% of Improvement from " + (ADA[0].ToString()) + " to " + (ADA[1].ToString());
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + str + ".pdf");

            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);


            GridView1.AllowPaging = false;
            GridView1.HeaderRow.ForeColor = System.Drawing.Color.Black;
            GridView1.FooterRow.ForeColor = System.Drawing.Color.Black;

            GridView1.HeaderRow.Style.Add("font-Color", "Black");
            GridView1.HeaderRow.Style.Add("font-size", "13px");
            GridView1.HeaderRow.Style.Add("text-decoration", "none");
            GridView1.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

            GridView1.Style.Add("font-Color", "Black");
            GridView1.Style.Add("text-decoration", "none");
            GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            GridView1.Style.Add("font-size", "11px");
            GridView1.ForeColor = System.Drawing.Color.Black;
            Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            GridView1.RenderControl(hw);

            StringReader sr = new StringReader(sw.ToString());
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

            // HTMLWorker htmlparser1 = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
            pdfDoc.Open();
            pdfDoc.Add(new Paragraph("hey! rashmi"));
            htmlparser.Parse(sr);

            pdfDoc.Close();
            HttpContext.Current.Response.Write(pdfDoc);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

这用于创建一个网格视图。任何人都可以帮助我使用这个代码创建一个动态网格视图。我试图给出一个for循环GridView1.RenderControl(hw);但我正在获得具有相同数据的单个网格视图。谢谢你提前。

1 个答案:

答案 0 :(得分:0)

如果你想在pdf中显示gridview,你可以在这里显示为表格 代码:

     public void createPdf()
             {
            // step 1
                 using (MemoryStream ms = new MemoryStream())
                 {
                     Document document = new Document(iTextSharp.text.PageSize.A4, 10, 22, 34, 34);
                     // step 2
                     PdfWriter writer = PdfWriter.GetInstance(document, ms);
                     // step 3
                     document.Open();
                     MemoryStream stream = new MemoryStream();
                     // step 4 how many tables you want to create
                     for (int i = 0; i < 5; i++)
                     {
                         document.Add(new Paragraph("Table:"));
                         document.Add(createFirstTable());

                     }                     
 // step 5
                     document.Close();
                     writer.Close();
                     Response.ContentType = "pdf/application";
                     Response.AddHeader("content-disposition",
                     "attachment;filename=First PDF document.pdf");
                     Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                 }
             }

            public static PdfPTable createFirstTable(DataTable dt)
            {


                // a table with three columns
                PdfPTable table = new PdfPTable(dt.Columns.Count);
                // the cell object
                PdfPCell cell;
                // we add a cell with colspan 3
                //cell = new PdfPCell(new Phrase("Cell with colspan 3"));


               table.AddCell(cell);
                //// now we add a cell with rowspan 2
                cell = new PdfPCell(new Phrase("Cell with rowspan 2"));

                    table.AddCell("ADXHGS");
                table.AddCell("WFEWSA");
                table.AddCell("EWSFCEDSW");
                table.AddCell("EWSEWSFDFCEDSW");



                //PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table            

                return table;
            }