Wednesday, June 17, 2009

Combining two PDF files into one file using QTP.

See main post here.

Here I am combining abc.pdf and abc1.pdf to New_File.pdf

Set AcroPDDoc = CreateObject("AcroExch.PDDoc")
Set PDDoc = CreateObject("AcroExch.PDDoc")

'source file from which we will take few pages

b = PDDoc.Open("c:\abc.pdf")

'destination file at the end of which we will insert pages. Although in the last line instead of New_File.pdf you can give abc1.pdf, but I have given a new file name to avoid any confusion.

b = AcroPDDoc.Open("c:\abc1.pdf")

'22: number of pages in the destination file -1

'PDDoc: LPDISPATCH argument for the source document.

'LPDISPATCH is a typedef for a pointer to an IDispatch and IDispatch is the interface that exposes the 'OLE Automation protocol.

'0: The first page of source to be inserted into the destination document.

'1: number of pages to insert

'False: Whether Bookmarks are to be copied or not.

b = AcroPDDoc.InsertPages(22, PDDoc, 0, 1, False)

'saving combined pages to new file, you can choose the destination file also.

b = AcroPDDoc.Save(1, "c:\New_File.pdf")