Hi
Attached is the iTextSharp.dll reference needed. I made a class a long time ago and converted it iLogic. you shouldnt need to alter any of the code in the MergePDF class, just adjust the rows in the Main Sub at the top.
Place the itextsharp.dll in a local place, mine was placed here
C:\Users\Public\global references
here is the iLogic code
AddReference "C:\Users\Public\global references\itextsharp.dll"
addreference "System.IO"
Sub Main()
'set the path of the folder contaiing all of the pdfs
Dim _Path As String = "C:\MyPath\MyPDF Collection"
'set the name of the new pdf you want to create without the extension
Dim _Name As String = "MyNewPDF"
'get the result, it will be either the path of the PDF or FALSE
Dim MakePDF As New MergePDF(_Path, _Name)
'get the PDF result
Dim OpenPDF As String = MakePDF.GetPDF
'open on condition to the result
Select Case OpenPDF
Case "FALSE"
MsgBox("No PDF was generated")
Case Else
Process.Start(OpenPDF)
End Select
End Sub
Public Class MergePDF
#Region "PRIVATE PROPERTIES"
Private Property _Folder_Path As String = String.Empty
Private Property _PDF_Name As String = Nothing
Private Property _GetPDF As String = String.Empty
#End Region
#Region "FRIEND PROPERTIES THAT YOU CALL BACK"
Friend Property GetPDF As String
Set(value As String)
_GetPDF = value
End Set
Get
Return _GetPDF
End Get
End Property
#End Region
#Region "CONSTRUCTORS"
Public Sub New(ByVal _folderpath As String, ByVal _pdfname As String)
'set the pdf Folder path
_Folder_Path = _folderpath
'set the pdf name
_PDF_Name = _pdfname
'create the PDF and set the path
_GetPDF = CreatePDF(_Folder_Path)
End Sub
#End Region
#Region "HELPERS"
Private Function CreatePDF(ByVal sFolderPath) As String
Dim bOutputfileAlreadyExists As Boolean = False
Dim sOutFilePath As String = IO.Path.Combine(sFolderPath, _PDFName & ".pdf")
'sdet up return for a successful pdf. ret changes to FALSE if any errors occur for qualifying purposes
Dim ret As String = sOutFilePath
If IO.File.Exists(sOutFilePath) Then
Try
IO.File.Delete(sOutFilePath)
Catch ex As Exception
bOutputfileAlreadyExists = True
End Try
End If
Dim iPageCount As Integer = GetPageCount(sFolderPath)
If iPageCount > 0 And bOutputfileAlreadyExists = False Then
Dim oFiles As String() = IO.Directory.GetFiles(sFolderPath)
Dim oPdfDoc As New iTextSharp.text.Document()
Dim oPdfWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(oPdfDoc, New IO.FileStream(sOutFilePath, IO.FileMode.Create))
oPdfDoc.Open()
System.Array.Sort(Of String)(oFiles)
For i As Integer = 0 To oFiles.Length - 1
Dim sFromFilePath As String = oFiles(i)
Dim oFileInfo As New IO.FileInfo(sFromFilePath)
Dim sFileType As String = "PDF"
Dim sExt As String = PadExt(oFileInfo.Extension)
Try
AddPdf(sFromFilePath, oPdfDoc, oPdfWriter)
Catch ex As Exception
ret = "FALSE"
End Try
Next
Try
oPdfDoc.Close()
oPdfWriter.Close()
Catch ex As Exception
Try
IO.File.Delete(sOutFilePath)
Catch ex2 As Exception
End Try
End Try
End If
Dim oFolders As String() = IO.Directory.GetDirectories(sFolderPath)
For i As Integer = 0 To oFolders.Length - 1
Dim sChildFolder As String = oFolders(i)
Dim iPos As Integer = sChildFolder.LastIndexOf("\")
Dim sFolderName As String = sChildFolder.Substring(iPos + 1)
CreatePDF(sChildFolder)
Next
Return ret
End Function
Private Sub AddPdf(ByVal sInFilePath As String, ByRef oPdfDoc As iTextSharp.text.Document, ByRef oPdfWriter As iTextSharp.text.pdf.PdfWriter)
Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
Dim iPage As Integer = 0
Do While (iPage < iNumberOfPages)
iPage += 1
oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
oPdfDoc.NewPage()
Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
If (iRotation = 90) Or (iRotation = 270) Then
oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
Else
oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Loop
End Sub
Private Function PadExt(ByVal s As String) As String
s = UCase(s)
If s.Length > 3 Then
s = s.Substring(1, 3)
End If
Return s
End Function
Private Function GetPageCount(ByVal sFolderPath As String) As Integer
Dim iRet As Integer = 0
Dim oFiles As String() = IO.Directory.GetFiles(sFolderPath)
For i As Integer = 0 To oFiles.Length - 1
Dim sFromFilePath As String = oFiles(i)
Dim oFileInfo As New IO.FileInfo(sFromFilePath)
Dim sFileType As String = "PDF"
Dim sExt As String = PadExt(oFileInfo.Extension)
iRet += 1
Next
Return iRet
End Function
#End Region
End Class
Nacho
Automation & Design Engineer
Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.