VB 6.0 - vb6 how to merge two pdf files - Asked By basel hm on 09-Jun-05 07:47 AM

vb6 merge to pdf files

Current Thread - Asked By Rohan Bairat on 14-Jun-05 05:38 PM

This will merge PDF
Sub MergeFiles(ByRef strDirectory As String, Optional ByRef blnLog As Boolean = True, Optional ByRef strLog As String = "AcroMerge.log")
 Dim oAcroMain As Object
 Dim oAcroSlave As Object
 Dim sTargetFile As String
 Dim vaSourceFiles As Variant
 Dim sCommandLine As String
 Dim slogfile As String
 Dim bLog As Boolean
 Dim lLoop As Long
 Dim bGotMainFile
 
 bLog = blnLog
 slogfile = strLog
 
 If bLog Then WriteLog slogfile, vbCrLf & "AcroMerge starting..." & Date & " " & Time
 

 If Right(strDirectory, 1) <> "\" Then
    strDirectory = strDirectory & "\"
 End If
 
' Puts an array of filenames into vaSourceFiles to work with in rogs code
  vaSourceFiles = ListFiles("c:\reportbooks\financials\", True, "*.pdf")

 

 If UBound(vaSourceFiles) = 0 Then
   If bLog Then WriteLog slogfile, "Fatal Error: No source document specified."
   Exit Sub
 End If
 
' Start Merge Process
 Set oAcroMain = CreateObject("ACROEXCH.PDDOC")
 Set oAcroSlave = CreateObject("ACROEXCH.PDDOC")

  
  lLoop = UBound(vaSourceFiles)
  bGotMainFile = False
  
  

  
    Dim aDist As New ACRODISTXLib.PdfDistiller
    Dim AcroApp As CAcroApp
    Dim PDDoc As CAcroPDDoc
    Dim InsertPDDoc As CAcroPDDoc
    Dim iNumberOfPagesToInsert As Integer, iLastPage As Integer

    Set AcroApp = CreateObject("AcroExch.App")      '<======================
    Set PDDoc = CreateObject("AcroExch.PDDoc")      '<======================
    
         '/ Create a Blank PDDoc (The Merge File) .         '<======================
    If PDDoc.Create = False Then                                    '<======================
        MsgBox "Creation not successful"                       '<======================
    End                                                                                 '<======================
    End If                                                                              '<======================
         
Do While lLoop > 0 And bGotMainFile = False
  
    If bLog Then WriteLog slogfile, "Opening: " & vaSourceFiles(lLoop)
    
    '===============
    Set InsertPDDoc = CreateObject("AcroExch.PDDoc")    '<======================
    '===============
    
         '/ Hide the Acrobat window
         AcroApp.Hide


      InsertPDDoc.Open vaSourceFiles(lLoop)   'PDF File
      iNumberOfPagesToInsert = InsertPDDoc.GetNumPages
      iLastPage = PDDoc.GetNumPages - 1
      PDDoc.InsertPages iLastPage, InsertPDDoc, 0, iNumberOfPagesToInsert, True
      InsertPDDoc.Close
    
    lLoop = lLoop - 1
  Loop
  
   '/ Save the PDF File Optimized
    PDDoc.Save &H5, "c:\test\FinalMerged.pdf"       '<====================== fix dir save routine
    ' Close the PDDoc
    PDDoc.Close
    ' Close Acrobat Exchange
    
    AcroApp.Exit
    Screen.MousePointer = 0
    Set AcroApp = Nothing
    Set PDDoc = Nothing
    Set InsertPDDoc = Nothing

  
  oAcroMain.Close
  
  Set oAcroMain = Nothing
  Set oAcroSlave = Nothing
  
End Sub