【问题标题】:Unable to generate PDF file with '0x' varbinary value ASP.net itextsharp无法使用 '0x' varbinary 值 ASP.net itextsharp 生成 PDF 文件
【发布时间】:2013-06-23 00:58:25
【问题描述】:

我开发了一个能够生成 PDF 文件的 webapp。用户可以选择上传最多 5 张图片。当用户上传5张图片后,我就可以生成PDF文件了。但是,如果用户只选择上传4个,我将无法生成PDF文件,我会收到这个错误。

Index was outside the bounds of the array

如果用户没有上传所有 5 张图片,我已插入默认值“0x”作为 varbinary。这是我如何直接从我的 SQL 服务器获取图像的代码。

phrase.Add(new Chunk("C-IMG1 :\u00a0", normalFont));
            Byte[] bytes1 = (Byte[])dr[8];
            iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(bytes1);
            image1.ScaleToFit(112f, 112f);
            Chunk imageChunk1 = new Chunk(image1, 0, 0);
            phrase.Add(imageChunk1);

            phrase.Add(new Chunk("C-IMG2 :\u00a0", normalFont));
            Byte[] bytes2 = (Byte[])dr[9];
            iTextSharp.text.Image image2 = iTextSharp.text.Image.GetInstance(bytes2);
            image2.ScaleToFit(112f, 112f);
            Chunk imageChunk2 = new Chunk(image2, 0, 0);
            phrase.Add(imageChunk2);

            phrase.Add(new Chunk("C-IMG3 :\u00a0", normalFont));
            Byte[] bytes3 = (Byte[])dr[10];
            iTextSharp.text.Image image3 = iTextSharp.text.Image.GetInstance(bytes3);
            image3.ScaleToFit(112f, 112f);
            Chunk imageChunk3 = new Chunk(image3, 0, 0);
            phrase.Add(imageChunk3);

            phrase.Add(new Chunk("C-IMG4 :\u00a0", normalFont));
            Byte[] bytes4 = (Byte[])dr[11];
            iTextSharp.text.Image image4 = iTextSharp.text.Image.GetInstance(bytes4);
            image4.ScaleToFit(112f, 112f);
            Chunk imageChunk4 = new Chunk(image4, 0, 0);
            phrase.Add(imageChunk4);

            phrase.Add(new Chunk("C-IMG5 :\u00a0", normalFont));
            Byte[] bytes5 = (Byte[])dr[12];
            iTextSharp.text.Image image5 = iTextSharp.text.Image.GetInstance(bytes5);
            image5.ScaleToFit(112f, 112f);
            Chunk imageChunk5 = new Chunk(image5, 0, 0);
            phrase.Add(imageChunk5);

我该如何解决这个问题?已经卡了一两天了。

【问题讨论】:

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


    【解决方案1】:

    您要求 iTextSharp 从空字节创建图像,这将导致错误,您需要添加一些代码来处理空图像情况。我在your other thread 中发帖建议切换到PdfPTable,这将使您的生活更轻松。但是,如果您要继续沿 Phrase/Chunk 路径前进,那么如果找不到图像,则让 SQL Server 返回有效的“默认图像”,或者让您的代码插入“默认图像”。

    例如,从 SQL Server 中,您可以只返回 smallest transparent image possible,如果我正确转换它会是:

    0x47494638396101000100800000ffffff00000021f90400000000002c00000000010001000002024401003b
    

    或者从 .Net 中,您可以生成一个简单的空白图像或从磁盘加载一个。

    此外,如果您将所有内容移入循环,您的代码可能会被简化:

    //Loop through the 5 images
    for (int i = 0; i < 5; i++) {
        //Output the image with the current index (adding 1 since we're starting at zero)
        phrase.Add(new Chunk("C-IMG" + (i + 1).ToString() + " :\u00a0", normalFont));
        //It appears 8 is the "magic number" of the column so add whatever index we're currently on
        Byte[] bytes = (Byte[])dr[8 + i];
        if (bytes.Length == 0) {
            //Do something special with the empty ones
        } else {
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bytes);
            image.ScaleToFit(112f, 112f);
            Chunk imageChunk = new Chunk(image, 0, 0);
            phrase.Add(imageChunk);
        }
    }
    

    【讨论】:

    • 我已将整个代码粘贴到另一个线程中。我确实使用了pdfptable。我使用了块,因为如果我要通过 datareader 获取数据,我无法格式化图片的大小或单词的字体大小。
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2016-01-25
    • 2011-06-29
    • 1970-01-01
    • 2022-10-06
    • 2014-01-01
    • 2017-01-24
    相关资源
    最近更新 更多