Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Sep 26, 2014 8:08 pm

Ok, so as you'll see, I'm quite the novice programmer. I've purchased the commercial version of Spire.pdf, but I can't get it to merge more than 3 PDFs. I've inserted the license file as an embedded resource per instructions I found on this site. Here's my code, hopefully it's obvious what I'm trying to do by the notes:

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.

EngineeRusty1979
 
Posts: 5
Joined: Fri Jun 20, 2014 1:21 pm

Sun Sep 28, 2014 2:31 am

Hello,

Thanks for your inquiry.
Your code only merged the first three pdf files hence you just got a output of merging three files.
Please try the following code to merge all pdf files.
Code: Select all
 
string output = "merged.pdf";
PdfDocumentBase doc = PdfDocument.MergeFiles(NewArray);
 doc.Save(output);
System.Diagnostics.Process.Start(output);


Please feel free to contact us if you have any problems.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Sep 29, 2014 8:56 am

Hello,

Does the code snippet resolve your issue?
Thanks for your feedback.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Sep 29, 2014 2:07 pm

amy.zhao wrote:Hello,

Does the code snippet resolve your issue?
Thanks for your feedback.

Best wishes,
Amy


Yes, it worked, and obviously cleans my messy code up. Thank you!

EngineeRusty1979
 
Posts: 5
Joined: Fri Jun 20, 2014 1:21 pm

Tue Sep 30, 2014 1:56 am

Hello,

We are glad to hear from you.
Please feel free to contact us if you have further problems.

Best wishes,
Amy
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF