Itextsharp pdf GridView定位

时间:2013-09-16 06:05:19

标签: c# asp.net pdf gridview

我正在使用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();
}

1 个答案:

答案 0 :(得分:0)

按如下方式设置gridview属性以将其置于中心

<asp:GridView ID="GridView1" RowStyle  HorizontalAlign="Center" ...>
你真的不需要...... 但请将txtboxgridview放在table