PageStamp.AlterContents() NullReferenceException

发布于 2024-09-27 20:35:01 字数 1379 浏览 0 评论 0原文

这就是问题所在:

我有这段代码...

它不断抛出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-10-04 20:35:01

请发布您的异常堆栈跟踪。

虽然我不确定是什么导致了异常,但我可以告诉您,在没有匹配的 cb.restoreState() 的情况下调用 cb.saveState() 是一件坏事。在 Java 版本中,这将抛出 IllegalPdfSyntaxException。在 C# 中,这可能已转换为 NPE,但这似乎有点牵强。

当您想做某事然后稍后“返回”时,可以使用保存和恢复状态。例如,

Draw some stuff
save the state
create a clipping region
   draw some clipped stuff
restore the state
draw some unclipped stuff.

这种事情在 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 matching cb.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

Draw some stuff
save the state
create a clipping region
   draw some clipped stuff
restore the state
draw some unclipped stuff.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文