Last active
October 11, 2015 15:08
-
-
Save pi8027/3877749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage_exit(){ | |
echo "usage: pagenum [-o offset] file.pdf" 1>&2 | |
exit 1 | |
} | |
pagenum_ps(){ | |
cat <<EOT | |
/input (%stdin) (r) file def | |
/input_buffer 2048 string def | |
/cshow { dup stringwidth pop 2 div neg 0 rmoveto show } def | |
/showpage { | |
input input_buffer readline pop cvx exec | |
systemdict /showpage get exec | |
} def | |
EOT | |
} | |
offset=0 | |
while getopts o: OPT ; do | |
case $OPT in | |
o) offset=$OPTARG ;; | |
*) usage_exit ;; | |
esac | |
done | |
shift `expr $OPTIND - 1` | |
file=$1 | |
[[ -z $file ]] && usage_exit | |
pdf2ps $file temp.ps | |
for i in $(seq 1 $(pdfinfo $file | grep -a "^Pages:" | sed "s/[^0-9]//g")); do | |
pagenum=$((offset + i)) | |
xargs -d '\n' <<EOT | |
gsave | |
/Helvetica findfont 10 scalefont setfont | |
$pagenum 2 mod 0 eq { 50 } { 464 } ifelse 30 moveto | |
($pagenum) cshow | |
grestore | |
EOT | |
done | gs -sDEVICE=pdfwrite -sOutputFile=output.pdf -dNOPAUSE -dBATCH -q <(pagenum_ps) temp.ps | |
rm temp.ps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/input (%stdin) (r) file def | |
/input_buffer 2048 string def | |
/cshow { dup stringwidth pop 2 div neg 0 rmoveto show } def | |
/showpage { | |
input input_buffer readline pop cvx exec | |
systemdict /showpage get exec | |
} def |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment