PageStamp.AlterContents() NullReferenceException
这就是问题所在:
我有这段代码...
它不断抛出 NullReferenceException stamp.AlterContents();线。
我不知道发生了什么事。任何帮助表示赞赏!
public static byte[] concatAndAddContent(List<byte[]> pdf)
{
byte [] todos;
using(MemoryStream ms = new MemoryStream())
{
Document doc = new Document();
doc.Open();
PdfCopy copy = new PdfCopy(doc, ms);
PdfCopyFields copy2 = new PdfCopyFields(ms);
PdfReader reader;
foreach (byte[] p in pdf)
{
reader = new PdfReader(p);
int pages = reader.NumberOfPages;
// loop over document pages
for (int i = 1; i <= pages; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader, i);
PdfCopy.PageStamp stamp = copy.CreatePageStamp(page);
PdfContentByte cb = stamp.GetUnderContent();
cb.SaveState();
stamp.AlterContents();
copy.AddPage(page);
}
}
doc.Close();
todos = ms.GetBuffer();
ms.Flush();
ms.Dispose();
}
return todos;
}
我在 VisualStudio 2010 中有 iTextSharp 版本 5.0.4
this is the problem:
i've got this piece of code...
It keeps throwing a NullReferenceException at the
stamp.AlterContents(); line.
i've got no clue on what's going on. Any help appreciated!
public static byte[] concatAndAddContent(List<byte[]> pdf)
{
byte [] todos;
using(MemoryStream ms = new MemoryStream())
{
Document doc = new Document();
doc.Open();
PdfCopy copy = new PdfCopy(doc, ms);
PdfCopyFields copy2 = new PdfCopyFields(ms);
PdfReader reader;
foreach (byte[] p in pdf)
{
reader = new PdfReader(p);
int pages = reader.NumberOfPages;
// loop over document pages
for (int i = 1; i <= pages; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader, i);
PdfCopy.PageStamp stamp = copy.CreatePageStamp(page);
PdfContentByte cb = stamp.GetUnderContent();
cb.SaveState();
stamp.AlterContents();
copy.AddPage(page);
}
}
doc.Close();
todos = ms.GetBuffer();
ms.Flush();
ms.Dispose();
}
return todos;
}
i've got iTextSharp version 5.0.4 in VisualStudio 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请发布您的异常堆栈跟踪。
虽然我不确定是什么导致了异常,但我可以告诉您,在没有匹配的
cb.restoreState()
的情况下调用cb.saveState()
是一件坏事。在 Java 版本中,这将抛出 IllegalPdfSyntaxException。在 C# 中,这可能已转换为 NPE,但这似乎有点牵强。当您想做某事然后稍后“返回”时,可以使用保存和恢复状态。例如,
这种事情在 PDF 中经常发生。想要绘制图像吗?您必须更改当前变换矩阵 (CTM),以将图像以所需的尺寸放置在您想要的位置。除非您希望之后的所有内容都进行同样的转换(而您永远不会这样做),否则您必须将矩阵更改和图像绘制包装在保存/恢复块中。当您调用 cb.addImage(...) 时,iText 会为您处理此问题,但它仍然在幕后发生。
Please post your exception stack trace.
While I'm not sure what's causing the exception, I can tell you that calling
cb.saveState()
without a matchingcb.restoreState()
is A Bad Thing. In the Java version, this will throw an IllegalPdfSyntaxException. In C#, this may have been converted to an NPE, but that seems a bit of a stretch.Saving and restoring the state is used when you want to do something and then "go back" later. For example
This sort of thing happens A LOT in PDF. Want to draw an image? You have to change the Current Transformation Matrix (CTM) to put the image where you want at the size you want. Unless you want everything that comes after to be likewise transformed (and you never do), you have to wrap the matrix change and image draw in a save/restore block. iText handles this for you when you call cb.addImage(...), but it's still happening behind the scenes.