1

我正在开发一个应用程序,如果用户需要,我想在其中支持将数据导出为 pdf,然后他可以将网格数据转换为 pdf。我尝试了很多方法,但没有奏效。它通过使用 itextsharp 第三方 dll 而不是直接工作。使用 :c# ,asp.net

protected void btnExportPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition",
     "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f,10f,10f,0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End(); 
}
4

3 回答 3

0

Pdf 代表可移植文档格式。由于它是一种标准格式,因此您必须仅以该格式放置数据。否则,它将是一个损坏的文件,不会被 pdf 阅读器赞美。您将不得不使用 3rd 方工具或自己动手。

于 2013-01-04T05:07:52.343 回答
0

编写一个像样的 PDF 解析器是一项巨大的工作(我们说的是几年),所以大多数人发现它更具成本效益,因此许可 IText 或类似的库。

于 2013-01-04T08:36:42.473 回答
-1

如果你想用代码来做,你可以使用 isharptext 来做到这一点,而不是你必须编写 parser 。

于 2013-01-04T10:43:13.087 回答