我遇到了使用itextsharp将图像添加到pdf的问题:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.util.zlib.Tree.d_code(Int32 dist)
at System.util.zlib.Deflate.compress_block(Int16[] ltree, Int16[] dtree)


代码:

 cb.BeginLayer(pdfLayer);
 iTextSharp.text.Image img = TextSharp.text.Image.GetInstance(memoryStream);
 img.SetAbsolutePosition(
      (float)(((resBounds.X - rectPage.X) / pageInfo.Dpi * 72.0f) * pageInfo.PrintOptions.Scale),
      (float)((pageHeight * pageInfo.PrintOptions.Scale) - ((resBounds.Y + resBounds.Height) / pageInfo.Dpi * 72.0f) * pageInfo.PrintOptions.Scale));
 img.ScaleAbsolute((float)((resBounds.Width / pageInfo.Dpi * 72.0f) * pageInfo.PrintOptions.Scale),
      (float)((resBounds.Height / pageInfo.Dpi * 72.0f) * pageInfo.PrintOptions.Scale));
 cb.AddImage(img);
 cb.EndLayer();


错误发生在以下行:
cb.AddImage(img);

最佳答案

尝试将流重新设置为开始-我遇到了这个问题,并设法通过以下方法解决了该问题:

memoryStream.Seek(0, SeekOrigin.Begin); //go back to start


与某种形式的光标位置有关–很傻,但是嘿!

10-06 03:45