Move PDF Pages programmatically C#

Moving a Page from one PDF Document to Another

This topic explains how to move page from one PDF document to the end of another document using C#.

The following code snippet also work with Aspose.PDF.Drawing library.

To move an page we should:

  1. Create a Document class object with the source PDF file.
  2. Create a Document class object with the destination PDF file.
  3. Get Page from the the PageCollection collection’s.
  4. Add page to the destination document.
  5. Save the output PDF using the Save method.
  6. Delete page in source document.
  7. Save the source PDF using the Save method.

The following code snippet shows you how to move one page.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void MovingAPageFromOnePdfDocumentToAnother()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    // Open PDF documents
    using (var srcDocument = new Aspose.Pdf.Document(dataDir + "MovingPageInput.pdf"))
    {
        using (var dstDocument = new Aspose.Pdf.Document())
        {
            var page = srcDocument.Pages[2];
            dstDocument.Pages.Add(page);
            // Save PDF document
            dstDocument.Save(dataDir + "MovingPage_out.pdf");
            srcDocument.Pages.Delete(2);
            // Save PDF document
            srcDocument.Save(dataDir + "MovingPageInput_out.pdf");
        }
    }
}

Moving bunch of Pages from one PDF Document to Another

  1. Create a Document class object with the source PDF file.
  2. Create a Document class object with the destination PDF file.
  3. Define an array with page numbers to be moved.
  4. Run loop through array:
    1. Get Page from the the PageCollection collection’s.
    2. Add page to the destination document.
  5. Save the output PDF using the Save method.
  6. Delete page in source document using array.
  7. Save the source PDF using the Save method.

The following code snippet shows you how to move a bunch of pages from one PDF document to another.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void MovingBunchOfPagesFromOnePdfDocumentToAnother()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    // Open PDF documents
    using (var srcDocument = new Aspose.Pdf.Document(dataDir + "MovingBunchOfPagesInput.pdf"))
    {
        using (var dstDocument = new Aspose.Pdf.Document())
        {
            var pages = new[] { 1, 3 };
            foreach (int pageIndex in pages)
            {
                var page = srcDocument.Pages[pageIndex];
                dstDocument.Pages.Add(page);
            }
            // Save PDF document
            dstDocument.Save(dataDir + "MovingBunchOfPages_out.pdf");
            srcDocument.Pages.Delete(pages);
            // Save PDF document
            srcDocument.Save(dataDir + "MovingBunchOfPagesInput_out.pdf";
        }
    }
}

Moving a Page in new location in the current PDF Document

  1. Create a Document class object with the source PDF file.
  2. Get Page from the the PageCollection collection’s.
  3. Add page to the new location (for example to end).
  4. Delete page in previous location.
  5. Save the output PDF using the Save method.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void MovingAPageInNewLocationInTheCurrentPdfDocument()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Pages();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "MovingAPageInNewLocationInTheCurrentPdfDocumentInput.pdf"))
    {
        var page = document.Pages[2];
        document.Pages.Add(page);
        document.Pages.Delete(2);
        // Save PDF document
        document.Save(dataDir + "MovingAPageInNewLocationInTheCurrentPdfDocument_out.pdf");
    }
}