Generally using qpdf or cpdf executables the extra page can be added as required, https://stackoverflow.com/a/73575464/10802527 however it seems from the tone of this question you wish to do that "inline" during PDF building, In that case it is best NOT to build A.pdf B.pdf and add single blanks before merge it is better to build the full A-Z.pdf then inject the blanks as needed.
This requires you build a personalized solution where you extract page numbers so lets say without your example
- Chapter A = page 1
- Chapter B = page 4
- Chapter C = page 6
- Chapter D = page 9
so you know you need inject blank before page 4 to move 4 to 5 and that will move 6 to 7 so now you don't need to shift page 6 however page 9 is now page 10 so WILL now need inject blank before page 10. So you need to keep track of the previous up lifts.
You can write that as a simple subroutine to call one of the libraries that reports the page content as number and ideally also includes a blank page insertion, method.
in pseudo code you need 2 trackers
- if last inject was even next is odd and vice versa (flip flop)
- page inject is current + previous blanks (incremental value)
NOTE the later answer by JohnWhitington from cpdf shows that this will be easiest in above case by use
cpdf -pad-before in.pdf 4,9 -o output.pdf
cpdf will do the 2nd shifting incremental part so 4 becomes 5 (& 6 becomes 7) and also old 9 will then move to 11 but you will still need to determine the flip flop state (odd or even) of which pages need a blank before them.
roughly you need to
set flag=EVEN
loop NEXT PAGE
if page is last page then exit from loop
is page a section heading ?
then if page odd and flag is ODD save page num and set flag as EVEN and go NEXT PAGE
if page odd and flag is EVEN go next page
if page even and flag is ODD go next page
if page even and flag is EVEN save page num and set flag as ODD and go NEXT PAGE
else page not section heading go NEXT PAGE
- 1 keeps flag as EVEN
- 2 & 3 is not section
- 4 is section, is even, flag is EVEN, save page num, set flag ODD
- 5 is not section
- 6 is section, is even, flag is ODD (skip save num)
- 7 & 8 is not section
- 9 is section, is odd, flag is ODD, save page num, set flag EVEN
... etc.