Combine PDF using Acrobat

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Combine PDF using Acrobat

27 Feb 2019, 02:58

Hi, maybe someone knows how to automate the merge of PDF files, using Adobe Acrobat Reader DC? I have a tool Combine PDF in the Adobe, and if I select the files / right click, Combine files in Acrobat... option appears. Is it possible to do that with AHK? I have found a script for PDFtk, but because I'm runing the script in business, I cannot use this tool and the script offcourse doesn't work.. But maybe the principle can be the same for Acrobat too?
I have Adobe Distiller, tried to run similar command as PDFtk, but without success...

Code: Select all

RunWait, %COMSPEC% /C distiler.exe %InputVar%\*.pdf cat output %OutputVar%\combined.pdf, , hide

Code: Select all

Gui, Add, Text, x25 y10 w170 h20 , Select INPUT folder
Gui, Add, Button, x12 y30 w120 h30 , INPUT
Gui, Add, Button, x22 y65 w100 h30 , Cancel
Gui -SysMenu
Gui, Show, w140 h110, PDFMerge
return

ButtonINPUT:
FileSelectFolder, InputVar, , 4 ;Select a source folder

;Count and display the number of files in the source folder for info
count=0
Loop,%InputVar%\*.pdf,0,1
   files:=(files ? "`n" : "") . A_LoopFileFullPath,count:=count+1
MsgBox % "Found " count " PDF files`nNow select the OUTPUT folder."     
    
FileSelectFolder, OutputVar, , 4 ;Select a destination folder

Gui, submit ;Save the selected folders to their associated variables
;MsgBox pdftk %InputVar%\*.pdf cat output %OutputVar%\combined.pdf ;Use MsgBox to test output to commandline
Run %comspec% /c pdftk %InputVar%\*.pdf cat output %OutputVar%\combined.pdf, , hide ;Pass the parameters to pdftk and hide the commandline window on completion

Gui, Destroy ;Close the first GUI so it doesn't muck up the progress bar

;~ ;Create a second GUI showing looping progress bar
;~ ;('cos merging 800 to 2000 PDF's can take several minutes)
;~ ;Credit to polyethene for the progress bar routine
;~ ;http://www.autohotkey.com/community/viewtopic.php?t=13469)
Gui, Add, Progress, vlvl -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
Gui, Show, , Merging PDF's...    Please wait...
SetTimer, Merging, 45
Return

Merging:
GuiControl, , lvl, 1
Process, Exist, pdftk.exe, ;Check for pdftk
If Errorlevel 
   Return   
SetTimer, Merging, Off ;turn off process timer
Gui, Destroy ;close the second GUI
MsgBox, 64, PDFMerge, Finished! Your merged file is in %OutputVar% ;Inform user that merge process has finished
Run, %OutputVar% ;Open the destination folder to show file
ExitApp

ButtonCancel:
ExitApp

Esc::ExitApp ;Use the Esc key to exit app
Ahk_fan
Posts: 237
Joined: 31 Aug 2018, 14:34
Contact:

Re: Combine PDF using Acrobat

28 Feb 2019, 01:23

Hi,

we use every day pdftk in our company. You don`t need to install the programm, you need only pdftk.exe. Copy pdftk.exe into your AHK-Script directory and use it.

download it here and extract/install it somewhere and copy file pdftk.exe (in a subdirectory) to your script-directory...
https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

here is a small example:

Code: Select all

PathPDFTK := A_ScriptDir "\pdftk.exe"
Path1stPDF := A_ScriptDir "\my1stPDF.pdf"               ;chande to your 1. pdf File
Path2ndPDF := A_ScriptDir "\my2ndPDF.pdf"              ; change your 2. pdf File
PathNewPDF := A_ScriptDir "\myNewPDF.pdf"

RunWait, %comspec% /c ""%PathPDFTK%" "%Path2ndPDF%" "stamp" "%Path1stPDF%" "output" "%PathNewPDF%" "dont_ask"" , , hide
this ist an example to stamp, if you want to combine use "cat" instead of "stamp"
regards,
AHK_fan :)
https://hr-anwendungen.de
RickC
Posts: 299
Joined: 27 Oct 2013, 08:32

Re: Combine PDF using Acrobat

28 Feb 2019, 09:48

I too use PDFtk in preference to Adobe Acrobat Reader. I built a very simple Source/Destination GUI in AHK to help with the automation:

Code: Select all

Gui, Add, Text, x25 y10 w170 h20 , Select INPUT folder
Gui, Add, Button, x12 y30 w120 h30 , INPUT
Gui, Add, Button, x22 y65 w100 h30 , Cancel
Gui -SysMenu
Gui, Show, w140 h110, PDFMerge
return

ButtonINPUT:
FileSelectFolder, InputVar, D:\Scans, 4, Select a SOURCE folder ; Select a source folder

; Count and display the number of files in the source folder for info
count=0
Loop,%InputVar%\*.pdf,0,1
   files:=(files ? "`n" : "") . A_LoopFileFullPath,count:=count+1
MsgBox % "Found " count " PDF files`nNow select the OUTPUT folder."     
    
FileSelectFolder, OutputVar, D:\Scans, 4, Select a DESTINATION folder ; Select a destination folder

Gui, submit ; Save the selected folders to their associated variables
; MsgBox pdftk %InputVar%\*.pdf cat output %OutputVar%\combined.pdf ; Use MsgBox to test output to commandline
Run %comspec% /c pdftk %InputVar%\*.pdf cat output %OutputVar%\combined.pdf, , hide ; Pass the parameters to pdftk and hide the commandline window on completion

Gui, Destroy ; Close the first GUI so it doesn't muck up the progress bar

;~ ; Create a second GUI showing looping progress bar
;~ ; ('cos merging 800 to 2000 PDF's can take several minutes)
;~ ; Credit to polyethene for the progress bar routine
;~ ; http://www.autohotkey.com/community/viewtopic.php?t=13469
Gui, Add, Progress, vlvl -Smooth 0x8 w350 h18 ; PBS_MARQUEE = 0x8
Gui, Show, , Merging PDFs...    Please wait...
SetTimer, Merging, 45
Return

Merging:
GuiControl, , lvl, 1
Process, Exist, pdftk.exe, ; Check for pdftk
If Errorlevel 
   Return   
SetTimer, Merging, Off ; Turn off process timer
Gui, Destroy ; Close the second GUI
MsgBox, 64, PDFMerge, Finished! Your merged file is in %OutputVar% ; Inform user that merge process has finished
Run, %OutputVar% ; Open the destination folder to show file
ExitApp

ButtonCancel:
ExitApp

Esc::ExitApp ; Use the Esc key to exit app
That's basically the point of PDFtk... to assist automation.

Hope this helps...
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Combine PDF using Acrobat

28 Feb 2019, 14:24

euras wrote:
27 Feb 2019, 02:58
Hi, maybe someone knows how to automate the merge of PDF files, using Adobe Acrobat Reader DC? I have a tool Combine PDF in the Adobe, and if I select the files / right click, Combine files in Acrobat... option appears. Is it possible to do that with AHK? I have found a script for PDFtk, but because I'm runing the script in business, I cannot use this tool and the script offcourse doesn't work.. But maybe the principle can be the same for Acrobat too?
I have Adobe Distiller, tried to run similar command as PDFtk, but without success...

Here is a COM API solution but this requires a paid version of Acrobat be installed.

Code: Select all

PDDoc := ComObjCreate("AcroExch.PDDoc")
PDDoc.Open(A_Desktop "\Test\Test.pdf")
PDDoc2 := ComObjCreate("AcroExch.PDDoc")
PDDoc2.Open(A_Desktop "\Test\Test2.pdf")
PDDoc2.InsertPages(-1, PDDoc, 0, PDDoc.GetNumPages, 1) ; index-0, add at beginning from PDDoc all pages with bookmarks
PDDoc2.Save(1,  A_Desktop "\Test\Test_Merge.pdf")
PDDoc.Close()
PDDoc2.Close()
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
euras
Posts: 429
Joined: 05 Nov 2015, 12:56

Re: Combine PDF using Acrobat

01 Mar 2019, 03:00

FanaticGuru wrote:
28 Feb 2019, 14:24
euras wrote:
27 Feb 2019, 02:58
Hi, maybe someone knows how to automate the merge of PDF files, using Adobe Acrobat Reader DC? I have a tool Combine PDF in the Adobe, and if I select the files / right click, Combine files in Acrobat... option appears. Is it possible to do that with AHK? I have found a script for PDFtk, but because I'm runing the script in business, I cannot use this tool and the script offcourse doesn't work.. But maybe the principle can be the same for Acrobat too?
I have Adobe Distiller, tried to run similar command as PDFtk, but without success...

Here is a COM API solution but this requires a paid version of Acrobat be installed.

Code: Select all

PDDoc := ComObjCreate("AcroExch.PDDoc")
PDDoc.Open(A_Desktop "\Test\Test.pdf")
PDDoc2 := ComObjCreate("AcroExch.PDDoc")
PDDoc2.Open(A_Desktop "\Test\Test2.pdf")
PDDoc2.InsertPages(-1, PDDoc, 0, PDDoc.GetNumPages, 1) ; index-0, add at beginning from PDDoc all pages with bookmarks
PDDoc2.Save(1,  A_Desktop "\Test\Test_Merge.pdf")
PDDoc.Close()
PDDoc2.Close()
FG
you are the champ! thank you!
Do you know how to convert .png files to pdf using COM? Because this function does not allows to add .png files to combine, it should be .pdf
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Combine PDF using Acrobat

01 Mar 2019, 15:51

euras wrote:
01 Mar 2019, 03:00
Do you know how to convert .png files to pdf using COM? Because this function does not allows to add .png files to combine, it should be .pdf

Yes, the code above only works for combining two PDF files.

I don't know right off how best to create a PDF from a PNG file but I will think about the best way to go about it.

Below is code that I already have that converts the clipboard to a PDF if the clipboard contains a picture. Coping a file to the clipboard from file explorer is not really a picture on the clipboard. It is really just coping a path to the file which then gets converted to a picture when pasting. A true picture on the clipboard is like when you do something like Print Screen to take a screenshot.

Code: Select all

Clipboard2Acrobat(SavePathPDF:="")		; Adobe Acrobat must be installed
{
	App := ComObjCreate("AcroExch.App")
	App.Show()
	App.MenuItemExecute("ImageConversion:Clipboard")
	if SavePathPDF
	{
		IfNotExist, %SavePathPDF%
			FileCreateDir, %SavePathPDF%
		FormatTime, TimeStamp ,, yyyy_MM_dd @ HH_mm_ss 
		FileName := TimeStamp ".PDF"
		AVDoc := App.GetActiveDoc()
		PVDoc := AVDoc.GetPDDoc()
		PDSaveIncremental		:= 0x0000   ;/* write changes only */ 
		PDSaveFull						:= 0x0001   ;/* write entire file */ 
		PDSaveCopy					:= 0x0002   ;/* write copy w/o affecting current state */
		PDSaveLinearized			:= 0x0004   ;/* write the file linearized for */
		PDSaveBinaryOK			:= 0x0010   ;/* OK to store binary in file */
		PDSaveCollectGarbage	:= 0x0020   ;/* perform garbage collection on */
		PVDoc.save(PDSaveFull|PDSaveLinearized, SavePathPDF FileName)
	}
}
This uses MenuItemExecute which lets you execute most Menu Items just as if the user clicked on them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Lamron750 and 241 guests