0

我正在尝试将一组 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就会抛出 a。我已经用许多其他功能替换了它,但是如果在编写器处于智能模式时将任何内容添加到 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: 
4

1 回答 1

4

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

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

dic.KeySet().ToArray(keys);

keys = dic.KeySet().ToArray(keys);
于 2016-08-26T07:24:30.300 回答