Move Pages to PDF using Java

Move Pages in PDF document. Use Aspose.PDF for Java to modify PDF files programmatically

How to Move pages to PDF using Java

In order to move page, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Move Page to PDF using Java


You need Aspose.PDF for Java to try the code in your environment.

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

Moving a Page from one PDF Document to Another

// Open document
String srcFileName = DATA_DIR.resolve("sample.pdf").toString();
String dstFileName = DATA_DIR.resolve("java-move-page.pdf").toString();

Document srcDocument = new Document(srcFileName);
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);
srcDocument.close();
dstDocument.close();