If you ever need to stamp a PDF with something like “Confidential” or “Draft”, you do not have to buy Adobe Acrobat. You can do it straight from Terminal using Ghostscript.
The command below takes an input file named original.pdf, adds a diagonal watermark to every page, then saves a new file called watermarked.pdf. It uses Ghostscript only. There is no Acrobat, no extra PDF tools, and no separate watermark image.
Before running it, check whether Ghostscript is already installed by typing gs --version in Terminal. Many Linux desktop installs already include it because printing stacks often depend on it, but minimal servers and containers usually do not. macOS does not ship it by default, and Windows does not either.
On macOS, the easiest path is Homebrew. If you already have brew, run brew install ghostscript. On Ubuntu or Debian, install it with sudo apt install ghostscript.
gs -dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite \
-sOutputFile=watermarked.pdf \
-c '
/Helvetica-Bold findfont 150 scalefont setfont
<<
/BeginPage {
currentpagedevice /PageSize get aload pop
/h exch def /w exch def
/txt (Confidential) def
gsave
% Set Transparency: /ca is alpha (0.0 to 1.0)
[ /ca 0.3 /BM /Normal /SetTransparency pdfmark
0.85 setgray
w 2 div h 2 div translate
45 rotate
newpath 0 0 moveto
txt true charpath pathbbox
/ury exch def /urx exch def /lly exch def /llx exch def
llx urx add 2 div neg
lly ury add 2 div neg
moveto
txt show
grestore
true
}
>> setpagedevice
' -f original.pdf
Customizing the watermark is straightforward because the script has a few obvious knobs. To change the text, edit the line /txt (Confidential) def and replace the word inside the parentheses. To change the angle, adjust 45 rotate. To change the size of the watermark, edit 150 scalefont up or down.
Need to go thru multiple files in one go? It is very simple. The script below (works on MacOS and Linux) will run a small loop that processes every .PDF file in the current directory. A new watermarked copy of the file will be created, while leaving the originals untouched. In a few seconds, an entire folder of documents can be stamped.
for f in *.pdf; do
gs -dNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite \
-sOutputFile="watermarked_$f" \
-c '
/Helvetica-Bold findfont 150 scalefont setfont
<<
/BeginPage {
currentpagedevice /PageSize get aload pop
/h exch def /w exch def
/txt (Confidential) def
gsave
[ /ca 0.3 /BM /Normal /SetTransparency pdfmark
0.85 setgray
w 2 div h 2 div translate
45 rotate
newpath 0 0 moveto
txt true charpath pathbbox
/ury exch def /urx exch def /lly exch def /llx exch def
llx urx add 2 div neg
lly ury add 2 div neg
moveto
txt show
grestore
true
}
>> setpagedevice
' -f "$f"
done