这是我用于合并 iTextSharp 中从数据库流式传输的 pdf 文档的功能
Public Function PDF_Functions_AddPages(PDFByteContent As List(Of Byte())) As Byte()
Try
Using MS As New System.IO.MemoryStream
Using vDoc As New iTextSharp.text.Document()
Using vCopy As New PdfSmartCopy(vDoc, MS)
vDoc.Open()
For Each Row In PDFByteContent
Using vReader As New PdfReader(Row)
vCopy.AddDocument(vReader)
End Using
Next
vDoc.Close()
End Using
End Using
Return MS.ToArray()
End Using
Catch ex As Exception
EmailError(ex)
Return Nothing
End Try
End Function
但是我在弄清楚如何使用 iText7 做同样的事情时遇到了问题。
更新
我试过
Function PDF_AddPages(PDFByteContent As List(Of Byte())) As Byte()
Try
Using MS As New System.IO.MemoryStream
Using vWriter As New iText.Kernel.Pdf.PdfWriter(MS)
Using vMerged As New iText.Kernel.Pdf.PdfDocument(vWriter)
Dim vMerger As New iText.Kernel.Utils.PdfMerger(vMerged)
For Each Row In PDFByteContent
Using copyFromMS As New MemoryStream(Row)
Using vReader As New iText.Kernel.Pdf.PdfReader(copyFromMS)
Using copyFromDoc As New iText.Kernel.Pdf.PdfDocument(vReader)
vMerger.Merge(copyFromDoc, 1, copyFromDoc.GetNumberOfPages())
End Using
End Using
End Using
Next
End Using
End Using
Return MS.ToArray()
End Using
Catch ex As Exception
EmailError(ex, 2226, PageName)
Return Nothing
End Try
End Function
但这会引发“充气城堡”错误
System.TypeInitializationException: The type initializer for 'iText.Bouncycastleconnector.BouncyCastleFactoryCreator' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at iText.Bouncycastleconnector.BouncyCastleFactoryCreator..cctor()
...也许更接近一点了:-)
下载 itext7.bouncy-castle-adapter 并修复冲突错误后,就可以正常工作了。