0

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

<asp:Button ID="btnPDF" runat="server" Text="Download PDF of Officer's Profile" OnClick="btnPDF_Click" Enabled="false" />

当用户选择一个gridview 数据时,btnPDF 将变为Enable=true。因此,当 btnPDF 启用为 true,并且当用户单击 pdf 按钮时,它应该生成 PDF 并在它之后运行我没有的代码。这就是我的 btnPDF 实际上的样子。我不太确定这可能是由于按钮问题还是pdf的生成问题。

protected void btnPDF_Click(object sender, EventArgs e)
{
    Byte[] bytes;

    using (var MS = new System.IO.MemoryStream())
    {
        var doc = new iTextSharp.text.Document();
        var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, MS);
        doc.Open();
        PdfPTable table = new PdfPTable(1);
        table.TotalWidth = 585f;
        table.LockedWidth = true;

        var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/image/logo.jpg"));
        doc.Add(logo);

        var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
        doc.Add(new Paragraph("Officer's Profile. Officer's Police ID: " + lblPoliceID.Text, titleFont));

        var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD);
        var phrase = new Phrase();
    }

这就是 btnPDF 第一部分的样子。后面会展示从 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 背后的代码也没有运行。

4

1 回答 1

0

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

于 2013-08-14T11:31:09.637 回答