C# 无法从我的asp:按钮生成pdf文件

C# 无法从我的asp:按钮生成pdf文件,c#,asp.net,pdf,itextsharp,C#,Asp.net,Pdf,Itextsharp,我访问过iTextSharp参考资料,我使用他们的示例代码编写了一个pdf格式。这是我的btnPDF的源代码 <asp:Button ID="btnPDF" runat="server" Text="Download PDF of Officer's Profile" OnClick="btnPDF_Click" Enabled="false" /> 这是btnPDF第一部分的外观。Behind将显示从SQL数据库检索不同数据的SQL命令 SqlConnection con = n

我访问过iTextSharp参考资料,我使用他们的示例代码编写了一个pdf格式。这是我的btnPDF的源代码

<asp:Button ID="btnPDF" runat="server" Text="Download PDF of Officer's Profile" OnClick="btnPDF_Click" Enabled="false" />
这是btnPDF第一部分的外观。Behind将显示从SQL数据库检索不同数据的SQL命令

SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");

    SqlCommand cm = new SqlCommand("Select pa.fullname, pp.profilepicture, pp.rank, pa.email, pa.contact, pa.address, pa.postedto, pp.achievement, pp.medal1, pp.medal2, pp.medal3, pp.medal4, pp.medal5 From PoliceAccount pa, PoliceProfile pp Where pa.policeid = '" + lblPoliceID.Text + "' And pa.policeid = pp.policeid", con);
    con.Open();
    SqlDataReader dr;

    dr = cm.ExecuteReader();

    if (dr.Read())
    {
        phrase.Add(new Chunk("Full Name :", normalFont));
        phrase.Add(dr[0].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Profile Picture :\u00a0", normalFont));
        Byte[] bytes1 = (Byte[])dr[1];
        iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
        image1.ScaleToFit(100f, 250f);
        Chunk imageChunk1 = new Chunk(image1, 0, 0);
        phrase.Add(imageChunk1);

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Rank : ", normalFont));
        Byte[] bytes2 = (Byte[])dr[2];
        iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(bytes2);
        image2.ScaleToFit(40f, 300f);
        Chunk imageChunk2 = new Chunk(image2, 0, 0);
        phrase.Add(imageChunk2);

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Email :", normalFont));
        phrase.Add(dr[3].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Contact :", normalFont));
        phrase.Add(dr[4].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Address :", normalFont));
        phrase.Add(dr[5].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Posted To :", normalFont));
        phrase.Add(dr[6].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Achievement :", normalFont));
        phrase.Add(dr[7].ToString());

        phrase.Add(Chunk.NEWLINE);
        phrase.Add(Chunk.NEWLINE);

        phrase.Add(new Chunk("Medal1", normalFont));
        Byte[] bytes3 = (Byte[])dr[8];
        iTextSharp.text.Image image3 = iTextSharp.text.Image.GetInstance(bytes3);
        image3.ScaleToFit(800f, 800f);
        Chunk imageChunk3 = new Chunk(image3, 0, 0);
        phrase.Add(imageChunk3);

        phrase.Add(new Chunk("Medal2", normalFont));
        Byte[] bytes5 = (Byte[])dr[9];
        iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
        image5.ScaleToFit(800f, 800f);
        Chunk imageChunk5 = new Chunk(image5, 0, 0);
        phrase.Add(imageChunk5);

        phrase.Add(new Chunk("Medal3", normalFont));
        Byte[] bytes6 = (Byte[])dr[10];
        iTextSharp.text.Image image6 = iTextSharp.text.Image.GetInstance(bytes6);
        image6.ScaleToFit(800f, 800f);
        Chunk imageChunk6 = new Chunk(image6, 0, 0);
        phrase.Add(imageChunk6);

        phrase.Add(new Chunk("Medal4", normalFont));
        Byte[] bytes7 = (Byte[])dr[11];
        iTextSharp.text.Image image7 = iTextSharp.text.Image.GetInstance(bytes7);
        image7.ScaleToFit(800f, 800f);
        Chunk imageChunk7 = new Chunk(image7, 0, 0);
        phrase.Add(imageChunk7);

        phrase.Add(new Chunk("Medal5", normalFont));
        Byte[] bytes8 = (Byte[])dr[12];
        iTextSharp.text.Image image8 = iTextSharp.text.Image.GetInstance(bytes8);
        image8.ScaleToFit(800f, 800f);
        Chunk imageChunk8 = new Chunk(image8, 0, 0);
        phrase.Add(imageChunk8);

        table.AddCell(phrase);
    }

    dr.Close();
    doc.Add(table);
    doc.Close();

    bytes = MS.ToArray();
}
为了允许浏览器在尝试下载PDF文件时提示“另存为”对话框,我添加了内容位置

Response.Clear();

Response.ContentType = "application/pdf";

String fileName = "OfficerID " + (String)Session["policeid"];

Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".pdf");

Response.BinaryWrite(bytes);

Response.End();

我已经检查过了,没有发现任何问题。但我不知道为什么当我单击btnPDF时,什么也没有发生,btnPDF后面的代码也没有运行。

您的代码在我的系统中工作正常。
首先检查您的所有图像路径。加载图像时出现问题。

如果单击事件未触发,则问题与生成pdf无关。你发错代码了。我不太确定它没有开火。我用标签测试了一下,效果很好。但是在添加了与pdf相关的代码之后,单击事件似乎没有触发。在单击事件处理程序的第一行上放置一个断点,然后运行网站。单击按钮时断点是否被击中?如果在按钮上设置Enabled=“true”,断点是否有效?
Response.Clear();

Response.ContentType = "application/pdf";

String fileName = "OfficerID " + (String)Session["policeid"];

Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".pdf");

Response.BinaryWrite(bytes);

Response.End();