c# - System.IndexOutOfRangeException 在 itextsharp 中添加图像

标签 c# wpf itext

我在使用 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/

相关文章:

java - 如何使用 iText api 提取 PDF 水印内容

c# - 通用抽象基类中的 NUnit 测试被忽略

c# - contenttypeorder 和 uniquecontenttypeorder 之间的区别

c# - 将标志枚举绑定(bind)到包含复选框的列表框

c# - 从 System.Drawing.Bitmap 加载 WPF BitmapImage

java - 写入pdf文件时如何设置字体大小

c# - 使用 Azure Blob 存储中的证书解密 WCF 回调

c# - 图像加载内存泄漏与 C#

c# - WPF:调整 ScrollViewer 内数据网格的宽度

java - 使用 iText 为生成的 PDF 创建单元测试