我正在使用 asp.net 应用程序,并在其中使用 itexsharp 生成 pdf 文件。我需要将我的 gridview 导出为 pdf,问题是在生成 pdf 后,gridview 始终位于页面顶部。我的要求是我需要页面中心的gridview,在顶部我将通过前端的文本框输入一些文本。请帮助我找到解决方案,提前感谢这里是我的代码按钮点击:
这是我单击按钮生成 PDF 的代码:
{
string name = TextBox1.Text;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Chunk c = new Chunk("Export GridView to PDF Using iTextSharp \n",FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
string s = sw.ToString();
GridView1.AllowPaging = false;
BindDDL();
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
var wri = PdfWriter.GetInstance(pdfDoc, new FileStream("D:/xyz" + ".pdf", FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
string contents = File.ReadAllText(Server.MapPath("~/bill.htm"));
contents = contents.Replace("[CNAME]", name);
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
pdfDoc.Add(htmlElement as IElement);
pdfDoc.Add(p);
System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(s));
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}