How to add page numbers to Postscript/PDF

前端 未结 14 1896
臣服心动
臣服心动 2020-12-07 12:06

If you\'ve got a large document (500 pages+) in Postscript and want to add page numbers, does anyone know how to do this?

14条回答
  •  天命终不由人
    2020-12-07 12:13

    Further to captaincomic's solution, I've extended it to support the starting of page numbering at any page.

    Requires enscript, pdftk 1.43 or greater and pdfjam (for pdfjoin utility)

    #!/bin/bash
    input="$1"
    count=$2
    blank=$((count - 1))
    output="${1%.pdf}-header.pdf"
    pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)
    (for i in $(seq "$blank"); do echo; done) | enscript -L1 -B --output - | ps2pdf - > /tmp/pa$$.pdf
    (for i in $(seq "$pagenum"); do echo; done) | enscript -a ${count}- -L1 -F Helvetica@10 --header='||Page $% of $=' --output - | ps2pdf - > /tmp/pb$$.pdf
    pdfjoin --paper letter --outfile /tmp/join$$.pdf /tmp/pa$$.pdf /tmp/pb$$.pdf &>/dev/null
    cat /tmp/join$$.pdf | pdftk "$input" multistamp - output "$output"
    rm /tmp/pa$$.pdf
    rm /tmp/pb$$.pdf
    rm /tmp/join$$.pdf
    

    For example.. place this in /usr/local/bin/pagestamp.sh and execute like:

    pagestamp.sh doc.pdf 3
    

    This will start the page number at page 3.. useful when you have coversheets, title pages and table of contents, etc.

    The unfortunate thing is that enscript's --footer option is broken, so you cannot get the page numbering at the bottom using this method.

提交回复
热议问题