How to add page numbers to Postscript/PDF

前端 未结 14 1867
臣服心动
臣服心动 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:08

    I used to add page numbers to my pdf using latex like in the accepted answer.

    Now I found an easier way: Use enscript to create empty pages with a header containing the page number, and then use pdftk with the multistamp option to put the header on your file.

    This bash script expects the pdf file as it's only parameter:

    #!/bin/bash
    input="$1"
    output="${1%.pdf}-header.pdf"
    pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)
    enscript -L1 --header='||Page $% of $=' --output - < <(for i in $(seq "$pagenum"); do echo; done) | ps2pdf - | pdftk "$input" multistamp - output $output
    
    0 讨论(0)
  • 2020-12-07 12:09

    You can use the free and open source pdftools to add page numbers to a PDF file with a single command line.

    The command line you could use is:

    pdftools --input-file input.pdf --output output.pdf --text $page/$pages 0.9 0.9 --fitpaper
    

    Regarding the --text option:

    • The first parameter is the text to add. Some placeholders are available. $page stands for the current page number, while $pages stands for the total number of pages in the PDF file. Thus the option so formulated would add something like "1/10" for the first page of a 10-page PDF document, and so on for the following pages
    • The second parameter is the horizontal position of the text as a percentage of the page size
    • The third parameter option is the vertical position of the text as a percentage of the page size

    The --fitpaper option is used to maintain the dimension of the input pdf file in the output pdf file.

    Disclaimer: I'm the author of pdftools

    0 讨论(0)
  • 2020-12-07 12:09

    Maybe pstops (part of psutils) can be used for this?

    0 讨论(0)
  • 2020-12-07 12:10

    This might be a solution:

    1. convert postscript to pdf using ps2pdf
    2. create a LaTeX file and insert the pages using the pdfpages package (\includepdf)
    3. use pagecommand={\thispagestyle{plain}} or something from the fancyhdr package in the arguments of \includepdf
    4. if postscript output is required, convert the pdflatex output back to postscript via pdf2ps
    0 讨论(0)
  • 2020-12-07 12:10

    I liked the idea of using pspdftool (man page) but what I was after was page x out of y format and the font style to match the rest of the page.

    To find out about the font names used in the document:

    $ strings input.pdf | grep Font
    

    To get the number of pages:

    $ pdfinfo input.pdf | grep "Pages:" | tr -s ' ' | cut -d" " -f2
    

    Glue it together with a few pspdftool commands:

    $ in=input.pdf; \
    out=output.pdf; \
    indent=30; \
    pageNumberIndent=49; \
    pageCountIndent=56; \
    font=LiberationSerif-Italic; \
    fontSize=9; \
    bottomMargin=40; \
    pageCount=`pdfinfo $in | grep "Pages:" | tr -s ' ' | cut -d" " -f2`; \
    pspdftool "number(x=$pageNumberIndent pt, y=$bottomMargin pt, start=1, size=$fontSize, font=\"$font\")" $in tmp.pdf; \
    pspdftool "text(x=$indent pt, y=$bottomMargin pt, size=$fontSize, font=\"$font\", text=\"page \")" tmp.pdf tmp.pdf; \
    pspdftool "text(x=$pageCountIndent pt, y=$bottomMargin pt, size=$fontSize, font=\"$font\", text=\"out of $pageCount\")" tmp.pdf $out; \
    rm tmp.pdf;
    

    Here is the result:

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题