Esta é uma função que tenho para mesclar documentos PDF transmitidos de um banco de dados no 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
Mas estou tendo problemas para descobrir como fazer a mesma coisa com o iText7.
ATUALIZAR
Eu tentei isso
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
Mas isso gera um erro de 'castelo inflável'
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()
... Chegando um pouco mais perto talvez :-)
Depois de baixar itext7.bouncy-castle-adapter e corrigir um erro de conflito, isso funciona.