from Patrick Gundlach |

Page shuffling

Categories: speedata Publisher

Yesterday I came across a Reddit question:

“I have a pdf where the pages somehow got warped into page 2 then 1 then 4 then 3 then 6 then 5…etc Everything is right except the even pages are a step ahead of the odds.”

This is very easy to do with the speedata Publisher:

<Layout xmlns=""
    <Record element="data">
        <SetVariable variable="fn" select="'fivepages.pdf'" />
        <SetVariable variable="cp" select="sd:number-of-pages($fn)" />
        <Loop select="$cp div 2 " variable="i">
            <PlaceObject row="0mm" column="0mm">
                <Image file="{$fn}" page="{$i * 2}" />
            <ClearPage />
            <PlaceObject row="0mm" column="0mm">
                <Image file="{$fn}" page="{$i * 2 - 1}" />
            <ClearPage />
            <Case test="sd:odd($cp)">
                <PlaceObject row="0mm" column="0mm">
                    <Image file="{$fn}" page="{$cp}" />

(use sp --dummy to run the speedata Publisher)

I save the number of pages of the document in a variable called $cp, then loop for half of the pages ($i) and insert pages $i * 2 and $i * 2 - 1. So I start with $i = 1 which gives pages 2 and 1, then with $i = 2, which gives pages 4 and 3, and so on. In case of an odd number of pages, I need to insert the last page as well.