Moving PDF pages

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Moving PDF pages

Post 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
Alex
Site Admin
Posts: 2304
Joined: Thu Jul 10, 2008 2:21 pm

Re: Moving PDF pages

Post 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
DanielLW
Posts: 22
Joined: Tue Sep 01, 2015 2:42 pm

Re: Moving PDF pages

Post by DanielLW »

The second way works, thank you!
Post Reply