This is a function I have for merging pdf documents streamed from a DB in iTextSharp
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
But I'm having problems figuring out how to do the same thing with iText7.
UPDATE
I have tried this
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
But this throws a 'bouncy castle' error
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()
... Getting a little closer perhaps :-)
After downloading itext7.bouncy-castle-adapter, and fixing a conflict error, this works.
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