0

The goal is to enumerate a set of .pdf files using command line tools. Ideally a free open source package would lend itself to performing the enumeration tasks on both Catalina and Ubuntu 20.04.

Assume a set of files:

  1. file1.pdf
  2. file2.pdf
  3. file3.pdf

Algorithm:

  1. Initialize the counter to 100 only once: do not reset ever to 100
  2. Starting with the first page of file1.pdf:
  3. Enumerate (stamp) the bottom right corner with "myText [counter]"
  4. increment the counter and stamp the next page
  5. if the last page of the file has been enumerated, repeat the process with the next file until all pages of all files are enumerated. The next file shall continue using the same counter, which will NOT be reset to 100.

Solutions / examples to accomplish the goal are appreciated.

CLARIFICATION:

All pages shall have a unique page numbers: If each document has 10 pages, then the last page number will be 129.

1 Answer 1

1

An update for the revised question...

#!/bin/bash

label="$1"    #the label to be added to the footer
from=$2       #the starting number

#for each pdf in this directory
for input in *.pdf; do

  #compose an output file name
  output=${input%.*}.numbered.pdf
  
  #count the pages
  pagenum=$(pdftk "$input" dump_data | grep "NumberOfPages" | cut -d":" -f2)
  
  #calculate the last page no
  to=$(($from+$pagenum-1))
              
  (echo -e ".nr FM 0.125i\n.ds CH"; for s in $(seq -f "%05g" $from $to); do 
       echo -e ".ds RF $label $s \n.bp +1"; done) | 
       groff -ms -Tpdf | 
       pdftk "$input" multistamp - output "$output" 

  #get ready for the next file  
  from=$((to +1))
done

Compose a basic groff -ms document with empty pages except the right footer .ds RF containing the desired text $label $s where $s is the page number which I padded with 0's in seq -f "%05g"

groff -ms macro applies a page header which is suppressed with .ds CH and .nr FM 0.125 reduces the footer margin height, so each "page" is just the footer with a page break .bp +1

Piping this text into groff -Tpdf creates a pdf which is then piped into pdftk via stdin -, multistamped onto the input file and finally written to the output file.

My apologies to anyone who actually knows how to use groff properly if this is a hack .... I never even looked at it until today.

2
  • I have added a clarification to the algorithm (apologies for not getting it right the first time). The idea is that a large set of pdf documents will be enumerated / serialized such that each page has a unique number starting from 100 (or ideally whatever number one chooses). Good job documenting the source!
    – gatorback
    Commented Nov 13, 2021 at 13:12
  • So, serial numbering starting from a specific value ....
    – bu5hman
    Commented Nov 14, 2021 at 14:35

You must log in to answer this question.

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