Splitting a pdf with pdftk into batches of 2 pages
I have a pdf document with an even number of pages, say 2n, and I would like to split it into n documents, say pages1_2.pdf, pages3_4.pdf, ....., pages2n-1_2n.pdf, each of which consists of 2 successive pages. More precisely, for any i in the interval {1..2n}, the i-th file should consist of page 2i-1 and page 2i of the original document.
pdftk does not seem to have a dedicated function but a for loop could achieve this.
Could you please help me in writing the right script? Thank you in advance
1 Answer
First obtain the number of pages in the document:
TOTAL=`pdftk file.pdf dump_data | grep NumberOfPages | awk '{print $2}'` Check the result with
echo $TOTALNow let's insert that into a loop where we loop from 1 to $TOTAL, skipping 1 each time. Each time around the loop, we will get the i-th and the i-th plus one pages and put them into a file.
for i in `seq 1 2 $TOTAL`
do
j=$((i+1))
echo "pdftk source.pdf cat $i-$j output output_$i-$j.pdf"
done 1