ASP.Net C#生成并下载PDF文件

时间:2011-04-25 13:30:38

标签: c# asp.net pdf

我正在尝试生成并下载pdf文件。它一直说文件类型错误或文件已损坏。

这是我的代码

 protected void downloadFile(int index)
    {

        string strContent = "<html> <body> <div> <b> Tjeu! </b> </div> <div> <INPUT TYPE=CHECKBOX NAME=\"maillist\" disabled=\"disabled\">Yes! Put me on the list! </div> </body> </html>";
        string attach = "attachment; filename=wordtest.pdf";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;

        HttpContext.Current.Response.AddHeader("content-disposition", attach);

        HttpContext.Current.Response.Write(strContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
    }

请求帮助;)

1 个答案:

答案 0 :(得分:2)

因为strContent是HTML字符串,而不是PDF文件的内容。

PDF有自己的格式,并不是那么容易用你期望在这里使用的方式创建的,事实上,甚至专注于编写'PDF Writer'将是一项非常出色的工作 - 那里已经有人工作/正在研究这个,所以,如果你真的需要PDF,我建议你使用第三方库。

这是一个,例如:http://sourceforge.net/projects/pdfsharp/