Imports System Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.pdf Imports System.Collections.Generic Public NotInheritable Class PDFJoiner Public Shared Sub JoinPDFs(ByVal Masterpdf As String, ByVal sourcefiles As String()) Dim f As Integer = 0 ' we create a reader for a certain document ' owner password as second param requires a byte array Dim pword As Byte() pword = Base.Utility.StrToByteArray("jellopudding") Dim reader As New PdfReader(sourcefiles(0), pword) ' we retrieve the total number of pages Dim n As Integer = reader.NumberOfPages ' Console.WriteLine("There are " & n & " pages in the original file.") ' step 1: creation of a document-object Dim document As New Document(reader.GetPageSizeWithRotation(1)) ' step 2: we create a writer that listens to the document Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(Masterpdf, FileMode.Create)) ' step 3: we open the document document.Open() Dim cb As PdfContentByte = writer.DirectContent Dim page As PdfImportedPage Dim rotation As Integer ' step 4: we add content f = 1 While f < sourcefiles.Length Dim i As Integer = 0 While i < n i += 1 document.SetPageSize(reader.GetPageSizeWithRotation(i)) document.NewPage() page = writer.GetImportedPage(reader, i) rotation = reader.GetPageRotation(i) If rotation = 90 Or rotation = 270 Then cb.AddTemplate(page, 0, -1, 1, 0, 0, reader.GetPageSizeWithRotation(i).Height) Else cb.AddTemplate(page, 1, 0, 0, 1, 0, 0) End If End While System.Diagnostics.Debug.WriteLine("Processed document " & f & " - " & sourcefiles(f)) f += 1 If f < sourcefiles.Length Then reader = New PdfReader(sourcefiles(f), pword) ' we retrieve the total number of pages n = reader.NumberOfPages System.Diagnostics.Debug.WriteLine("There are " & n & " pages in the original file.") End If End While document.Close() End Sub End Class