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

ASP.Net C#生成并下载PDF文件,c#,asp.net,pdf,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\"

我正在尝试生成并下载一个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();
    }
受保护的void下载文件(int索引)
{
string strContent=“Tjeu!是的!把我放在名单上!”;
string attach=“附件;文件名=wordtest.pdf”;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset=“”;
HttpContext.Current.Response.ContentType=System.Net.Mime.MediaTypeNames.Application.Pdf;
HttpContext.Current.Response.AddHeader(“内容处置”,附件);
HttpContext.Current.Response.Write(strContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}

谢谢您的帮助;)

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

PDF有它自己的格式,用你希望在这里使用的方法创建它不是那么容易,事实上,即使你致力于编写一个“PDF编写器”也是一项了不起的工作——已经有人在做这个了,所以,如果你真的需要PDF,我建议你使用第三方库


例如,这里有一个例子:

pdf文件的内容是什么样子的?我必须生成复选框和其他html文件使用一些第三方工具来创建PDF文件。iTextSharp是个不错的选择。@Evert:是否有使用PDF的要求?你不能使用HTML吗?例如,当你在记事本中打开现有的PDF文件时,你不会看到HTML。将HTML文件重命名为.PDF不会使其成为PDF。