I need to be able to rearrange pages of a PDF.
DynaPDFMBS exposes the MovePage(from to) method, so it is certainly possible.
My issue is how to calculate new page positions, as the original page numbers change as soon as the move occurs.
Visual Example:
If I have 9 pages (the number will vary, but there will always be a number formed by multiplying two integers such as 4x3 or 7 x 6)
Pages 1 to 9 usually come out in in page order 1…9
They can be considered as a map and laid out like this.
147
258
369
But I would like the ability to shuffle these pages about as a user preference so that what comes out is in this sequence in the PDF:
147 258 369
Obviously 1 and 9 stay put.
What I cannot work out is what kind of method will take the original page order and move the pages to this new order.
For example, if I start by moving page 6 to position 8, the page that used to be page 7 is now page 6 in the ordinal.
If I took old page 4 and made it new page 2, everything after page 3 is suspect.
I would make a a new document and than use ImportPage function to import the pages with the indexes as needed.
This way you can append the pages you need to import.
How about this for an idea:
If I insert (n) blank pages at the start, then use EditPage(n)
I might be able to output the commands for pages 1,2,3 but be affecting pages 1 , 4, 7 as I create the document…?
depending on how often you need to do this, and how big your document is…
it can be done “manually”
open the PDF in a texteditor (if you see “gibberish” don’t worry, just DON’T change any of it)
find the clear text that looks like
<</Type /Pages
/Kids [5 0 R 3 0 R 4 0 R]
/Count 3>>
the number may be different, and there will be one pair times the value in COUNT (# of pages)
the pairs may be vastly different, but the important item is their ORDER
If I changed the above to
/Kids [3 0 R 4 0 R 5 0 R]
the document would now appear as Page 2, Page 3, Page 1
PDF is very sensitive to filesize and pointers… so make sure you don’t introduce more spaces, and only rearrange the pairs using the EXACT same values that were already there.
In DynaPDF you always have a current working PDF and an import PDF.
So you open a PDF with OpenImportFile/OpenImportBuffer and than import from that one into the work PDF.