I’m glad you got some value out of that old post. I was thinking about your problem last night (and my own need to occasionally add simple text to certain pages of a PDF) and learned about pdfmark, an Adobe system for adding annotations and other things to existing PDFs. If you have Ghostscript installed—say, through Homebrew—it’s very easy to set up a text-only system for annotating PDFs.
First, you create a file with a few pdfmark commands, like this:
[
/Subtype /FreeText
/SrcPg 1
/Rect [206 758 406 774]
/Color [1 1 .75]
/DA (/HeBo 14 Tf 0 0 .5 rg)
/Contents (My Filename Here)
/ANN pdfmark
This creates a light yellow box at the top of the first page with dark blue text saying “My Filename Here.”
The light yellow box came from the
/Rect [206 758 406 774]
/Color [1 1 .75]
lines. The right, bottom, left, and top coordinates of the box are given in PostScript points, which are 1/72 of an inch and have an origin at the bottom left corner of the page. A letter-sized page is 612×792 points, so you can see that the box is centered left/right with its top a quarter of an inch from the top of the page. PDF colors are typically expressed in RGB values, where the intensity of each color goes from 0 to 1. White is the default background color, so if that’s what you want, you can just leave out the /Color
line.
The font is chosen through the
/DA (/HeBo 14 Tf 0 0 .5 rg)
line. It’s Helvetica Bold, 14 pt, and dark blue. If you want black text, which is the default, leave out the 0 0 .5 rg
part. If you want regular Helvetica, use /Helv
instead of /HeBo
.
You can work out other ways to use pdfmark from this set of examples.
With the pdfmark commands saved in a file called pdfmark.txt
and the original PDF as original.pdf
, we create the annotated PDF with this Ghostscript command:
gs -dBATCH -dNOPAUSE -dQUIET -sDEVICE=pdfwrite -sOutputFile=annotated.pdf pdfmarks.txt original.pdf
Not the sort of thing you want to type in directly from the command line on a regular basis, but very easy to script. You could create temporary pdfmark.txt
files on the fly using a template that changes only the /Contents
line.
Anyway, if the AppleScript solution works for you, feel free to ignore this. But I have a feeling a solution using pdfmark will take less work overall.