> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/android/get-started/samples/pdfpagetest.md).

# PDFPage

Sample Java code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations.

Sample Java code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations. Learn more about our [Android SDK](/core/get-started/languages/java.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples;

import com.pdftron.android.pdfnetsdksamples.OutputListener;
import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
import com.pdftron.android.pdfnetsdksamples.R;
import com.pdftron.android.pdfnetsdksamples.util.Utils;
import com.pdftron.pdf.PDFDoc;
import com.pdftron.pdf.Page;
import com.pdftron.pdf.PageIterator;
import com.pdftron.sdf.SDFDoc;

import java.util.ArrayList;

public class PDFPageTest extends PDFNetSample {

	private static OutputListener mOutputListener;

	private static ArrayList<String> mFileList = new ArrayList<>();

    public PDFPageTest() {
        setTitle(R.string.sample_pdfpage_title);
        setDescription(R.string.sample_pdfpage_description);
    }

	@Override
	public void run(OutputListener outputListener) {
		super.run(outputListener);
		mOutputListener = outputListener;
		mFileList.clear();
		printHeader(outputListener);

        // Sample 1 - Split a PDF document into multiple pages
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 1 - Split a PDF document into multiple pages...");
        mOutputListener.println("Opening the input pdf...");
        try (PDFDoc in_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
            in_doc.initSecurityHandler();

            int page_num = in_doc.getPageCount();
            for (int i = 1; i <= page_num; ++i) {
                try (PDFDoc new_doc = new PDFDoc()) {
                    new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null);
                    String fname = "newsletter_split_page_" + i + ".pdf";
                    new_doc.save(Utils.createExternalFile(fname, mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
                    mOutputListener.println("Done. Result saved in newsletter_split_page_" + i + ".pdf");
                }
            }
        } catch (Exception e2) {
            mOutputListener.printError(e2.getStackTrace());
        }

        // Sample 2 - Merge several PDF documents into one
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 2 - Merge several PDF documents into one...");
        try (PDFDoc new_doc = new PDFDoc()) {
            new_doc.initSecurityHandler();

            int page_num = 15;
            for (int i = 1; i <= page_num; ++i) {
                mOutputListener.println("Opening newsletter_split_page_" + i + ".pdf");
                String fname = "newsletter_split_page_" + i + ".pdf";
                try (PDFDoc in_doc = new PDFDoc(Utils.createExternalFile(fname, mFileList).getAbsolutePath())) {
                    new_doc.insertPages(i, in_doc, 1, in_doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
                }
            }
            new_doc.save(Utils.createExternalFile("newsletter_merge_pages.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
            mOutputListener.println("Done. Result saved in newsletter_merge_pages.pdf");
        } catch (Exception e2) {
            mOutputListener.printError(e2.getStackTrace());
        }

        // Sample 3 - Delete every second page
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 3 - Delete every second page...");
        mOutputListener.println("Opening the input pdf...");
        try (PDFDoc in_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
            in_doc.initSecurityHandler();

            int page_num = in_doc.getPageCount();
            while (page_num >= 1) {
                PageIterator itr = in_doc.getPageIterator(page_num);
                in_doc.pageRemove(itr);
                page_num -= 2;
            }

            in_doc.save(Utils.createExternalFile("newsletter_page_remove.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
            mOutputListener.println("Done. Result saved in newsletter_page_remove.pdf...");

        } catch (Exception e2) {
            mOutputListener.printError(e2.getStackTrace());
        }

        // Sample 4 - Inserts a page from one document at different
        // locations within another document
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 4 - Insert a page at different locations...");
        mOutputListener.println("Opening the input pdf...");
        try (PDFDoc in1_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
            in1_doc.initSecurityHandler();
            try (PDFDoc in2_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath()))) {
                in2_doc.initSecurityHandler();

                PageIterator src_page_itr = in2_doc.getPageIterator();
                Page src_page = src_page_itr.next();
                PageIterator dst_page_itr = in1_doc.getPageIterator();
                int page_num = 1;
                while (dst_page_itr.hasNext()) {
                  if (page_num++ % 3 == 0) {
                    in1_doc.pageInsert(dst_page_itr, src_page);
                }
                dst_page_itr.next();
               }

               in1_doc.save(Utils.createExternalFile("newsletter_page_insert.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
               mOutputListener.println("Done. Result saved in newsletter_page_insert.pdf...");
            }
        } catch (Exception e) {
              mOutputListener.printError(e.getStackTrace());
        }

        // Sample 5 - Replicate pages within a single document
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 5 - Replicate pages within a single document...");
        mOutputListener.println("Opening the input pdf...");
        try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
            doc.initSecurityHandler();

            // Replicate the cover page three times (copy page #1 and place it before the
            // seventh page in the document page sequence)
            Page cover = doc.getPage(1);
            PageIterator p7 = doc.getPageIterator(7);
            doc.pageInsert(p7, cover);
            doc.pageInsert(p7, cover);
            doc.pageInsert(p7, cover);

            // Replicate the cover page two more times by placing it before and after
            // existing pages.
            doc.pagePushFront(cover);
            doc.pagePushBack(cover);

            doc.save(Utils.createExternalFile("newsletter_page_clone.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
            mOutputListener.println("Done. Result saved in newsletter_page_clone.pdf...");
            
        } catch (Exception e2) {
            mOutputListener.printError(e2.getStackTrace());
        }

        // Sample 6 - Use ImportPages() in order to copy multiple pages at once
        // in order to preserve shared resources between pages (e.g. images, fonts,
        // colorspaces, etc.)
        mOutputListener.println("_______________________________________________");
        mOutputListener.println("Sample 6 - Preserving shared resources using ImportPages...");
        mOutputListener.println("Opening the input pdf...");
        try (PDFDoc in_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
            in_doc.initSecurityHandler();

            try (PDFDoc new_doc = new PDFDoc()) {

                Page[] copy_pages = new Page[in_doc.getPageCount()];
                int j = 0;
                for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
                    copy_pages[j] = itr.next();
                }

                Page[] imported_pages = new_doc.importPages(copy_pages);
                for (int i = 0; i < imported_pages.length; ++i) {
                    new_doc.pagePushFront(imported_pages[i]); // Order pages in reverse order.
                    // Use pushBackPage() if you would like to preserve the same order.
                }

                new_doc.save(Utils.createExternalFile("newsletter_import_pages.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);

                mOutputListener.println("Done. Result saved in newsletter_import_pages.pdf...");
                mOutputListener.println();
                mOutputListener.println("Note that the output file size is less than half the size");
                mOutputListener.println("of the file produced using individual page copy operations");
                mOutputListener.println("between two documents");
            }
        } catch (Exception e1) {
            mOutputListener.printError(e1.getStackTrace());
        }

		for (String file : mFileList) {
			addToFileList(file);
		}
		printFooter(outputListener);
	}

}
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

package com.pdftron.android.pdfnetsdksamples.samples

import com.pdftron.android.pdfnetsdksamples.OutputListener
import com.pdftron.android.pdfnetsdksamples.PDFNetSample
import com.pdftron.android.pdfnetsdksamples.R
import com.pdftron.android.pdfnetsdksamples.util.Utils
import com.pdftron.pdf.PDFDoc
import com.pdftron.pdf.Page
import com.pdftron.sdf.SDFDoc
import java.util.*

class PDFPageTest : PDFNetSample() {
    init {
        setTitle(R.string.sample_pdfpage_title)
        setDescription(R.string.sample_pdfpage_description)
    }

    override fun run(outputListener: OutputListener?) {
        super.run(outputListener)
        mOutputListener = outputListener
        mFileList.clear()
        printHeader(outputListener!!)

        // Sample 1 - Split a PDF document into multiple pages
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 1 - Split a PDF document into multiple pages...")
            mOutputListener!!.println("Opening the input pdf...")
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
                in_doc.initSecurityHandler()

                val page_num = in_doc.pageCount
                for (i in 1..page_num) {
                    PDFDoc().use { new_doc ->
                        new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null)
                        val fname = "newsletter_split_page_$i.pdf"
                        new_doc.save(Utils.createExternalFile(fname, PDFPageTest.mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
                        mOutputListener!!.println("Done. Result saved in newsletter_split_page_$i.pdf")
                    }
                }
            }
        } catch (e2: Exception) {
            mOutputListener!!.printError(e2.stackTrace)
        }

        // Sample 2 - Merge several PDF documents into one
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 2 - Merge several PDF documents into one...")
            PDFDoc().use { new_doc ->
                new_doc.initSecurityHandler()

                val page_num = 15
                for (i in 1..page_num) {
                    mOutputListener!!.println("Opening newsletter_split_page_$i.pdf")
                    val fname = "newsletter_split_page_$i.pdf"
                    PDFDoc(Utils.createExternalFile(fname, PDFPageTest.mFileList).absolutePath).use { in_doc ->
                        new_doc.insertPages(i, in_doc, 1, in_doc.pageCount, PDFDoc.InsertBookmarkMode.NONE, null)
                    }
                }
                new_doc.save(Utils.createExternalFile("newsletter_merge_pages.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
                mOutputListener!!.println("Done. Result saved in newsletter_merge_pages.pdf")
            }
        } catch (e2: Exception) {
            mOutputListener!!.printError(e2.stackTrace)
        }

        // Sample 3 - Delete every second page
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 3 - Delete every second page...")
            mOutputListener!!.println("Opening the input pdf...")
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
                in_doc.initSecurityHandler()

                var page_num = in_doc.pageCount
                while (page_num >= 1) {
                    val itr = in_doc.getPageIterator(page_num)
                    in_doc.pageRemove(itr)
                    page_num -= 2
                }

                in_doc.save(Utils.createExternalFile("newsletter_page_remove.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
                mOutputListener!!.println("Done. Result saved in newsletter_page_remove.pdf...")

            }
        } catch (e2: Exception) {
            mOutputListener!!.printError(e2.stackTrace)
        }

        // Sample 4 - Inserts a page from one document at different
        // locations within another document
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 4 - Insert a page at different locations...")
            mOutputListener!!.println("Opening the input pdf...")

            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in1_doc ->
                in1_doc.initSecurityHandler()

                PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "fish.pdf")!!.absolutePath).use { in2_doc ->
                    in2_doc.initSecurityHandler()

                    val src_page_itr = in2_doc.pageIterator
                    val src_page = src_page_itr.next()
                    val dst_page_itr = in1_doc.pageIterator
                    var page_num = 1
                    while (dst_page_itr.hasNext()) {
                        if (page_num++ % 3 == 0) {
                            in1_doc.pageInsert(dst_page_itr, src_page)
                        }
                        dst_page_itr.next()
                    }

                    in1_doc.save(Utils.createExternalFile("newsletter_page_insert.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
                    mOutputListener!!.println("Done. Result saved in newsletter_page_insert.pdf...")
                }
            }
        } catch (e2: Exception) {
            mOutputListener!!.printError(e2.stackTrace)
        }

        // Sample 5 - Replicate pages within a single document
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 5 - Replicate pages within a single document...")
            mOutputListener!!.println("Opening the input pdf...")
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { doc ->
                doc.initSecurityHandler()

                // Replicate the cover page three times (copy page #1 and place it before the
                // seventh page in the document page sequence)
                val cover = doc.getPage(1)
                val p7 = doc.getPageIterator(7)
                doc.pageInsert(p7, cover)
                doc.pageInsert(p7, cover)
                doc.pageInsert(p7, cover)

                // Replicate the cover page two more times by placing it before and after
                // existing pages.
                doc.pagePushFront(cover)
                doc.pagePushBack(cover)

                doc.save(Utils.createExternalFile("newsletter_page_clone.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
                mOutputListener!!.println("Done. Result saved in newsletter_page_clone.pdf...")
            }
        } catch (e2: Exception) {
            mOutputListener!!.printError(e2.stackTrace)
        }

        // Sample 6 - Use ImportPages() in order to copy multiple pages at once
        // in order to preserve shared resources between pages (e.g. images, fonts,
        // colorspaces, etc.)
        try {
            mOutputListener!!.println("_______________________________________________")
            mOutputListener!!.println("Sample 6 - Preserving shared resources using ImportPages...")
            mOutputListener!!.println("Opening the input pdf...")
            PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
                in_doc.initSecurityHandler()

                PDFDoc().use { new_doc ->
                    val copy_pages = arrayOfNulls<Page>(in_doc.pageCount)
                    var j = 0
                    val itr = in_doc.pageIterator
                    while (itr.hasNext()) {
                        copy_pages[j] = itr.next()
                        j++
                    }

                    val imported_pages = new_doc.importPages(copy_pages)
                    for (i in imported_pages.indices) {
                        new_doc.pagePushFront(imported_pages[i]) // Order pages in reverse order.
                        // Use pushBackPage() if you would like to preserve the same order.
                    }

                    new_doc.save(Utils.createExternalFile("newsletter_import_pages.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)

                    mOutputListener!!.println("Done. Result saved in newsletter_import_pages.pdf...")
                    mOutputListener!!.println()
                    mOutputListener!!.println("Note that the output file size is less than half the size")
                    mOutputListener!!.println("of the file produced using individual page copy operations")
                    mOutputListener!!.println("between two documents")
                }
            }
        } catch (e1: Exception) {
            mOutputListener!!.printError(e1.stackTrace)
        }

        for (file in mFileList) {
            addToFileList(file)
        }
        printFooter(outputListener)
    }

    companion object {

        private var mOutputListener: OutputListener? = null

        private val mFileList = ArrayList<String>()
    }

}
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/android/get-started/samples/pdfpagetest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
