Page 1 of 1

Moving PDF pages

Posted: Tue Oct 06, 2015 3:51 pm
by DanielLW
Hello,

how can i move pages within a pdf document, for example page number 4 to page index 1?

I've tried to use the Insert- and Remove-Methods, but when moving a page to a lower page index, this does not work, as the moved page is removed then:

Code: Select all

            int iPageToMove = 4;
            int iPageDest = 1;
            Vintasoft.Imaging.Pdf.PdfDocument pdfDocument;

            pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument(@"d:\test.pdf");

            pdfDocument.Pages.Insert(iPageDest, pdfDocument.Pages[iPageToMove]);

            if (iPageToMove > iPageDest)
                pdfDocument.Pages.Remove(pdfDocument.Pages[iPageToMove + 1]);
            else
                pdfDocument.Pages.Remove(pdfDocument.Pages[iPageToMove]);

            pdfDocument.SaveChanges(@"d:\testnew.pdf");

            pdfDocument.Dispose();
Or is there another method to move pages within a pdf file? I don't want to swap two pages, only move one.

Thank you for your help!

Best regards,
Daniel

Re: Moving PDF pages

Posted: Tue Oct 06, 2015 5:06 pm
by Alex
Hello Daniel,

Your code has logical mistake.

Here are your steps:
  • Get reference to PDF page.
  • Insert PDF page into PDF page collection. After this action PDF page collection contains 2 references to the same PDF page, not 2 different PDF pages.
  • Remove PDF page from PDF page collection. During this action the first reference to PDF page is removed from PDF page collection, i.e. you are removing PDF page which you have added before.

Here is the first possible way for solving your task:
  • Get reference to PDF page.
  • Insert PDF page into PDF page collection - you can do this because you have reference to PDF page.
  • Remove PDF page from PDF page collection using the RemoveAt method (not using Remove method).
Here is the second possible way for solving your task:
  • Get reference to PDF page.
  • Remove PDF page from PDF page collection.
  • Insert PDF page into PDF page collection - you can do this because you have reference to PDF page.
Best regards, Alexander

Re: Moving PDF pages

Posted: Thu Oct 08, 2015 5:41 pm
by DanielLW
The second way works, thank you!