如果启用智能模式,则会引发NullReferenceException [英] If I enable Smart Mode, a NullReferenceException is thrown

查看:85
本文介绍了如果启用智能模式,则会引发NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一组PDF合并在一起,并确保它们被正确压缩,并且没有重复的资源.但是,在我的代码中,如果我在编写器上调用SetSmartMode(true),则对其的第一次写入将始终导致NullReferenceException.

I'm trying to merge a group of PDFs together and ensure that they are compressed well, and that there are no duplicate resources. However, in my code, if I call SetSmartMode(true) on my writer, the first write to it will always result in a NullReferenceException.

这是我的(vb.net)代码:

Here is my (vb.net) code:

    Private Function CombinePdfBatch(pdfMetaData As IEnumerable(Of QuestDataSet.MetaDataRow), batchNumber As Integer,
                                 fileNamePrefix As String, outputDir As String) As String

    Dim outputFileName As String = Path.Combine(outputDir, fileNamePrefix & "_" & batchNumber & ".pdf")

    Using combinedPdf As New PdfDocument(New PdfWriter(Path.Combine(outputDir, fileNamePrefix & "_" & batchNumber & ".pdf")).SetSmartMode(True))

        'Make sure we close the underlying stream when we're done with the combination
        combinedPdf.SetCloseWriter(True)
        combinedPdf.SetCloseReader(False)
        combinedPdf.SetFlushUnusedObjects(False)
        combinedPdf.GetWriter().SetCompressionLevel(CompressionConstants.BEST_COMPRESSION)
        combinedPdf.GetWriter().SetCloseStream(True)
        combinedPdf.SetDefaultPageSize(New Geom.PageSize(630, 810))

        Dim merger As New PdfMerger(combinedPdf)

        For Each currentMD As QuestDataSet.MetaDataRow In pdfMetaData
            Using currentPDF As New PdfDocument(New PdfReader(Path.Combine(programPaths.Input, currentMD.ReceivedFilesRowByInputFileRelation.FileName)))
                currentPDF.SetCloseReader(True)
                currentPDF.SetCloseWriter(False)
                currentPDF.GetReader().SetCloseStream(True)

                currentMD.CombinedFileName = outputFileName
                currentMD.StartPage = combinedPdf.GetNumberOfPages() + 1
                merger.Merge(currentPDF, 1, currentPDF.GetNumberOfPages())
                currentMD.EndPage = combinedPdf.GetNumberOfPages()
            End Using
        Next
        merger.Close()            
    End Using

    Return outputFileName
End Function

一旦调用merger.Merge,就会抛出NullReferenceException.我已经用许多其他功能替换了它,但是当编写器处于智能模式"时,如果在PDF中添加了任何内容,则会崩溃.

As soon as merger.Merge is called, a NullReferenceException is thrown. I've replaced that with many other functions, but if anything is added to the PDF when the writer is in Smart Mode, it crashes.

如果禁用智能模式,则PDF会合并.但我需要尽可能减小这些PDF的大小,而又不会牺牲太多的质量.因为我知道它们都使用相同的字体并共享一些图片,所以我想将它们全部合并在一起.

If I disable Smart Mode, the PDFs are merged. But I need to reduce the size of these PDFs as much as possible without sacrificing too much quality. Since I know they all use the same font and share some stock images, I figured I'd combine them all in order to do so.

因为我爱你们,这是一个堆栈跟踪:

Here's a stack trace since I love you guys:

System.NullReferenceException occurred
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=itext.kernel
  StackTrace:
       at iText.Kernel.Pdf.PdfWriter.ByteStore.SerDic(PdfDictionary dic, Int32 level, ByteBufferOutputStream bb, IntHashtable serialized)
       at iText.Kernel.Pdf.PdfWriter.ByteStore.SerObject(PdfObject obj, Int32 level, ByteBufferOutputStream bb, IntHashtable serialized)
       at iText.Kernel.Pdf.PdfWriter.ByteStore..ctor(PdfStream str, IntHashtable serialized)
       at iText.Kernel.Pdf.PdfWriter.SmartCopyObject(PdfObject obj)    
       at iText.Kernel.Pdf.PdfWriter.CopyObject(PdfObject obj, PdfDocument document, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfObject.ProcessCopying(PdfDocument documentTo, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfArray.CopyContent(PdfObject from, PdfDocument document)
       at iText.Kernel.Pdf.PdfWriter.CopyObject(PdfObject obj, PdfDocument document, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfObject.ProcessCopying(PdfDocument documentTo, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfDictionary.CopyContent(PdfObject from, PdfDocument document)
       at iText.Kernel.Pdf.PdfWriter.CopyObject(PdfObject obj, PdfDocument document, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfObject.ProcessCopying(PdfDocument documentTo, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfObject.CopyTo(PdfDocument document, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfDictionary.CopyTo(PdfDocument document, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfDictionary.CopyTo(PdfDocument document, IList`1 excludeKeys, Boolean allowDuplicating)
       at iText.Kernel.Pdf.PdfPage.CopyTo(PdfDocument toDocument, IPdfPageExtraCopier copier)
       at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument, Int32 insertBeforePage, IPdfPageExtraCopier copier)
       at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument, IPdfPageExtraCopier copier)
       at iText.Kernel.Pdf.PdfDocument.CopyPagesTo(IList`1 pagesToCopy, PdfDocument toDocument)
       at iText.Kernel.Utils.PdfMerger.Merge(PdfDocument from, IList`1 pages)
       at iText.Kernel.Utils.PdfMerger.Merge(PdfDocument from, Int32 fromPage, Int32 toPage)
       at QuestMonolithic.Process.CombinePdfBatch(IEnumerable`1 pdfMetaData, Int32 batchNumber, String fileNamePrefix, String outputDir) in C:\Users\cchrist\Documents\Visual Studio 2012\Projects\Quest_Monolithic\trunk\source\Process.vb:line 594
  InnerException: 

推荐答案

这是iText 7 .NET代码中的一个已知错误,并且很快将部署此修复程序. SerDic()方法(仅在以智能模式进行复制时才被调用)处理.NET中错误地检索字典键,从而导致空指针.

It's a known bug in the iText 7 .NET code, and a fix will be deployed soon. The SerDic() method, which is only called when copying in smart-mode handles the retrieval of the dictionary keys incorrectly in .NET, resulting in null-pointers.

如果要自己修复,请替换itext.kernel.PdfWriter中的第592行:

If you want to fix it yourself, replace line 592 in itext.kernel.PdfWriter:

dic.KeySet().ToArray(keys);

使用

keys = dic.KeySet().ToArray(keys);

这篇关于如果启用智能模式,则会引发NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆