将gridview导出为带有标题和边距0的pdf

时间:2013-12-05 09:53:32

标签: c# asp.net pdf gridview datagrid

我在导出到PDF时遇到问题,因为网格视图包含阿拉伯文本,所以我更改了我的代码并且它正常工作,问题是当我导出它时gridview标头丢失了,我怎么能改变宽度表格?,如何将相同的网格视图外观导出到PDF,另外如何更改导出的PDF的边距?

iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count);
        table.RunDirection = PdfWriter.RUN_DIRECTION_LTR;


        BaseFont bf = BaseFont.CreateFont("c:\\\\windows\\\\fonts\\\\tahoma.ttf", BaseFont.IDENTITY_H, true);
        iTextSharp.text.Font f2 = new iTextSharp.text.Font(bf, 8, iTextSharp.text.Font.NORMAL);


        for (int i = 0; i <= GridView1.Rows.Count-1; i++)
        {
                for (int j = 0; j <= GridView1.Columns.Count - 1; j++)
                {
                    string cellText = Page.Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(100, cellText, f2));

                    table.AddCell(cell);
                }

        }
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);       
        int[] intTblWidth = { 10, 10, 25, 50,25,25,50,10,50,10 };
        table.SetWidths(intTblWidth);
        table.TotalWidth = 500f;
        PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.SetMargins(0, 0, 0, 0);
        pdfDoc.Add(table); // add the table
        pdfDoc.Close();
        Page.Response.ContentType = "application/pdf";
        Page.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.Write(pdfDoc);
        Page.Response.End();

2 个答案:

答案 0 :(得分:0)

阅读以下链接:

您可以根据需要设置保证金

http://www.dotnetspider.com/resources/29759-Exporting-GridView-PDF.aspx

答案 1 :(得分:0)

相关问题