Moving pages within a PDF document

I’m trying to move a group of consecutive pages in a pdf document.

In your samples they all show moving to the end of the pdf. How do I move, for example, pages 80-81 to page 2 in the same pdf document?

@ns1

You can move any number of pages to any location or sequence in the current PDF using Aspose.PDF. Please find more information in the below article:

In case you still face any issues, please share your sample PDF with us so that we can further test the scenario in our environment and address it accordingly.

Thanks for your reply and I did see that page. My question is how do you move those pages to a position in a pdf that isn’t the end of the pdf.

From the code how would I move those pages to after the second page, for example:

public static void MovePagesInOnePDF()
{
    var srcFileName = "<enter file name>";
    var dstFileName = "<enter file name>";
    var srcDocument = new Aspose.Pdf.Document(srcFileName);
   
    var page = srcDocument.Pages[2];
    srcDocument.Pages.Add(page);  <== add the selected pages to after page 2?
    srcDocument.Pages.Delete(2);          
   
    // Save output file
    srcDocument.Save(dstFileName);
}

@ns1

In that case, you can use Document.Pages.Insert() method. This method will place the desired page at desired location among other pages.

Thanks for your reply but I did see that page too but insert only works with external documents from what I could see, [Insert PDF pages|Aspose.PDF for .NET].

Could you please post an example of how I could use insert to take, for example, an 80 page pdf and move pages 50-55 to position 6 in the same pdf.

I couldn’t see how to do this in the API but the move pages link you attached in a previous post says you can do it but only shows how to add to the end of the document.

Thanks for you help with this

@ns1

Can you please share a sample PDF document for our reference as well? We will use it while creating a code sample for you and shar it with you.

I have attached a 6 page pdf, so if you could show an example of taking page 4 & 5 and putting them after page 2.

Thanks

Adobe Cloud Services.pdf (303.3 KB)

@ns1

Please check the below code snippet along with the generated output by it:

Document doc = new Document(dataDir + "Adobe Cloud Services.pdf");
var page4toadd = doc.Pages[4];
var page5toadd = doc.Pages[5];
doc.Pages.Insert(3, page4toadd);
doc.Pages.Insert(4, page5toadd);
doc.Pages.Delete(page4toadd.Number);
doc.Pages.Delete(page5toadd.Number);
doc.Save(dataDir + "MovedPages.pdf"); 

Thanks for sending that code through unfortunately for me I get a corrupted pdf created when I try an open it in Adobe Reader.

Here is my code:

Document doc = new Document(“C:\Adobe Cloud Services.pdf”);
var page4toadd = doc.Pages[3];
var page5toadd = doc.Pages[4];
doc.Pages.Insert(2, page4toadd);
doc.Pages.Insert(3, page5toadd);
doc.Pages.Delete(page4toadd.Number);
doc.Pages.Delete(page5toadd.Number);
doc.Save(“C:\Adobe Cloud Services(MovedPages).pdf”);

I don’t seem to have the option to attach the pdf created from this code, did you see the same thing or do you have any idea what I’m doing wrong here?

Here is the message I get from Adobe Reader
image.png (7.0 KB)

and hopefully this is a link to the document created Adobe Cloud Services(MovedPages).pdf (284.1 KB)

@ns1

We did not notice any such issue in our environment using same code snippet. Please check the attached output PDF for your reference as well. Also, would you please confirm using latest version of the API i.e. 22.8?MovedPages.pdf (306.1 KB)

Thanks for sending the document through, I can confirm that I too can open that document successfully.

I am using version 17.4, is this something that didn’t work in that version or is there a different way to do the page move in 17.4?

@ns1

There must be some issue in the older version of the API as the code is very basic and working with the latest version. Please download and use the latest available version as always recommended and feel free to let us know in case you face any issues.

Ok thanks for all your help with this issue

1 Like