c# – 从GridView导出为PDF

我已经使用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的大小,因此会压缩列.如果你改变尺寸它会工作.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...