> 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/page-manipulation/create.md).

# Create a page

Learn how to create document pages in Android using UI components or programmatically with an API guide. Add various page types, sizes, and colors easily. Create new PDFs or add pages to existing docu

There are two options to create a page. First is using a UI component that can create a new page or a new document. Second is an API guide to programmatically create a page.

{% tabs %}
{% tab title="UI component" %}

## Create document pages in Android

The [`AddPageDialogFragment`](https://sdk.apryse.com/api/android/javadoc/reference/com/pdftron/pdf/controls/AddPageDialogFragment.html) allows users are able to add new pages to an existing document or create a completely new document. The new pages created can have various types, sizes, and colors.

![](https://226546913-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0QItsdFBmuuL9ezHimHy%2Fuploads%2Fgit-blob-de25653365e8f896fae4fd5a9d3d3a9465ad9f1a%2F8388d04f51e54f679e6e2075355c3bea138e8a1a-2300x2300.png?alt=media)

Add page dialog: dialog for creating new document (left), dialog for adding pages to an existing document (right).

## Page properties

The following properties are available for creating or adding pages:

* `PageType` The following types of page are supported in this dialog:
  * `Blank`: a page with nothing on it.
  * `Lined`: a page with horizontal lines on it.
  * `Grid`: a page with a superimposed grid on it.
  * `Graph`: a page with Cartesian axes on it.
  * `Music`: a page set up with modern staff notation for notating music.
* `PageSize` The following page sizes are supported in this dialog: `Custom`, `Letter`, `Legal`, `A4`, `A3`, `Ledger`.

{% hint style="info" %}
The `Custom` option is only available when adding new pages to an existing PDF document, not when a new document is being created. The page size of `Custom` option is specified as here: [add pages to an existing document](https://docs.apryse.com)
{% endhint %}

* `PageColor` The page background can be set to the following colors: `White`, `Yellow`, and `Blueprint`.

## Create a new PDF document

To create a new PDF document, call `newInstance()` and override the `OnCreateNewDocumentListener` interface. The implementation of `onCreateNewDocument(PDFDoc, String)` should create a new file with the given title normalized to a ".pdf" extension.

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

```java
void addPagesToNewDocument(FragmentManager fragmentManager, final String folder) {
    AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance();
    addPageDialogFragment.setOnCreateNewDocumentListener(new AddPageDialogFragment.OnCreateNewDocumentListener() {
        @Override
        public void onCreateNewDocument(PDFDoc doc, String title) {
            if (doc == null || title == null) {
                return;
            }
            if (!FilenameUtils.isExtension(title, "pdf")) {
                title = title + ".pdf";
            }
            File documentFile = new File(folder, title);
            try {
                SDFDoc.SaveMode saveModes[] = new SDFDoc.SaveMode[]{SDFDoc.SaveMode.REMOVE_UNUSED};
                doc.save(documentFile.getAbsolutePath(), saveModes, null);
            } catch (PDFNetException e) {
                e.printStackTrace();
            } finally {
                try {
                    doc.close();
                } catch (PDFNetException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    addPageDialogFragment.show(fragmentManager, "add_page_dialog");
}
```

{% endcode %}
{% endtab %}

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

```kotlin
internal fun addPagesToNewDocument(fragmentManager: FragmentManager, folder: String) {
    val addPageDialogFragment = AddPageDialogFragment.newInstance()
    addPageDialogFragment.setOnCreateNewDocumentListener(AddPageDialogFragment.OnCreateNewDocumentListener { doc, title ->
        var title = title
        if (doc == null || title == null) {
            return@OnCreateNewDocumentListener
        }
        if (!FilenameUtils.isExtension(title, "pdf")) {
            title = "$title.pdf"
        }
        val documentFile = File(folder, title)
        try {
            val saveModes = arrayOf(SDFDoc.SaveMode.REMOVE_UNUSED)
            doc.save(documentFile.absolutePath, saveModes, null)
        } catch (e: PDFNetException) {
            e.printStackTrace()
        } finally {
            try {
                doc.close()
            } catch (e: PDFNetException) {
                e.printStackTrace()
            }

        }
    })
    addPageDialogFragment.show(fragmentManager, "add_page_dialog")
}
```

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

## Add pages to an existing document

To add pages to an existing PDF, create a new instance of the add page dialog fragment using `newInstance(double, double)` and provide the valid page width and page height arguments. These arguments will be used only if the user selects the `Custom` option in the Page Size dropdown. You also must implement the `OnAddNewPagesListener` interface and override `onAddNewPages(Page[])`, in which the implementation should add the provided pages to the document.

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

```java
void addPagesToCurrentDocument(final PDFViewCtrl pdfViewCtrl, final FragmentManager fragmentManager) throws Exception {
    final WeakReference<PDFViewCtrl> pdfViewCtrlRef = new WeakReference<>(pdfViewCtrl);
    pdfViewCtrl.docLockRead(new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            // enable user to add new pages with the same size as the last page of the current document
            Page lastPage = pdfViewCtrl.getDoc().getPage(pdfViewCtrl.getDoc().getPageCount());
            AddPageDialogFragment addPageDialogFragment = AddPageDialogFragment.newInstance(lastPage.getPageWidth(), lastPage.getPageHeight());
            addPageDialogFragment.setOnAddNewPagesListener(new AddPageDialogFragment.OnAddNewPagesListener() {
                @Override
                public void onAddNewPages(final Page[] pages) {
                    final PDFViewCtrl pdfViewCtrl = pdfViewCtrlRef.get();
                    if (pages == null || pdfViewCtrl == null) {
                        return;
                    }
                    final PDFDoc doc = pdfViewCtrl.getDoc();
                    if (doc == null) {
                        return;
                    }

                    try {
                        pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
                            @Override
                            public void run() throws Exception {
                                List<Integer> pageList = new ArrayList<>();
                                for (int i = 1, cnt = pages.length; i <= cnt; i++) {
                                    int newPageNum = pdfViewCtrl.getCurrentPage() + i;
                                    pageList.add(newPageNum);
                                    doc.pageInsert(doc.getPageIterator(newPageNum), pages[i - 1]);
                                }

                                // To support undo/redo when a tool manager is attached to the PDFViewCtrl
                                ToolManager toolManager = (ToolManager) pdfViewCtrl.getToolManager();
                                if (toolManager != null) {
                                    toolManager.raisePagesAdded(pageList);
                                }
                                pdfViewCtrl.updatePageLayout();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            addPageDialogFragment.show(fragmentManager, "add_page_dialog");
        }
    });
}
```

{% endcode %}
{% endtab %}

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

```kotlin
fun addPagesToCurrentDocument(pdfViewCtrl: PDFViewCtrl, fragmentManager: FragmentManager?) {
    val pdfViewCtrlRef = WeakReference(pdfViewCtrl)
    pdfViewCtrl.docLockRead {
        // enable user to add new pages with the same size as the last page of the current document
        val lastPage = pdfViewCtrl.doc.getPage(pdfViewCtrl.doc.pageCount)
        val addPageDialogFragment = AddPageDialogFragment.newInstance(lastPage.pageWidth, lastPage.pageHeight)
        addPageDialogFragment.setOnAddNewPagesListener(OnAddNewPagesListener { pages ->
            val pdfViewCtrl = pdfViewCtrlRef.get()
            if (pages == null || pdfViewCtrl == null) {
                return@OnAddNewPagesListener
            }
            val doc = pdfViewCtrl.doc ?: return@OnAddNewPagesListener
            try {
                pdfViewCtrl.docLock(true, LockRunnable {
                    val pageList: MutableList<Int> = ArrayList()
                    var i = 1
                    val cnt = pages.size
                    while (i <= cnt) {
                        val newPageNum = pdfViewCtrl.currentPage + i
                        pageList.add(newPageNum)
                        doc.pageInsert(doc.getPageIterator(newPageNum), pages[i - 1])
                        i++
                    }
                    // To support undo/redo when a tool manager is attached to the PDFViewCtrl
                    val toolManager = pdfViewCtrl.toolManager as ToolManager
                    toolManager?.raisePagesAdded(pageList)
                    pdfViewCtrl.updatePageLayout()
                })
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        })
        addPageDialogFragment.show(fragmentManager!!, "add_page_dialog")
    }
}
```

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

{% tab title="API guide" %}

## API to programmatically create pages in Android

To create a new 8.5x11 blank page and add it at the end of document's page sequence.

Note that, after the page is created, it does not yet belong to a document's page sequence. The page needs to be placed within the page sequence in order to become "visible". `PagePushBack()` inserts page x into the position of the document's last page.

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

```java
PDFDoc doc = new PDFDoc(filename);

// create a new page
Page x = doc.pageCreate();
doc.pagePushBack(x);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc(filename)

// create a new page
val x = doc.pageCreate();
doc.pagePushBack(x);
```

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

## About creating a new blank page

To create a new page, use the `PDFDoc.PageCreate(media_box)` method. `PageCreate()` takes an optional Rect argument that can be used to specify page size. This Rect is called a media box.

A media box is a rectangle, expressed in default user space units, defining the boundaries of the physical medium on which the page is intended to be displayed or printed. A user space unit is 1/72 of an inch. If media\_box is unspecified, the default dimensions of the page are 8.5 x 11 inches (or 8.5\*72, 11\*72 units).
{% 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/page-manipulation/create.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.
