如果我启用智能模式,则会抛出 NullReferenceException

If I enable Smart Mode, a NullReferenceException is thrown

我正在尝试将一组 PDF 合并在一起,并确保它们压缩得很好,并且没有重复的资源。但是,在我的代码中,如果我在编写器上调用 SetSmartMode(true),第一次写入它总是会导致 NullReferenceException。

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

    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 添加任何内容,它就会崩溃。

如果我禁用智能模式,PDF 将被合并。但是我需要尽可能地减小这些 PDF 的大小 而不会 牺牲太多质量。因为我知道他们都使用相同的字体并共享一些库存图片,所以我想我应该将它们全部组合起来才能做到这一点。

编辑:这是堆栈跟踪,因为我爱你们:

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 中错误地处理字典键的检索,导致空指针。

如果您想自己修复它,请将 itext.kernel.PdfWriter 中的第 592 行替换为:

dic.KeySet().ToArray(keys);

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