Page 1 of 1

Manipulate PDF

Posted: 27 Apr 2024, 19:15
by Felix Siano
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?

Re: Manipulate PDF

Posted: 27 Apr 2024, 19:19
by mikeyww
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.

Re: Manipulate PDF

Posted: 29 Apr 2024, 23:06
by Felix Siano
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

Re: Manipulate PDF

Posted: 30 Apr 2024, 06:13
by mikeyww
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.

Re: Manipulate PDF

Posted: 30 Apr 2024, 10:15
by xMaxrayx
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.

Re: Manipulate PDF

Posted: 30 Apr 2024, 10:35
by Jasonosaj
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()


Re: Manipulate PDF

Posted: 30 Apr 2024, 13:18
by Felix Siano
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.

Re: Manipulate PDF

Posted: 02 May 2024, 08:14
by metallizer
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).

Re: Manipulate PDF

Posted: 02 May 2024, 11:32
by Felix Siano
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.

Re: Manipulate PDF

Posted: 07 May 2024, 03:40
by xMaxrayx
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.

Re: Manipulate PDF

Posted: 17 May 2024, 05:16
by Yfreet
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

Re: Manipulate PDF

Posted: 17 May 2024, 17:30
by Felix Siano
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?

Re: Manipulate PDF

Posted: 17 May 2024, 22:41
by Felix Siano
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"
}

Re: Manipulate PDF

Posted: 18 May 2024, 02:45
by boiler
The first one isn’t correct either unless you’re running it with v1. Do you see why?

Re: Manipulate PDF

Posted: 28 May 2024, 04:32
by Yfreet
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.

Re: Manipulate PDF

Posted: 28 May 2024, 10:41
by xMaxrayx
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