此代码确实将我的gridview导出为pdf,但部分文本被截断。什么是我在pdf文档中确定gridview / table的宽度?
GridView1.Columns[0].Visible = false;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Equipment.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView1);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);//create a document object
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);//get a pdf writer instance
pdfDoc.Open();//open the document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);//invoke XMLWorkerHelper.getInstance().parseXhtml(writer, document, new FileInputStream)
pdfDoc.Close();//close the document
Response.Write(pdfDoc);
Response.End();
答案 0 :(得分:0)
如果没有特定的HTML,很难重现您的确切问题。我首先看一下RenderControl
生成的HTML。如果没有任何特定的width
命令,XmlWorker
将根据内容尝试将内容放到页面上。如果列中的所有文本/对象的最大长度为50,那么该列将基本上具有宽度(可能加上一些填充)。对每一列重复此操作,您就可以获得表格的宽度。如果它比文档宽,那么将通过包装文本来缩小列,直到它适合。