c# – 从GridView导出为PDF
发布时间:2020-12-15 21:44:11 所属栏目:百科 来源:网络整理
导读:我已经使用iTextSharp从gridview导出到PDF.由于gridview列更多,因此PDF中的列未对齐且非常小.我尝试在codebehind和.aspx页面中编写样式.但规模并没有改变. .aspx asp:GridView ID="grdResult" runat="server" AutoGenerateColumns="true" Width="100%" CellP
我已经使用iTextSharp从gridview导出到PDF.由于gridview列更多,因此PDF中的列未对齐且非常小.我尝试在codebehind和.aspx页面中编写样式.但规模并没有改变.
.aspx <asp:GridView ID="grdResult" runat="server" AutoGenerateColumns="true" Width="100%" CellPadding="3" CellSpacing="3" Font-Size="10pt"> <HeaderStyle Font-Bold="true" Width="250px" /> </asp:GridView> .cs页面 Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition","attachment;filename=MARGEmployees.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); HtmlForm frm = new HtmlForm(); grdResult.Parent.Controls.Add(frm); frm.Attributes["runat"] = "server"; frm.Controls.Add(grdResult); frm.RenderControl(hw); grdResult.HeaderRow.Style.Add("width","15%"); grdResult.HeaderRow.Style.Add("font-size","10px"); grdResult.Style.Add("font-family","Tahoma"); grdResult.Style.Add("font-size","8px"); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4,7f,0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc,Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); 请帮帮我 解决方法
尝试在后面的代码中更改您的pdf文档大小.
//尝试其中任何一个 Document pdfDoc = new Document(PageSize.A3,0f); Document pdfDoc = new Document(PageSize.A2,0f); Document pdfDoc = new Document(PageSize.A1,0f); 由于设置了pdf doc的大小,因此会压缩列.如果你改变尺寸它会工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |