Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merge PDFs and Visual Basic 1

Status
Not open for further replies.

jkb17

Programmer
Nov 27, 2000
156
US
Hello.

I have several PDF documents that I need to merge into a single PDF document. Can this be done programmatically with Adobe Acrobat and Visual Basic 6.0? I have the full version of software - acrobat 5.0

any help is appreciated

thanks

Jim
 

I belive (could be wrong) that there is a utility (macro) in Acrobat that will do this for you without the need for vb.

Good Luck

 
this is what I use

Code:
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CombinePDFs"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
Option Explicit

Private msProblem As String

Public Event Status(ByVal sMessage As String)

Public Property Get Problem() As String
  Problem = msProblem
End Property

Public Function Combine(ByVal sOutputFile As String, _
                        ParamArray SourceFiles()) As Boolean
' files will be combined in the order they were added to SourceFiles()
  Dim i As Long
  Dim nLB As Long
  Dim nUB As Long
  
  Dim nRet As VbMsgBoxResult
  
  Dim oOutputPDF As Acrobat.CAcroPDDoc
  Dim oInputPDF As Acrobat.CAcroPDDoc
  
  Dim nPagesTo As Long
  Dim nPagesFrom As Long

  On Error GoTo ERRHANDLER
  
  Combine = False
  
  nLB = LBound(SourceFiles)
  nUB = UBound(SourceFiles)
  
  If nUB < nLB Then
    Err.Raise vbObjectError + 6001, &quot;Combine&quot;, &quot;No files to combine&quot;
  Else
    Set oOutputPDF = CreateObject(&quot;AcroExch.PDDoc&quot;)
    RaiseEvent Status(&quot;Opening &quot; & SourceFiles(nLB))
    If Not oOutputPDF.Open(SourceFiles(nLB)) Then
      Err.Raise vbObjectError + 6002, &quot;Combine&quot;, &quot;Unable to open &quot; & SourceFiles(nLB)
    End If
    nPagesTo = oOutputPDF.GetNumPages
    
    For i = (nLB + 1) To nUB
      Set oInputPDF = CreateObject(&quot;AcroExch.PDDoc&quot;)
      RaiseEvent Status(&quot;Opening &quot; & SourceFiles(i))
      If Not oInputPDF.Open(SourceFiles(i)) Then
        Err.Raise vbObjectError + 6003, &quot;Combine&quot;, &quot;Unable to open &quot; & SourceFiles(i)
      End If
      nPagesFrom = oInputPDF.GetNumPages
      RaiseEvent Status(&quot;Inserting &quot; & nPagesFrom & &quot; pages from &quot; & SourceFiles(i))
      If Not oOutputPDF.InsertPages(nPagesTo - 1, oInputPDF, 0, nPagesFrom, True) Then
        Err.Raise vbObjectError + 6004, &quot;Combine&quot;, &quot;Unable to add &quot; & SourceFiles(i)
      End If
      nPagesTo = oOutputPDF.GetNumPages
      Set oInputPDF = Nothing
    Next i
    
    RaiseEvent Status(&quot;Saving &quot; & sOutputFile)
    If Not oOutputPDF.Save(PDSaveFull, sOutputFile) Then
      Err.Raise vbObjectError + 6005, &quot;Combine&quot;, &quot;Unable to save &quot; & sOutputFile
    End If
    
    RaiseEvent Status(&quot;Closing &quot; & sOutputFile)
    Call oOutputPDF.Close
    Set oOutputPDF = Nothing
    
    Combine = True
  End If
  
  Exit Function

ERRHANDLER:
  msProblem = Err.Description
  nRet = MsgBox(&quot;Error &quot; & Err.Number & &quot;: &quot; & Err.Description, _
                vbQuestion + vbRetryCancel, _
                App.Title & &quot; - &quot; & Err.Source)
  If nRet = vbRetry Then Resume
  
  Combine = False
  Set oOutputPDF = Nothing
  Set oInputPDF = Nothing
End Function
 
thanks...justin.

It looks good. I had a little corruption on the merge but i'll have to check my soruce files again. By corruptin, the characters were a little sloppy and not looking like they should.

thanks again.

bye
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top