Move PDF Pages

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 Java. 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.

package com.aspose.pdf.examples;

import com.aspose.pdf.*;

public class ExampleMovePDFPages {

  private static String _dataDir = "/home/admin1/pdf-examples/Samples/";

  public static void MovePage() {
    String srcFileName = _dataDir + "<enter file name>";
    String dstFileName = _dataDir + "<enter file name>";
    Document srcDocument = new Document();
    Document dstDocument = new Document();
    Page page = srcDocument.getPages().get_Item(2);
    dstDocument.getPages().add(page);
    // Save output file
    dstDocument.save(srcFileName);
    srcDocument.getPages().delete(2);
    srcDocument.save(dstFileName);
  }

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 insert an empty page at the end of a PDF file.

  public static void MoveBunchPages() {
    String srcFileName = _dataDir + "<enter file name>";
    String dstFileName = _dataDir + "<enter file name>";
    Document srcDocument = new Document(srcFileName);
    Document dstDocument = new Document();

    Integer[] pages = { 1, 3 };
    for (int pageIndex : pages) {
      Page page = srcDocument.getPages().get_Item(pageIndex);
      dstDocument.getPages().add(page);
    }
    // Save output files
    dstDocument.save(srcFileName);
    srcDocument.getPages().delete(pages);

    srcDocument.save(dstFileName);
  }

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.
  public static void MovePagesInOnePDF() {
    String srcFileName = _dataDir + "<enter file name>";
    String dstFileName = _dataDir + "<enter file name>";

    Document srcDocument = new Document(srcFileName);
    Page page = srcDocument.getPages().get_Item(2);
    srcDocument.getPages().add(page);
    srcDocument.getPages().delete(2);

    // Save output file
    srcDocument.save(dstFileName);
  }
}