从SQL Server检索二进制图像数据转换成通过iTextSharp的ASP.NET PDF [英] Retrieve binary image data from SQL Server into pdf via iTextSharp ASP.NET

查看:136
本文介绍了从SQL Server检索二进制图像数据转换成通过iTextSharp的ASP.NET PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的SQL服务器上检索二进制图像数据,并通过如下方法将其导出到我的PDF文件

  phrase.Add(新组块(图像,normalFont));
字节[]字节=(字节[])博士[0];
将Response.Buffer =真;
Response.Charset的=;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =图像/ JPG
Response.BinaryWrite(字节);
Response.Flush();
到Response.End();
phrase.Add(字节);table.AddCell(短语);

当我试图显示二进制图像数据转换成用上面的方法我的web应用程序,它完美的罚款。不幸的是,当我想要将图片导出到我的PDF文件,这是行不通的。

我对phrase.Add法这样的错误。我知道我做错了,但我不能弄明白

下面是我的整个后端code为我的PDF按钮。

 保护无效btnPDF_Click(对象发件人,EventArgs的发送)
    {        VAR DOC1 =新的文件();
        变种文件名=MyTestPDF+ DateTime.Now.ToString(yyyyMMddHHmmssfff)+.PDF;
        无功输出=新的FileStream(Path.Combine(C:\\\\ \\\\用户\\\\ apr12mpsip桌面\\\\新建文件夹,文件名),FileMode.Create);
        PdfWriter.GetInstance(DOC1,输出);
        PdfPCell细胞=无效;
        doc1.Open();        PdfPTable表=新PdfPTable(1);
        table.TotalWidth = 585f;
        table.LockedWidth = TRUE;        VAR标志= iTextSharp.text.Image.GetInstance(使用Server.Mappath(〜/图像/ logo.jpg));
        doc1.Add(标识);        VAR titleFont = FontFactory.GetFont(宋体,18,Font.BOLD);
        doc1.Add(新段(官方报告会员报告编号:+ DDLCase.SelectedValue,titleFont));        变种normalFont = FontFactory.GetFont(FontFactory.HELVETICA,14,Font.BOLD);
        VAR短语=新词();
        SqlConnection的CON =新的SqlConnection(数据源=本地主机;初始目录=项目;集成安全性= SSPI);        的SqlCommand厘米=新的SqlCommand(选择lro.fullname,lro.contact,mr.typeofcrime,mr.location,mr.crdatetime,pr.policeid,pr.prdatetime,mr.citizenreport,pr.policereport,aor.officialreport,先生。从MemberReport image1的先生,PoliceReport PR,LoginRegisterOthers LRO,AdminOfficialReport AOR那里mr.memberreportid ='+ DDLCase.SelectedValue +'和mr.memberreportid = pr.memberreportid和pr.policereportid = aor.policereportid,CON);
        con.Open();
        SqlDataReader的博士;        博士= cm.ExecuteReader();        如果(dr.Read())
        {            phrase.Add(新组块(全名,normalFont));
            phrase.Add(DR [0]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(联系人,normalFont));
            phrase.Add(DR [1]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(式的罪,normalFont));
            phrase.Add(DR [2]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(位置:normalFont));
            phrase.Add(博士[3]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(公民报告日期时间,normalFont));
            phrase.Add(DR [4]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(警察ID:normalFont));
            phrase.Add(DR [5]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(警察报告日期时间,normalFont));
            phrase.Add(DR [6]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(公民报告,normalFont));
            phrase.Add(DR [7]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(警察报告:normalFont));
            phrase.Add(DR [8]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(官方报告:normalFont));
            phrase.Add(DR [9]的ToString());            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);
            phrase.Add(Chunk.NEWLINE);            phrase.Add(新组块(图像,normalFont));
            字节[]字节=(字节[])博士[0];
            将Response.Buffer =真;
            Response.Charset的=;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType =图像/ JPG
            Response.BinaryWrite(字节);
            Response.Flush();
            到Response.End();
            phrase.Add(字节);            table.AddCell(短语);
        }        dr.Close();
        doc1.Add(表);
        doc1.Close();


解决方案

要添加创建带有iTextSharp的一个PDF图像时,你应该使用图片类(从 iTextSharp.text 命名空间)。这个类有许多静态辅助方法的GetInstance 重载这有助于创建一个图片例如,参<一href=\"http://sourceforge.net/p/itextsharp/$c$c/HEAD/tree/trunk/src/core/iTextSharp/text/Image.cs#l347\"相对=nofollow>来源。你的情况很可能只有一个过载字节[] 参数就可以了:

 使用iTextSharp.text;
[...]
字节[]字节=(字节[])博士[10];
形象画像= Image.GetInstance(字节);
块imageChunk =新块(图像,0,0);
phrase.Add(imageChunk);

I'm trying to retrieve binary image data from my SQL server and export it into my pdf file by using the follow method

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. Unfortunately when i want to export the image into my pdf file it doesn't work.

I have error like this on the phrase.Add method. I know i'm doing something wrong but i cant figure it out

Here is my entire back-end code for my PDF button.

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). This class has numerous static helper method GetInstance overloads which assist in creating an Image instance, cf. the source. In your case most likely the overload with only one byte[] argument will do:

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

这篇关于从SQL Server检索二进制图像数据转换成通过iTextSharp的ASP.NET PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆