【问题标题】:Retrieve binary image data from SQL Server into pdf via iTextSharp ASP.NET通过 iTextSharp ASP.NET 从 SQL Server 检索二进制图像数据到 pdf
【发布时间】:2013-06-03 07:05:30
【问题描述】:

我正在尝试从我的 SQL 服务器检索二进制图像数据并使用以下方法将其导出到我的 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);

当我尝试使用上述方法将二进制图像数据显示到我的 web 应用程序中时,它工作得非常好。不幸的是,当我想将图像导出到我的 pdf 文件中时,它不起作用。

我在短语.Add 方法上有这样的错误。我知道我做错了什么,但我想不通

这是我的 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();

【问题讨论】:

    标签: c# asp.net sql-server pdf itextsharp


    【解决方案1】:

    我在您的代码中发现了两个主要问题。可能还有其他,但让我们从这些开始:

    1. 您拥有:Response.ContentType = "image/jpg";,但您正在创建 PDF,并且您的问题表明您正在向浏览器发送 PDF,而不是图像。你需要Response.ContentType = "application/pdf"
    2. 我假设Byte[] bytes 代表一个图像。我为什么要将这些图像字节直接添加到Phrase 对象中。 Phrase 对象究竟应该如何知道这些字节应该转换为 Image 对象?你跳过了几个步骤。首先,您需要创建一个Image 对象,然后将该图像包装在Chunk 中,然后将该块添加到Phrase

    有关创建图片的示例,请浏览http://tinyurl.com/itextsharpIIA2C10

    如果你想知道分块包装图像是什么,请阅读我的书chapter 2 的最后几页。

    【讨论】:

      【解决方案2】:

      要在使用 iTextSharp 创建 PDF 时添加图像,您应该使用 Image 类(来自 iTextSharp.text 命名空间)。这个类有许多静态帮助方法GetInstance 重载,它们有助于创建Image 实例,参见。 the source。在您的情况下,很可能只有一个 byte[] 参数的重载会这样做:

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

      【讨论】:

      • 您的意思是在数据读取器中插入代码?短语.Add(new Chunk("Image :", normalFont));字节[]字节=(字节[])博士[10]; iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);短语。添加(图像);我收到了非法元素 32 的错误插入
      • 你在哪一行得到的?你有堆栈跟踪吗?
      • 不,对不起,我不知道。我可以在此处粘贴带有代码和错误的记事本文件filedropper.com/e32
      • 这次我在 Byte[] bytes = (Byte[])dr[0];无法将“System.String”类型的对象转换为“System.Byte[]”类型。
      • 抱歉,Image 将被添加到包裹在 Chunk 中的 Phrase 中,而不是直接添加。
      猜你喜欢
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2014-10-13
      相关资源
      最近更新 更多