Script to Split Pdf and Add WaterMark to MultiPDF

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Script to Split Pdf and Add WaterMark to MultiPDF

Post by Tiago » 2018-02-23T03:18:58-07:00

Hello everyone,

I need to convert a folder with multiple pdfs at any given time and a i need a script to transform each and every multipdf into single pdfs.

for example, i have a file called A1.pdf, its a pdf with 300 pages, and i need to split it into 300 single pdfs.

but i need to do it in a batch so that a folder with multiple pdfs creates a folder with the same name as the original pdf and puts all the single pdfs inside.

ive manage to do something similar with tif files

@echo off
cls
setlocal EnableDelayedExpansion
pushd I:\TESTE_MULTIPDFPDF-SINGLEPDF\MULTITIFF
echo.
echo --- MultiTif to Tif---
echo.
echo Using magick
echo.
echo.
cd I:\TESTE_MULTIPDFPDF-SINGLEPDF\MULTITIFF
FOR /R %%a IN (*.TIF) DO (
Set Folder=%%~dpa
set nome=%%~na
cd !Folder!

echo A converter ficheiro !nome! na pasta !Folder!

REM ********* Cria pasta SINGLETIFF e cria ficheiros individuais
md ..\SINGLETIFF
I:\convertfile\Magic\convert -scene 1 %%a ..\SINGLETIFF\%%~na_%%d.tif

)

but in this case it only creates a folder called singletiff and puts the single tiffs inside.

any help would be much appriciated.

Kind Regards to All
Last edited by Tiago on 2018-02-26T04:34:11-07:00, edited 1 time in total.

snibgo
Posts: 10273
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Script to batch split Multipdf to pdf

Post by snibgo » 2018-02-23T03:39:04-07:00

You can do the same "convert" command with PDF files.

However, ImageMagick is a raster image processor. So it will rasterize each PDF page and put each of those raster images inside its own PDF file. This probably isn't what you really want.
snibgo's IM pages: im.snibgo.com

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to batch split Multipdf to pdf

Post by Tiago » 2018-02-23T04:03:37-07:00

Thanks for the quick response snibgo

I had tried that but with no results.

as soon as i got your response i decided to try with a smaller pdf and it worked like a charm, i just had to be more patient.

That was the first thing i did, replace the .tif to .pdf but the folder created was not getting the single pdfs and i assumed that it wasnt working.

Thank you so much for your help.

Just a quick question, in addition to this i also need to get a watermark in all the pdfs inside a folder, is that possible with imagemagick or i do i need to check other options?

Kind regards

snibgo
Posts: 10273
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Script to batch split Multipdf to pdf

Post by snibgo » 2018-02-23T04:44:27-07:00

snibgo's IM pages: im.snibgo.com

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to batch split Multipdf to pdf

Post by Tiago » 2018-02-26T02:17:01-07:00

using a similar script as before but changing the command in the last row shouldn't be enough?

i tried this but i didn't succeed, any help would be appreciated.

@echo off
cls
setlocal EnableDelayedExpansion
pushd I:\TESTE_MULTIPDFPDF-SINGLEPDF\MULTIPDF
echo.
echo ---Watermarking PDF---
echo.
echo Using magick
echo.
echo.
cd I:\TESTE_MULTIPDFPDF-SINGLEPDF\MULTIPDF
FOR /R %%a IN (*.PDF) DO (
Set Folder=%%~dpa
set nome=%%~na
cd !Folder!

echo A converter ficheiro !nome! na pasta !Folder!

REM ********* Creates folder Watermarked and all the files
md ..\WATERMARKED
C:\Users\Tiago.Lima\Desktop\TESTE-WATERMARK\convertfile\Magic\composite logo.png -dissolve 25% -gravity south \%%a ..\WATERMARKED\%%~na_%%d.pdf

i have the logo.png and the script in one folder and the pdfs in another folder CALLED MULTIPDF but i've tried putting them all in the same folder but it didn't work either.

snibgo
Posts: 10273
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Script to batch split Multipdf to pdf

Post by snibgo » 2018-02-26T04:17:12-07:00

You pushd to a directory then cd to the same directory. Why?

The input to your convert command is "\%%a". The leading backslash means this is a directory. But (a) convert expects a file, not a directory and (b) %%a without the backslash is a file, not a directory. So remove the leading backslash.
snibgo's IM pages: im.snibgo.com

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to batch split Multipdf to pdf

Post by Tiago » 2018-02-26T04:32:14-07:00

@echo off
cls
setlocal EnableDelayedExpansion
pushd C:\Users\Tiago.Lima\Desktop\TESTEPDF\PDFS
echo.
echo ---Watermarking PDF---
echo.
echo Using magick
echo.
echo.
FOR /R %%a IN (*.PDF) DO (
Set Folder=%%~dpa
set nome=%%~na
cd !Folder!

echo A converter ficheiro !nome! na pasta !Folder!

REM ********* Creates folder Watermarked and all the files
md ..\WATERMARKED
C:\Users\Tiago.Lima\Desktop\TESTE-WATERMARK\convertfile\Magic\composite logo.png -dissolve 25% -gravity south %%a ..\WATERMARKED\%%~na_%%d.pdf

when i try like this i cant even see the script running, it closes immediately.

snibgo
Posts: 10273
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Script to Split Pdf and Add WaterMark to MultiPDF

Post by snibgo » 2018-02-26T04:47:30-07:00

Why do you have "@echo off"?

You have no error trapping.

You open a parenthesis at "for", but never close it.
snibgo's IM pages: im.snibgo.com

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to Split Pdf and Add WaterMark to MultiPDF

Post by Tiago » 2018-02-26T04:50:09-07:00

Im new to scripts and i basically modified a script that i found to suit my needs, after some tries a managed to get the multitiff to split and then passed to pdf and now im trying to add the watermark but for some reason i cant get it to work

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to Split Pdf and Add WaterMark to MultiPDF

Post by Tiago » 2018-02-27T01:01:52-07:00

any help would be appreciated!

Kind Regards

Tiago
Posts: 7
Joined: 2018-02-23T03:01:45-07:00
Authentication code: 1152

Re: Script to Split Pdf and Add WaterMark to MultiPDF

Post by Tiago » 2018-02-28T06:42:00-07:00

i resolved this issue another way, i also need to add the watermark to some photos so for now i had the watermark to the images and then convert the image with the watermark to pdf

i leave the script for anyone that may need it, just change the bold parts to suit your OS

@echo off
cls
setlocal EnableDelayedExpansion
pushd C:\Users\Tiago.Lima\Desktop\TESTE\27022018
echo.
echo
echo.
echo Add Watermark creating TIFF and PDF Watermarked
echo.
echo.
cd C:\Users\Tiago.Lima\Desktop\TESTE\27022018
FOR /R %%a IN (*.TIF) DO (
Set Folder=%%~dpa
set nome=%%~na
cd !Folder!

echo Adding Watermark to file !nome! on folder !Folder!
REM ********* Creates the folder WATER and the tiff files with the watermark
md ..\WATER
C:\Users\Tiago.Lima\Desktop\TESTE-WATERMARK\convertfile\Magic\composite -dissolve 20 -gravity center -geometry +160+13 C:\logo.bmp %%a ..\WATER\%%~na.tif

pushd C:\Users\Tiago.Lima\Desktop\TESTE\27022018\WATER
echo Creating File !nome! on folder !Folder!
REM ********* Creates the folder JPG and the jpg files with the watermark
echo|set /p="...JPG"
if not exist "..\JPG" md ..\JPG
C:\Users\Tiago.Lima\Desktop\TESTE-WATERMARK\convertfile\Magic\mogrify -quiet -format jpg -quality 50%% -path ..\JPG %%~nxa

REM ********* Creates folder SINGLEPDF and all the PDF files with the watermark
echo|set /p="...SINGLEPDF"
if not exist "..\SINGLEPDF" md ..\SINGLEPDF
C:\Users\Tiago.Lima\Desktop\TESTE-WATERMARK\convertfile\Magic\mogrify -quiet -format pdf -compress JPEG -quality 50 -path ..\SINGLEPDF %%~nxa

)

so what the script does is:
grabs the tiff and creates a new tiff with watermark
grabs the watermarked tiff and turns it into a jpg (i also need to have the jpg for some clients that require it)
grabs the watermarked tiff and converts it into a pdf

the tiff goes to a TIFF folder
the jpg goes to a JPG folder
the PDF goes to a SINGLEPDF folder because in some cases i may need to create a multi pdf from all the single pdfs created

feel free to give me some input on how to make the script better

Post Reply