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="urn:speedata.de:2009/publisher/en"
    xmlns:sd="urn:speedata:2009/publisher/functions/en">
    <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}" />
            </PlaceObject>
            <ClearPage />
            <PlaceObject row="0mm" column="0mm">
                <Image file="{$fn}" page="{$i * 2 - 1}" />
            </PlaceObject>
            <ClearPage />
        </Loop>
        <Switch>
            <Case test="sd:odd($cp)">
                <PlaceObject row="0mm" column="0mm">
                    <Image file="{$fn}" page="{$cp}" />
                </PlaceObject>
            </Case>
        </Switch>
    </Record>
</Layout>

(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.