New Forum | Previous | Next | (P-PDF) Developers
Topic: Help with Merging PDFs in VB
Conf: (P-PDF) Developers, Msg: 129479
Date: 3/16/2005 07:09 AM
I'm having some trouble trying to merge PDF's. I'm new to VB, but I was able to find some code from Planet PDF that does just what I am looking for. The only problem is that when I run it, it goes through each of the pdf's in my folder, but the output file only includes the first pdf in the folder. Is it something I'm doing wrong? This is the code I'm using:
Const PDF_WILDCARD = "*.pdf"
Const PDF_DIRECTORY = "C:\Documents and Settings\Christian\My Documents\2.1 documentation\"
Const JOIN_FILENAME = "complete.pdf"
Dim AcroExchApp As Object, AcroExchPDDoc As Object, _
AcroExchInsertPDDoc As Object
Dim strFileName As String, strPath As String
Dim iNumberOfPagesToInsert As Integer, _
iLastPage As Integer
Set AcroExchApp = CreateObject("AcroExch.App")
Set AcroExchPDDoc = CreateObject("AcroExch.PDDoc")
' Show the Acrobat Exchange window
' Set the directory / folder to use
strPath = PDF_DIRECTORY
' Get the first pdf file in the directory
strFileName = Dir(strPath + PDF_WILDCARD, vbNormal)
' Open the first file in the directory
AcroExchPDDoc.Open strPath + strFileName
' Get the name of the next file in the directory [if any]
If strFileName <> "" Then
strFileName = Dir
' Start the loop.
Do While strFileName <> ""
' Get the total pages less one for the last page num [zero based]
iLastPage = AcroExchPDDoc.GetNumPages - 1
Set AcroExchInsertPDDoc = CreateObject("AcroExch.PDDoc")
' Open the file to insert
AcroExchInsertPDDoc.Open strPath + strFileName
' Get the number of pages to insert
iNumberOfPagesToInsert = AcroExchInsertPDDoc.GetNumPages
' Insert the pages
AcroExchPDDoc.InsertPages iLastPage, AcroExchInsertPDDoc, 0, iNumberOfPagesToInsert, True
' Close the document
' Get the name of the next file in the directory
strFileName = Dir
' Save the entire document as the JOIN_FILENAME using SaveFull [0x0001 = &H1]
AcroExchPDDoc.Save &H1, strPath + JOIN_FILENAME
' Close the PDDoc
' Close Acrobat Exchange
No one else seems to have a problem with this (at least the problem I'm having), so I'm not sure what I'm doing wrong. Again, I'm very new to VB, so it could easily be something stupid that I'm doing. Thanks in advance for any help.