# Reordering DynaPDFMBS pages - a maths question

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.

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…?

There are a lot of ways. You decide between

• MovePage after importing all pages
• importPDFpage in right Order
• create blank pages and import content with ImportPageEx
• or for Lite only: run importPDFfile often and delete all pages you do not want.

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.

And of course. make a backup first

[quote]importPDFpage in right Order
create blank pages and import content with ImportPageEx[/quote]

These functions take a page number , but not a source document.
So if I have one PDF object called PDF1 and another called PDF2,

I cannot find a way to get page 6 of the other PDF, something like this:

``call PDF1.ImportPage (PDF2,6)``

Basically, Import page from where?

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.

I don’t follow.
The ImportPage command doesnt have reference to work and import documents.

Is there an example in the folders anywhere?

Well, you do something like this:

[code]// start new PDF context
pdf = new MyDynapdfMBS
call pdf.CreateNewPDF nil

// import PDF
dim n as integer = pdf.OpenImportFile(InputFile, 0, “”)
if n < 0 then
Return false
end if

call pdf.ImportPDFPage(PageToImport)[/code]

So first part starts new PDF.
Than you open import file.

And in the next lines you can call ImportPDFPage to import all the pages from input PDF to current PDF.

or here a combine example:

[code]dim pdf as new MyDynapdfMBS

pdf.SetLicenseKey “Lite” // For this example you can use a Lite, Pro or Enterprise License

dim outFile as folderitem = GetSaveFolderItem(MyFileTypes.Pdf, “Combine PDF files.pdf”)

if outFile = nil then Return

call pdf.CreateNewPDF(outFile)

dim flags as integer = Bitwise.BitOr(pdf.kifImportAsPage, pdf.kifImportAll)
call pdf.SetImportFlags(flags)

dim c as integer = List.ListCount-1
for i as integer = 0 to c

dim f as FolderItem = List.CellTag(i, 0)

call pdf.openimportFile(f, pdf.kptopen, “”)
call pdf.ImportPDFFile(pdf.GetPageCount+1, 1.0, 1.0)
call pdf.CloseImportFile

next

call pdf.closefile[/code]

It loops over a listbox called list with folderitems for all the PDF files to merge.

Thanks.
I will give these a try.