使用字节数组添加多个ITextSharp二进制映像会导致IndexOutOfRangeException

时间:2017-02-21 05:18:11

标签: javascript c# asp.net itext html2canvas

我正在使用 ITextSharp html2canvas 添加二进制图片 PDF生成

以下是我的Codebehind代码的摘要。

var overallResultsBase64string = hdnOverallResults.Value.Replace("data:image/png;base64,", "");
var appServerCapacityBase64string = hdnAppServerCapacity.Value.Replace("data:image/png;base64,", "");
var appPerformanceBase64string = hdnAppPerformance.Value.Replace("data:image/png;base64,", "");
var dbServerCapacityBase64string = hdnDBServerCapacity.Value.Replace("data:image/png;base64,", "");
var dbPerformanceBase64string = hdnDBPerformance.Value.Replace("data:image/png;base64,", "");

iTextSharp.text.Image overallResultsImg, appServerCapacityImg, appPerformanceImg
  , dbServerCapacityImg, dbPerformanceImg;

byte[] overallResultsBytes = Convert.FromBase64String(overallResultsBase64string);
overallResultsImg = iTextSharp.text.Image.GetInstance(overallResultsBytes);
byte[] appServerCapacityBytes = Convert.FromBase64String(appServerCapacityBase64string);
appServerCapacityImg = iTextSharp.text.Image.GetInstance(appServerCapacityBytes);
byte[] appPerformanceBytes = Convert.FromBase64String(appPerformanceBase64string);
appPerformanceImg = iTextSharp.text.Image.GetInstance(appPerformanceBytes);
byte[] dbServerCapacityBytes = Convert.FromBase64String(dbServerCapacityBase64string);
dbServerCapacityImg = iTextSharp.text.Image.GetInstance(dbServerCapacityBytes);
byte[] dbPerformanceBytes = Convert.FromBase64String(dbPerformanceBase64string);
dbPerformanceImg = iTextSharp.text.Image.GetInstance(dbPerformanceBytes);

document.Add(overallResultsImg);
document.Add(new Paragraph("OVERALL APPLICATION PERFORMANCE"));
document.Add(appPerformanceImg);
document.Add(dbPerformanceImg);
document.Add(new Paragraph("OVERALL SERVER CAPACITY"));
document.Add(appServerCapacityImg);
document.Add(dbServerCapacityImg);

var appServerCapacityBase64string NULL,它会引发IndexOutOfBounds例外

byte[] appServerCapacityBytes = Convert.FromBase64String(appServerCapacityBase64string);
appServerCapacityImg = iTextSharp.text.Image.GetInstance(appServerCapacityBytes);

我的 Javascript html2canvas 代码有问题吗?请检查。

<script>
   html2canvas($("#overallResultsDiv"), {
        onrendered: function (canvas) {
            // canvas is the final rendered <canvas> element
            var myImage = canvas.toDataURL("image/png");
            document.getElementById("hdnOverallResults").value = myImage;
        }
    });

    html2canvas($("#appServerCapacityDiv"), {
        onrendered: function (canvas) {
            // canvas is the final rendered <canvas> element
            var myImage = canvas.toDataURL("image/png");
           $("#hdnAppServerCapacity").value = myImage;
        }
    });

    html2canvas($("#appPerformanceDiv"), {
        onrendered: function (canvas) {
            // canvas is the final rendered <canvas> element
            var myImage = canvas.toDataURL("image/png");
            document.getElementById("hdnAppPerformance").value = myImage;
        }
    });

    html2canvas($("#dbServerCapacityDiv"), {
        onrendered: function (canvas) {
            // canvas is the final rendered <canvas> element
            var myImage = canvas.toDataURL("image/png");
            document.getElementById("hdnDBServerCapacity").value = myImage;
        }
    });

    html2canvas($("#dbPerformanceDiv"), {
        onrendered: function (canvas) {
            // canvas is the final rendered <canvas> element
            var myImage = canvas.toDataURL("image/png");
            document.getElementById("hdnDBPerformance").value = myImage;
        }
    });
</script>

第一个html2canvas 成功base64 string传递给相应的HiddenField作为值,并且成功CodeBehind中访问。我的脚本有什么问题导致5 HiddenFields返回 NULL

1 个答案:

答案 0 :(得分:0)

您是否尝试将var myImage = canvas.toDataURL("image/png");更改为功能中的其他名称?