我在使用 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
与某种形式的光标位置有关 - 很愚蠢,但是嘿!
关于c# - System.IndexOutOfRangeException 在 itextsharp 中添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898298/