- Code: Select all
mports Spire.PDF
Module Module_CombineFilestoPDF
Public Function CombineFilestoPDF(RawFileArray() As String, FinalPDFPath As String, FinalPDFName As String) As String
'This function recieves an array of filepaths and a final filename and directory as arguments. It looks at each array file and determines the filetype of each. If the files are not .pdfs, they are ignored.
'The .pdf files are then combined into one .pdf and named as prescribed in the arguments. The function returns the filename of the combined .pdf file.
CombineFilestoPDF = ""
Dim PDFArrayList As New ArrayList
Dim Extension As String = ""
Dim ArrayFilePath As String = ""
'Examines each file in the array argument and checks to see that it's a .pdf. If it is, it adds the filename to an array list.
For Each File In RawFileArray
'Get extension
Extension = System.IO.Path.GetExtension(File)
'if not .pdf, convert to pdf. If .pdf, add to list.
If Extension.Contains("pdf") Then
PDFArrayList.Add(File)
Else
MsgBox(File & " does not appear to be a .pdf file, which is the only file format that this program can currently handle. Please convert all files to be used by this program into .pdf format and run the program again.")
End If
Next
'Convert ArrayList to string Array
Dim NewArray() As String = DirectCast(PDFArrayList.ToArray(GetType(String)), String())
'Merge Documents
Dim docs(NewArray.Length - 1) As PdfDocument
For i As Integer = 0 To NewArray.Length - 1
docs(i) = New PdfDocument(NewArray(i))
Next i
docs(0).AppendPage(docs(1))
For i As Integer = 0 To docs(2).Pages.Count - 1 Step 2
docs(0).InsertPage(docs(2), i)
Next i
'Save and Launch
docs(0).SaveToFile(FinalPDFPath & "\" & FinalPDFName)
For Each doc As PdfDocument In docs
doc.Close()
Next doc
System.Diagnostics.Process.Start(FinalPDFPath & "\" & FinalPDFName)
End Function
Again, the resulting PDF is only of the first 3 files. I've also tested this with two multi-page PDFs (about 10 pages each), and it throws and exception here:
- Code: Select all
For i As Integer = 0 To docs(2).Pages.Count - 1 Step 2
This is probably a simple issue, but the exception is "Index was outside the bounds of the array".
Any help would be appreciated.