Manipulate PDF

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Manipulate PDF

27 Apr 2024, 19:15

Is it possible to manipulate pdf files with AutoHotkey?
Merge two PDF files from a folder and also merge two PDFs on the same page, like one page on top of the other.
possible?
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Manipulate PDF

27 Apr 2024, 19:19

Hello,

You might want to search the forum for some of the existing posts about PDF that show techniques. You can also use other command-line tools such as xpdf and pdftk.
Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Re: Manipulate PDF

29 Apr 2024, 23:06

I'm looking for. Reading, trying to understand!
I found a post (I think it was yours) using sumatraPDF
Could you help me use these commands in ahk?
Two options that would help me with my work are printing a PDF file from a folder and printing using the "duplex" printing that is in the documentation. Follow Link below:
https://www.sumatrapdfreader.org/docs/Command-line-arguments
User avatar
mikeyww
Posts: 28871
Joined: 09 Sep 2014, 18:38

Re: Manipulate PDF

30 Apr 2024, 06:13

If you craft a command line that would work in a Windows batch file, you can then use :arrow: Run to execute the same command line.

Here is maybe a hidden gem, too. MiKTeX is a free program library that you can install as portable. It contains a program called pdfunite.exe that you can use to join or merge your PDF files easily. You can write an AHK script that uses a GUI to select files to merge. Run can then call pdfunite.exe to merge them.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: Manipulate PDF

30 Apr 2024, 10:15

if you have heavy work and not merge you better use Python if you want an easier approach because of its library,

in AHK you just need to use an external app or libraries, so you can use RUN ,COM ,DLL command.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
Jasonosaj
Posts: 58
Joined: 02 Feb 2022, 15:02
Location: California

Re: Manipulate PDF

30 Apr 2024, 10:35

xMaxrayx wrote:
30 Apr 2024, 10:15
if you have heavy work and not merge you better use Python if you want an easier approach because of its library,

in AHK you just need to use an external app or libraries, so you can use RUN ,COM ,DLL command.
I use the following to diff PDFs:



import subprocess
import sys
from tkinter import filedialog, Tk, messagebox

def select_files():
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
file_paths = filedialog.askopenfilenames() # show an "Open" dialog box and return the path to the selected file
return file_paths

def run_diff_pdf(file1, file2):
command = f'diff-pdf /m /g --view "{file1}" "{file2}"'
subprocess.run(command, shell=True)

def main():
if len(sys.argv) == 3: # if two files are passed as arguments
file1 = sys.argv[1]
file2 = sys.argv[2]
else: # if no files or only one file is passed as argument
files = select_files()
if len(files) != 2:
messagebox.showerror("Error", "Please select exactly two files.")
return
file1, file2 = files

run_diff_pdf(file1, file2)

if __name__ == "__main__":
main()

Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Re: Manipulate PDF

30 Apr 2024, 13:18

I did it with .ahk and using Sumatrapdf to print and pdftk to merge the PDFs.
Thank you for your help. But I don't understand Python very well.
The good thing about ahk is that it is possible to compile to .exe, I don't know if it is possible in Python.
The only thing missing was a good option to join the two pages of a PDF, as if it were a stamp. I need to remove the printed page from the printer and place it to be printed in the same previous print.
metallizer
Posts: 32
Joined: 13 Aug 2021, 13:34

Re: Manipulate PDF

02 May 2024, 08:14

If you have acrobat pro (not sure about non pro) you can use ComObject('AcroExch.App') for automation (Link). There is a method under AcroExch.PDDoc called InsertPages which inserts the specified pages from the source document after the indicated page within the current document (essentially merging two pdf).
Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Re: Manipulate PDF

02 May 2024, 11:32

Thank you for your help!
I just managed to do it on script.vbs I couldn't get it in .ahk. But it adds the first page does not mix one file in the other. The final PDF is the same as the PDF being mixed. is not accepting one page mixed in the other. At least I couldn't have it.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: Manipulate PDF

07 May 2024, 03:40

Felix Siano wrote:
30 Apr 2024, 13:18
I did it with .ahk and using Sumatrapdf to print and pdftk to merge the PDFs.
Thank you for your help. But I don't understand Python very well.
The good thing about ahk is that it is possible to compile to .exe, I don't know if it is possible in Python.
The only thing missing was a good option to join the two pages of a PDF, as if it were a stamp. I need to remove the printed page from the printer and place it to be printed in the same previous print.
you can actually convert python to exe even you can do virtual environment to have all dependency packages but no one do it because it will make it bigger than it should be ,the good with python it has a lot of libraries but yeah I understand it's annoying if you didn't learn the language but I think it's worth it.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
Yfreet
Posts: 6
Joined: 19 Mar 2024, 05:02

Re: Manipulate PDF

17 May 2024, 05:16

If you can do AHK, you would be able to learn Python quickly. Are the same concepts, just with different keywords. As for your problem - I was using pdfcpu as commandline from AHK script. It has watermark function, which may be of interest for you.
https://pdfcpu.io/core/watermark#examples

Part of my script, which merges pdfs:

Code: Select all

app := "\\path\to\pdfcpu\pdfcpu.exe " ;with space at the end
Loop Files, folder_path "*.pdf"
{
    file_paths.Push(A_LoopFileFullPath)   ;adds file paths to an array for a given counter of files
	if file_paths.Length >= file_count[]{
		break
	}
}

for index, Value in file_paths ;converts the array into string, delimited by space, as required for pdfcpu
{
	file_path_txt .= Value . " " 
}

RunWait app " merge " folder_path today "\" filename " " file_path_txt ;executing commandline pdfcpu
Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Re: Manipulate PDF

17 May 2024, 17:30

Perfect!
I will take a look...
I downloaded pdfcpu.exe, do I just need to put the script.ahk in the same folder where pdfcpu.exe is?
Felix Siano
Posts: 98
Joined: 23 Apr 2023, 13:03

Re: Manipulate PDF

17 May 2024, 22:41

What am I doing wrong in ~a&2?
No ~a & 1 works

Code: Select all

~a & 1::
{
RunWait "C:\Users\siano\Desktop\pdfcpu.exe" merge "C:\Users\siano\Desktop\Junto.pdf" "C:\Users\siano\Desktop\NOTAS\*.pdf"
}

~a & 2::
{
RunWait "C:\Users\siano\Desktop\pdfcpu.exe" watermark add -mode pdf -- "C:\Users\siano\Desktop\1.pdf:1" "C:\Users\siano\Desktop\10.pdf" "C:\Users\siano\Desktop\THEEND.pdf"
}
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Manipulate PDF

18 May 2024, 02:45

The first one isn’t correct either unless you’re running it with v1. Do you see why?
Yfreet
Posts: 6
Joined: 19 Mar 2024, 05:02

Re: Manipulate PDF

28 May 2024, 04:32

You are running a command line, so you need to send a string to it. That means all the commands shold also be in the string, all delimited by spaces.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: Manipulate PDF

28 May 2024, 10:41

Felix Siano wrote:
17 May 2024, 22:41
What am I doing wrong in ~a&2?
No ~a & 1 works

Code: Select all

~a & 1::
{
RunWait "C:\Users\siano\Desktop\pdfcpu.exe" merge "C:\Users\siano\Desktop\Junto.pdf" "C:\Users\siano\Desktop\NOTAS\*.pdf"
}

~a & 2::
{
RunWait "C:\Users\siano\Desktop\pdfcpu.exe" watermark add -mode pdf -- "C:\Users\siano\Desktop\1.pdf:1" "C:\Users\siano\Desktop\10.pdf" "C:\Users\siano\Desktop\THEEND.pdf"
}
arguments should with quotes

this example

Code: Select all

Run('"' AHK__python_Musicplayer_py__path '"' ' "' path '"' ' "' enableLoop '"' ' "' loopTime '"' , , hide__Opt ,&musicPlayer__PID )
https://github.com/xmaxrayx/MaxAHKv2-Lib/blob/main/musicplayerV1.ahk
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: DavidP, FanaticGuru, mikeyww, TomDonovan and 106 guests