Sign in to follow this  
Followers 0
mozart90

Merge Pdf Files Using Acrobat Com

8 posts in this topic

#1 ·  Posted (edited)

Hi

I got nerved of merging PDF files manually ... so I wrote some Functions...

You need the Autoit Beta (for COM) and Acrobat (Full). May not work with the Reader.

Run the following Code and specify a folder with pdfs. You are asked for a filename under which the merged file will be saved. After clicking OK the whole folder is merged into one PDF file.

Tested with Acrobat 6 under XP SP2

Dim $filelist[1]


; example  merging files 
$var= FileSelectFolder("Select folder", "","",$sourcepath) & "\"
MergeFiles($var,FileSaveDialog( "Select Filename", $var, "PDF Files (*.pdf)", 3))
; example end 



func GetRotation($Targetpath)
;----------------------------------------------------
; Get the Rotation of a Page (eg. page 1)
; Ex.: msgbox(0,"Rotation",GetRotation("c:\pdft\test.pdf" ))
;----------------------------------------------------
    
    if not FileExists($Targetpath) then
        return -1
    endif 
    
    $SourcePDF = ObjCreate("AcroExch.PDDoc")
    
    if not IsObj($SourcePDF) then 
        return -2
    endif   
    
    $b = $SourcePDF.Open($Targetpath)
    $rotation = $sourcePDF.AcquirePage(0).GetRotate
    $SourcePDF.close
    $sourcePDF=""
    return $rotation                                ; in Degree
EndFunc
    


func MergeFiles($SourcePath , $DestinationPath)

    if fileexists($DestinationPath) then 
        FileDelete($DestinationPath)
    endif 


    getfiles($sourcepath, "*.pdf")                                      ; Search Files in Dir
    if $filelist[0] < 2 then 
        Msgbox(32,"Info", "Ther are less than two files in folder")
        exit
    endif 


    for $n=2 to $filelist[0]
;msgbox(0,"Info" , $filelist[0] & " " & $filelist[$n])
        ProgressOn("PDF Merge","Processing ...")
        ProgressSet((($n-1)/$filelist[0]) * 100, $filelist[$n-1])
        
        if $n=2 then 
            pdfmerge($sourcepath & $filelist[$n-1],$sourcepath & $filelist[$n], $DestinationPath) 
        else 
            pdfmerge($DestinationPath, $sourcepath & $filelist[$n], $DestinationPath) 
        endif 
    next 
    ProgressSet(100, $filelist[$filelist[0]])
    sleep(800)
    ProgressOff()
Endfunc




func getfiles($dir, $filter) ; search files in dir
Global $filelist[1]

    $n=0    
    $search= FileFindFirstFile($dir & $filter)
; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf

    While 1
        $n=$n+1
        $file = FileFindNextFile($search) 
        If @error Then ExitLoop
        redim $filelist[$n+1]  
        $filelist[$n]= $file 
        $filelist [0] = $n
    WEnd
; Close the search handle
    FileClose($search)
EndFunc


func PDFMerge($File1, $File2, $Fileout)  
; ---------------------------------------
; File1 first  file 
; File2 second file (inserted after)
; Fileout is the saving name
;
; returns  1 on success
; returns -1 Error insert Pages 
; returns -2 Error Object Create
;----------------------------------------
    
    $SourcePDF = ObjCreate("AcroExch.PDDoc")
    if not IsObj($SourcePDF) then 
        return -2
    endif   
    
    $b = $SourcePDF.Open($file1)

    $TargetPDF = ObjCreate("AcroExch.PDDoc")
    $b = $TargetPDF.Open($file2)

    $intSourcePgs = $SourcePDF.GetNumPages
    $intInsertPgs = $TargetPDF.GetNumPages
    if not $SourcePDF.InsertPages($intSourcePgs-1, $TargetPDF, 0, $intInsertPgs, False) = -1  then 
        $SourcePDF.Close
        $TargetPDF.Close
        $SourcePDF=""
        $TargetPDF=""
        return -1 
    endif 
    $b = $SourcePDF.Save(1, $fileout)
    $SourcePDF.Close
    $TargetPDF.Close
    $SourcePDF=""
    $TargetPDF=""
    return 1
endfunc


func GetPagesCount($target); count pages 
    $AcroPDDoc = ObjCreate("AcroExch.PDDoc")
    if @error then return -1
    $bPDF = $AcroPDDoc.Open($target)
    if @error then return -2
    $Pages= $AcroPDDoc.GetNumPages
    $bPDF = $AcroPDDoc.Close
    return $pages 
endfunc

Feel free to improve the code...

Greetings Mozat90

Edited by mozart90
1 person likes this

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

6 years old thread. but I found this very helpful. thanks mozart90

Can you someone please expand this to compress pdf too

Edit: Typo :)

Edited by chakka

Share this post


Link to post
Share on other sites

Or (also?), how about the same merge function without the need for Full Acrobat? Maybe using one of the open source PDF utilities? Of course, native Au3 would be great. Just a thought.

Share this post


Link to post
Share on other sites

You can merge the PDFs using ghostscript on the command line.

http://www.linux.com/news/software/applications/8229-putting-together-pdf-files


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

thanks for the info.

but mozart90 already coded well to merge pdf's using accrobat. Can u you please help me to expand this cript to COMPRESS pdf files.

Share this post


Link to post
Share on other sites

Help you in what way? Do it for you? Have you at least googled the question to see if it's even possible to compress a PDF after it's been created?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Help you in what way? Do it for you? Have you at least googled the question to see if it's even possible to compress a PDF after it's been created?

Object creation and calling was a problem form me. (As a starter)

But it was done using ghostscript instead. thanks for your link.

Searching & PDF compression, merging(certain files) with metadata, and to FTP them to a remote server...

I'm almost finished. thank you all.

Share this post


Link to post
Share on other sites

This script worked great for me...

Does anyone know how to insert a page break for each document?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0