Adding File Names to PDFs in MacOS

Hi!
Every month I have to download about 48 invoices from Facebook Advertising. I have a script that will do this for me, but the problem is that Facebook does not put the business/location name anywhere in the PDF. So, I have a Hazel script that is able to surmise the business name and rename the files to the proper name.

But beyond that, I really need that file name on the actual file/page. It could be a header or footer, watermark, etc. But I can’t figure out how to accomplish this.

Can anyone think of a way to batch add text to PDF files like this?

Maybe try this approach outlined by @drdrang?

From the post:

Enter PDFpenPro. With a little AppleScript and an Automator action to run it, I can add the header to the first page of any PDF document by right-clicking on it in the Finder and choosing the Add Company Header service from the popup menu.

I could, I suppose, turn this into a folder action, so the header would be added to any file dropped into a particular folder. If I owned Hazel—which the latest Mac Power Users podcast has just about convinced me to do—I could turn it into a rule associated with a folder or a filename pattern.

If you don’t have PDFpen, other PDF software you have might offer similar options - the post above is almost a decade old, so the other apps around now have probably caught up a little :wink:

2 Likes

I suspect the solution @sylumer linked to won’t be satisfying, because it requires a premade PDF with the title to overlay onto the base PDF. The ideal solution would be to pass text to an AppleScript and have it added somewhere near the top or bottom edge of the document where it’s unlikely to interfere with anything else.

I’m think this can be done in PDFpen, but I’m not sure, and I’m not at a Mac right now to check on it. If you don’t have PDFpen, there are several command-line utilities that manipulate PDFs, and I think something can be worked out.

Thanks for the replies @sylumer and @drdrang! I was table to take a little of your overlay script and also a prebuilt script PDFpen has to solve the stamping portion. I just haven’t yet had a chance to pass the text to it and get it all working together.

This was one of those things where the automated/quick solution was taking way longer than just muscling through it and I was running out of time. So now I’ve got a month to work through the automation before the next batch!

Thanks for pointing me in the right direction.

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.

4 Likes

@drdrang, I salute you in showing me a rather nice looking rabbit hole… :slight_smile:

1 Like