简体   繁体   English

通过iTextSharp ASP.NET将二进制图像数据从SQL Server检索为pdf

[英]Retrieve binary image data from SQL Server into pdf via iTextSharp ASP.NET

I'm trying to retrieve binary image data from my SQL server and export it into my pdf file by using the follow method 我正在尝试从我的SQL服务器检索二进制图像数据,并使用follow方法将其导出到我的pdf文件中

phrase.Add(new Chunk("Image :", normalFont));
Byte[] bytes = (Byte[])dr[0];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
phrase.Add(bytes);

table.AddCell(phrase);

When i'm trying to display binary image data into my webapp using the above method, it works perfectly fine. 当我尝试使用上述方法将二进制图像数据显示到我的webapp中时,它完全正常。 Unfortunately when i want to export the image into my pdf file it doesn't work. 不幸的是,当我想将图像导出到我的pdf文件时,它不起作用。

I have error like this on the phrase.Add method. 我在phrase.Add方法上有这样的错误。 I know i'm doing something wrong but i cant figure it out 我知道我做错了但我无法弄明白

在此输入图像描述

Here is my entire back-end code for my PDF button. 这是我的PDF按钮的整个后端代码。

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop\\New folder", filename), FileMode.Create);
        PdfWriter.GetInstance(doc1, output);
        PdfPCell cell = null;
        doc1.Open();

        PdfPTable table = new PdfPTable(1);
        table.TotalWidth = 585f;
        table.LockedWidth = true;

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

        var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
        doc1.Add(new Paragraph("Official Report. Member Report ID : " + DDLCase.SelectedValue, titleFont));

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


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

        SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid,  pr.prdatetime, mr.citizenreport, pr.policereport, aor.officialreport, mr.image1 from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro, AdminOfficialReport aor where mr.memberreportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and pr.policereportid=aor.policereportid", 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(new Chunk("Contact :", normalFont));
            phrase.Add(dr[1].ToString());

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

            phrase.Add(new Chunk("Type Of Crime :", normalFont));
            phrase.Add(dr[2].ToString());

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

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

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

            phrase.Add(new Chunk("Citizen Report Date Time :", normalFont));
            phrase.Add(dr[4].ToString());

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

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

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

            phrase.Add(new Chunk("Police Report Date Time :", normalFont));
            phrase.Add(dr[6].ToString());

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

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

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

            phrase.Add(new Chunk("Police Report :", normalFont));
            phrase.Add(dr[8].ToString());

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

            phrase.Add(new Chunk("Official Report :", normalFont));
            phrase.Add(dr[9].ToString());

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

            phrase.Add(new Chunk("Image :", normalFont));
            Byte[] bytes = (Byte[])dr[0];
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "image/jpg";
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            phrase.Add(bytes);

            table.AddCell(phrase);


        }

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

To add an image when creating a PDF with iTextSharp, you should use the Image class (from the iTextSharp.text namespace). 要在使用iTextSharp创建PDF时添加图像,您应该使用Image类(来自iTextSharp.text命名空间)。 This class has numerous static helper method GetInstance overloads which assist in creating an Image instance, cf. 这个类有许多静态辅助方法GetInstance重载,它们有助于创建一个Image实例,参见 the source . 来源 In your case most likely the overload with only one byte[] argument will do: 在您的情况下,很可能只有一个byte[]参数的重载将执行:

using iTextSharp.text;
[...]
Byte[] bytes = (Byte[])dr[10];
Image image = Image.GetInstance(bytes);
Chunk imageChunk = new Chunk(image, 0, 0);
phrase.Add(imageChunk);

I see two major problems in your code. 我在你的代码中看到了两个主要问题。 There may be others, but let's start with these: 可能还有其他人,但让我们从这些开始:

  1. You have: Response.ContentType = "image/jpg"; 你有: Response.ContentType = "image/jpg"; but you're creating a PDF, and your question shows that you're sending PDF to the browser, NOT an image. 但是你正在创建一个PDF,你的问题表明你正在向浏览器发送PDF,而不是图像。 You need Response.ContentType = "application/pdf" . 你需要Response.ContentType = "application/pdf"
  2. I assume that Byte[] bytes represents an image. 我假设Byte[] bytes代表一个图像。 It beats me why you'd add these image bytes straight to a Phrase object. 它击败了我为什么你要将这些图像字节直接添加到Phrase对象。 How on earth is the Phrase object supposed to know that these bytes should be converted to an Image object? Phrase对象究竟应该知道这些字节应该转换为Image对象吗? You've skipped a couple of steps. 你已经跳过了几个步骤。 First you need to create an Image object, then wrap that image inside a Chunk , then add that chunk to a Phrase . 首先,您需要创建一个Image对象,然后将该图像包装在Chunk ,然后将该块添加到Phrase

For examples on creating images, please browse http://tinyurl.com/itextsharpIIA2C10 有关创建图像的示例,请浏览http://tinyurl.com/itextsharpIIA2C10

If you want to know what wrapping images in chunks is about, please read the final pages of chapter 2 of my book. 如果你想知道块中包装图像的内容,请阅读本书第2章的最后几页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM