4

I would like to print a PDF on windows, but want to print the path of the PDF file in the footer of the printed document (like I am able to do in Word).

We are using either Adobe Reader or Foxit Reader.

Do you know any way how to achieve this ?

3
  • Which operating system? What type of info do you need in the footer (other than the path to the PDF file)? Aug 4, 2010 at 20:39
  • Operating system: Windows with either Acrobat Reader or foxit reader / I just need the path of the file (on the computer) in the footer. Aug 5, 2010 at 7:00
  • You want this to happen automatically for any PDF file you print? Or do you need it only from time to time, and some manual work (extra clicks outside of PDF viewer) is in order? Aug 5, 2010 at 22:24

3 Answers 3

4

First off: some printer drivers would allow you to define a "watermark" for each page printed. You may be able to abuse that feature in order to do what you want. However, to the best of my knowledge, Acrobat Reader does not include such a feature (though you may be able to (ab)use the builtin JavaScript support inside Acrobat to achieve something like what you want...

OK then. Let me also give it a shot. Since we are here on superuser.com (and not stackoverflow.com), I'll do without a programming language, and use just (batch) scripting instead.

First step: Use Ghostscript to create a PDF footer page showing the 'footer' string with a path

We'll use a relatively simple Ghostscript command for that. The command takes a short PostScript snippet as commandline input (-c "...") and outputs the PDF as 'myfooter.pdf'. We'll use letter as the media size (replace with a4 or whatever you need). Here goes:
(I'm assuming we are on Windows. Should one of the readers be on Linux, use gs instead of gswin32c.exe.)

gswin32c.exe ^
 -o myfooter.pdf ^
 -sDEVICE=pdfwrite ^
 -sPAPERSIZE=letter ^
 -c "72 12 moveto /Helvetica findfont 9 scalefont setfont (File: d:\\c\\b\\a.pdf) show"

Now we have a PDF page that contains the footer only, with the rest of the canvas without any content. You can open it in a viewer and see that the string starts at 72 PostScript points (72 pt == 1 inch) to the left of the lower left corner, and 12 points to the top.

Second step: Use the PDF ToolKit to overlay the footer page with your original PDF

We'll use the PDF ToolKit's pdftk.exe command now. This commandline utility can do a lot of things to PDFs. The feature we need here: overlay pages from two different PDF documents over each other in order to provide a 'stamp' or a 'background' to the original file. Here goes:
(Should a reader be on Linux, use pdftk instead of pdftk.exe.)

pdftk.exe ^
  d:\c\b\a.pdf ^
  stamp myfooter.pdf ^
  output d:\c\b\result-with-footer-a.pdf

(BTW, pdftk is also derived from the iText libarary, the one the other answer refers to.)

Third step: Create a batch script that makes both previous steps happen automatically

This is left for your exercising pleasures... ;-)


You may want to rotate the string by 90 degree, so it reads vertically on the left page border. For this case modify the Ghostscript command like this: gswin32c.exe -sDEVICE=pdfwrite -o myfooter.pdf -dAutoRotatePages=/None -c "12 72 moveto /Helvetica findfont 9 scalefont setfont 90 rotate (File: d:\\c\\b\\a.pdf) show showpage".

0
2

Here's a windows batch file built from @Kurt's answer:

@echo off
if [%*]==[] goto :usage
if not exist outdir mkdir outdir

for %%a in (%*) do (
 call :makeFooter %%a
 call :addFooter %%a
 )
goto :eof

:makeFooter
  gswin32c ^
  -o outdir\footer_%1 ^
  -sDEVICE=pdfwrite ^
  -c "5 5 moveto /Helvetica findfont 12 scalefont setfont (%~n1) show"
  goto :eof

:addFooter
  pdftk ^
  %1 ^
  stamp outdir\footer_%1 ^
  output outdir\%1
  del outdir\footer_%1
  goto :eof

:usage
  echo. &echo. -={ Usage: %0 [list of pdf files or pattern] }=-
  echo. &echo. %~n0 foo*.pdf &echo.

2013-Feb-04:

  • bug-fix: was failing because filename was not being passed to the make & addFooter routines properly
  • change: out filename now kept same as input, also removes temporary footer files

You may need to change gswin64c for 64bit installs of ghostscript.

The units in the -c line of :makeFooter are in points. So 5 5 /moveto is 5pts in and 5pts up from bottom left of page, and /Helvetica ... 12 is to use a 12pt Helvetica font.

1
  • It does not work if the filename has spaces or umlaut-characters. But this is only a minor inconvenience compared to how great it works. :vote up:
    – masgo
    Mar 7, 2017 at 21:39
1

There is no way to auto-update this like the field in Word. But what you can do is process the file after the it is created, and moved to the location other than the source document. PDF was really designed for printing and the changed to allow forms and other stuff was just a hack.

I am guessing that you will want this to be some kind of automated process and generic, so you will need to use a seperate library to do this. One of the most prolific is iText which is a java open source library that allows the editing of PDF files. It is not too hard to use and I will provide an example below of the code you will need to make this exercise work.

You will need the JDK and iText from sourceforge. Here is the java code

import java.io.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.*;
public class AddFooter {
    public static void main(String[] args) throws Exception {
        String fileName;
        try {
            fileName = args[0];
        } catch (Exception e) {
            System.out.println("You need to pass the filename after command");
            return;
        }
        int         bottomHorizontalOffset = 100;
        int         bottomVerticalOffset = 10;
        String      workingDir = System.getProperty("user.dir");
        String      file = workingDir + "\\" + fileName;
        String      stamped = workingDir + "\\" + fileName + "_stamped.pdf";
        stamped = stamped.replaceAll("\\\\", "\\\\\\\\");
        file    = file.replaceAll("\\\\", "\\\\\\\\");
        PdfReader   pdfReader   = new PdfReader(file);
        PdfStamper  pdfStamper  = new PdfStamper(pdfReader, new FileOutputStream(stamped));

        for(int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            PdfContentByte content = pdfStamper.getUnderContent(i);
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 12);
            content.setTextMatrix(bottomHorizontalOffset, bottomVerticalOffset);
            stamped = stamped.replaceAll("\\\\\\\\", "\\\\");
            content.showText(stamped);
            content.endText();
        }
        pdfStamper.close();
    }
}

This code has some issues, and since I don't know all the specifics then here they are:

  1. A new pdf is created with a diffrent name, and that is the path and name in the footer
  2. You can tweak the bottom... variables to position the text at the bottom
  3. You need to adjust the font and size to what will match your documents
  4. It is coded for windows, so a few tweaks would be needed for *nix
  5. It expects to be run from the directory where the file is, that is how it gets the path.
  6. It will NOT autoupdate if you move the file, in fact you cannot run this a second time and eliminate the original text.
  7. This code was thrown together quickly, but does compile and work, so if you need more this would be a good starting point.

Since pdf doesn't have the 'autoupdate' fields you do not want to change the pdf association to point to a script to run this, because then every other time that you open the file it will rewrite the footer.

Have fun.

1
  • thank you for your comment -> altough I preferred pipitas answer, yours is excellent too Aug 10, 2010 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .