10 Replies Latest reply: Jul 20, 2014 11:48 AM by Test Screen Name RSS

    How to Merge Multiple PDF's

    SathishVelu Community Member

      HI Forum

       

      I am a newbie to both Acrobat and VB,

      Basically a perl Developer,

       

      I have created a application to Merge multiple PDF's into a Single PDF, but the problem I am facing is some pages are missed during Merging

       

      For Example

       

      I have 4 pdf's (1.pdf, 2.pdf, 3.pdf, 4.pdf), each PDF has 2 pages, totally 8 pages,

       

      When i merge using my application, it is getting collapsed and then merged

       

      Here is my Code

       

      <code>

      Imports System.IO

      Imports Acrobat

       

      Public Class Form1

       

          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

              MergePDF()

          End Sub

       

          Sub MergePDF()

       

              Dim gPDDoc1 As AcroPDDoc

              Dim gPDDoc2 As AcroPDDoc

              Dim gPDDoc3 As AcroPDDoc

              Dim gPDDoc4 As AcroPDDoc

       

              gPDDoc1 = CreateObject("AcroExch.PDDoc")

              gPDDoc2 = CreateObject("AcroExch.PDDoc")

              gPDDoc3 = CreateObject("AcroExch.PDDoc")

              gPDDoc4 = CreateObject("AcroExch.PDDoc")

       

              Dim chk1 = gPDDoc1.Open("D:\sathish\1.pdf")

              Dim chk2 = gPDDoc2.Open("D:\sathish\2.pdf")

              Dim chk3 = gPDDoc3.Open("D:\sathish\3.pdf")

              Dim chk4 = gPDDoc4.Open("D:\sathish\4.pdf")

       

              Dim mergefile As Boolean

              Dim numpg1, numpg2, numpg3, numpg4

              numpg1 = gPDDoc1.GetNumPages()

              numpg2 = gPDDoc2.GetNumPages()

              numpg3 = gPDDoc3.GetNumPages()

              numpg4 = gPDDoc4.GetNumPages()

       

              MsgBox(numpg1 & numpg2 & numpg3 & numpg4)

       

              mergefile = gPDDoc1.InsertPages(numpg1 - 1, gPDDoc2, 0, 1, 0)

              mergefile = gPDDoc1.InsertPages(numpg2 - 1, gPDDoc3, 0, 1, 0)

              mergefile = gPDDoc1.InsertPages(numpg3 - 1, gPDDoc4, 0, 1, 0)

       

       

              Dim savemergefile As Boolean

              savemergefile = gPDDoc1.Save(1, "D:\sathish\merged.pdf")

       

          End Sub

       

       

      End Class

      </code>

       

      I Dont know how to give the correct page end to merge,

       

      Thanks in Advance for your Kind replies,

       

      Thanks & Regards

      Sathish V.

        • 1. Re: How to Merge Multiple PDF's
          Test Screen Name CommunityMVP

          I see your code but I don't know what it is supposed to do. Can you explain "I Dont know how to give the correct page end to merge," in detail.

          • 2. Re: How to Merge Multiple PDF's
            SathishVelu Community Member

            Thanks for your reply,

             

            If i use this code, the Final ouput PDF (D:\sathish\merged.pdf) is generated, But the pages are not in proper order (1.pdf+2.pdf+3.pdf+4.pdf),

             

            For e.g (1.pdf contains pages 1& 2, 2.pdf - 3 & 4, 3.pdf - 5 &6, 4.pdf - 7 & 8)

             

            But the Final Output (merged.pdf) has only 5 pages (1,2,7,5,3), i dont know how it happened, may be some wrong in my coding (VB) in the below part

             

              MsgBox(numpg1 & numpg2 & numpg3 & numpg4)

             

                    mergefile = gPDDoc1.InsertPages(numpg1 - 1, gPDDoc2, 0, 1, 0)

                    mergefile = gPDDoc1.InsertPages(numpg2 - 1, gPDDoc3, 0, 1, 0)

                    mergefile = gPDDoc1.InsertPages(numpg3 - 1, gPDDoc4, 0, 1, 0)

             

            Please guide me,

             

            Thanks & Regards

            Sathish V.

            • 3. Re: How to Merge Multiple PDF's
              Test Screen Name CommunityMVP

              Certainly you are asking only for one page to be inserted with each code line. Do you have the documentation of InsertPages in the SDK?

              • 4. Re: How to Merge Multiple PDF's
                SathishVelu Community Member

                Thanks for your help and reply, finally i got it right,

                 

                Here is the Updated coding to Merge Multiple PDF's in VB

                 

                Imports System.IO

                Imports Acrobat

                 

                Public Class Form1

                 

                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                        MergePDF()

                    End Sub

                 

                    Sub MergePDF()

                 

                        Dim gPDDoc1 As AcroPDDoc

                        Dim gPDDoc2 As AcroPDDoc

                        Dim gPDDoc3 As AcroPDDoc

                        Dim gPDDoc4 As AcroPDDoc

                 

                        gPDDoc1 = CreateObject("AcroExch.PDDoc")

                        gPDDoc2 = CreateObject("AcroExch.PDDoc")

                        gPDDoc3 = CreateObject("AcroExch.PDDoc")

                        gPDDoc4 = CreateObject("AcroExch.PDDoc")

                 

                        Dim chk1 = gPDDoc1.Open("D:\sathish\1.pdf")

                        Dim chk2 = gPDDoc2.Open("D:\sathish\2.pdf")

                        Dim chk3 = gPDDoc3.Open("D:\sathish\3.pdf")

                        Dim chk4 = gPDDoc4.Open("D:\sathish\4.pdf")

                 

                        Dim mergefile As Boolean

                        Dim numpg1, numpg2, numpg3, numpg4

                        numpg1 = gPDDoc1.GetNumPages()

                        numpg2 = gPDDoc2.GetNumPages()

                        numpg3 = gPDDoc3.GetNumPages()

                        numpg4 = gPDDoc4.GetNumPages()

                 

                        MsgBox(numpg1 & numpg2 & numpg3 & numpg4)

                 

                        mergefile = gPDDoc1.InsertPages(numpg1 - 1, gPDDoc2, 0, numpg2, 0)

                        mergefile = gPDDoc1.InsertPages(numpg1 + numpg2 - 1, gPDDoc3, 0, numpg3, 0)

                        mergefile = gPDDoc1.InsertPages(numpg1 + numpg2 + numpg3 - 1, gPDDoc4, 0, numpg4, 0)

                 

                 

                        Dim savemergefile As Boolean

                        savemergefile = gPDDoc1.Save(1, "D:\sathish\merged.pdf")

                 

                    End Sub

                 

                 

                End Class

                • 5. Re: How to Merge Multiple PDF's
                  jaimegranad1

                      Hello,

                   

                  In excel VB, I am trying to print(as pdf) only certain pages from a pdf and save them to the same. I don't want to open the PDF file. I just want to create a new pdf with those specific pages. Also I was wondering if I could highlight certain text as well? any help would be greatly appreciated.

                  Thanks!

                  Jaime

                  • 6. Re: How to Merge Multiple PDF's
                    Test Screen Name CommunityMVP

                    Have you downloaded and installed the Acrobat SDK? You should not be printing a PDF to PDF, this is called "refrying" and is strongly discouraged. You can use OLE to delete pages.

                    • 7. Re: Re: How to Merge Multiple PDF's
                      jaimegranad1 Community Member

                      Great idea. do you have example code? I found some code but it its contingent upon the pages having a page number on them. the pdfs im dealing with have no page numbers on the pages. also this code deletes pages in a range. I need to delete all other pages except for the page numbers on the range. thanks! Option Explicit Sub Delete_PDF_Pages() ' Adobe code based on http://vbcity.com/forums/t/51200.aspx Dim xMsg          As String Dim xInput        As String Dim xOutput        As String Dim xResponse      As Long Dim xLast_Row      As Long Dim xErrors        As Long Dim xDeleted      As Long Dim i              As Long Dim j              As Long Dim AcroApp        As CAcroApp Dim AcroPDDoc      As CAcroPDDoc Dim AcroHiliteList As CAcroHiliteList Dim AcroTextSelect As CAcroPDTextSelect Dim xarray()      As Variant Dim PageNumber    As Variant Dim PageContent    As Variant Dim xContent      As Variant xInput = "C:\Users\jaime\Desktop\Granado LLC\test\TestPages.pdf" xOutput = "C:\Users\jaime\Desktop\Granado LLC\test\TestPages_Output.pdf" xLast_Row = [A1].SpecialCells(xlLastCell).Row ReDim xarray(xLast_Row) xResponse = MsgBox("About to delete all pages which contain values from the range A1:A" & xLast_Row & Chr(10) _             & Chr(10) & "Input:" & Chr(9) & xInput _             & Chr(10) & "Output:" & Chr(9) & xOutput _             & Chr(10) & Chr(10) & "('OK' to continue, 'Cancel' to quit.)", vbOKCancel, "Delete Pages") If xResponse = 2 Then     MsgBox "User chose not to continue. Run terminated."     Exit Sub End If ' Files and data OK? If Dir(xInput) = "" Then xMsg = "Input file not found - " & xInput & Chr(10) If Dir(xOutput) <> "" Then xMsg = "Output file exists - " & xOutput & Chr(10) xarray = Application.Transpose(Range("A1:A" & xLast_Row)) For i = 1 To xLast_Row     If Not IsNumeric(xarray(i)) Or xarray(i) = "" Then         xMsg = "Non-numeric ""Delete"" value of """ & xarray(i) & """ found on row " & i & Chr(10)         Exit For     End If Next If xMsg <> "" Then     MsgBox (xMsg & Chr(10) & "Run cancelled.")     Exit Sub End If ' Open the PDF... Set AcroApp = CreateObject("AcroExch.App") Set AcroPDDoc = CreateObject("AcroExch.PDDoc") If AcroPDDoc.Open(xInput) <> True Then     MsgBox (xInput & " couldn't be opened - run cancelled.")     Exit Sub End If ' Read each page... For i = AcroPDDoc.GetNumPages - 1 To 0 Step -1     Set PageNumber = AcroPDDoc.AcquirePage(i)     Set PageContent = CreateObject("AcroExch.HiliteList")     'Get up to 9,999 words from page...     If PageContent.Add(0, 9999) <> True Then                 Debug.Print "Add Error on Page " & i + 1         xErrors = xErrors + 1         Else         Set AcroTextSelect = PageNumber.CreatePageHilite(PageContent)             If Not AcroTextSelect Is Nothing Then             xContent = ""             For j = 0 To AcroTextSelect.GetNumText - 1                 xContent = xContent & AcroTextSelect.GetText(j)             Next j             For j = 1 To xLast_Row                 If InStr(1, xContent, xarray(j)) > 0 Then                     Debug.Print "Page " & i + 1 & " contains " & xarray(j) & " - " & xContent                     ' To avoid problems with the delete...                     Set AcroTextSelect = Nothing                     Set PageContent = Nothing                     Set PageNumber = Nothing                     If AcroPDDoc.DeletePages(i, i) = False Then                         MsgBox ("Error deleting page " & i + 1 & " - run cancelled.")                         Exit Sub                     End If                     xDeleted = xDeleted + 1                     Exit For                 End If             Next         End If             End If Next i If AcroPDDoc.Save(PDSaveFull, xOutput) = False Then     MsgBox "Cannot save the modified document"     Exit Sub Else     MsgBox (xDeleted & " pages deleted. (" & xErrors & " errors.)") End If     AcroPDDoc.Close AcroApp.Exit End Sub

                      • 8. Re: How to Merge Multiple PDF's
                        Test Screen Name CommunityMVP

                        You did not answer my question. Have you downloaded and installed the Acrobat SDK?

                        • 9. Re: How to Merge Multiple PDF's
                          jaimegranad1 Community Member

                          I did download and viewed a lot of the info but there is nothing that shows me how to use the code in excel.

                          • 10. Re: How to Merge Multiple PDF's
                            Test Screen Name CommunityMVP

                            Excel uses VBA. VBA can use OLE objects like PDDoc. These are documented in the Interapplication Communication Section.

                             

                            The SDK contains information for programming, not samples to copy/paste...