# Customizing the PDF editing toolbar on Android

Document Editor offers you and your users a whole host of document editing features, including creation of new pages, page duplication, reordering, rotation or deletion, and the ability to create a new document from selected pages.

The editor is integrated with the [`PdfActivity`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-activity/index.html) as a drop-in replacement for the original `PdfThumbnailGrid`. To switch into document editing mode, simply press the thumbnail grid menu option and the document page grid will be displayed. You can begin editing by pressing the floating action button, or alternatively, by long pressing any page.

If document editing isn’t enabled in your license, the standard [`PdfThumbnailGrid`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-thumbnail-grid/index.html) will be used.

## Document editing features

Once in editing mode, the toolbar will show all editing options available. Furthermore, the floating action button will allow the creation of new pages.

The following is a list of currently supported editing actions:

- Reordering pages via drag and drop

- Page rotation 90 degrees clockwise

- Duplication of pages

- Deletion of existing pages

- Multi-selection of pages

- Full undo/redo support and discarding of all changes

- Saving changes back to the original document

- Saving changes to a given destination

- Exporting selected pages into a new document

- Importing a new document selected from the device storage

When saving changes to a given destination or exporting selected pages into a new document, Document Editor will open the Storage Access Framework file picker by default in order to select a destination file. To modify the default behavior with a custom file picker, use [`com.pspdfkit.ui.PdfThumbnailGrid#setFilePicker(FilePicker)`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-thumbnail-grid/file-picker.html).

Page rotation, duplication, and deletion are also possible when selecting multiple pages.

### KOTLIN

```kotlin

val documentEditor = PdfDocumentEditorFactory.createForDocument(document)

```

### JAVA

```java

PdfDocumentEditor documentEditor = PdfDocumentEditorFactory.createForDocument(document)

```

### Transactions

Transactions allow for managing batches of operations as single units. A transaction is a change that occurred because of an operation performed by the Document Editor that can be undone/redone. The kind of operation performed is specified in [`EditingOperation`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.undo/-editing-operation/index.html) and may be one of the following: `REMOVE`, `MOVE`, `INSERT`, or `ROTATE`.

- [`PdfDocumentEditor#beginTransactions()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor/-pdf-document-editor/begin-transaction.html) begins a transaction.

- [`PdfDocumentEditor#isTransactionActive()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor/-pdf-document-editor/is-transaction-active.html) checks if there is currently an active transaction.

- [`PdfDocumentEditor#discardTransaction()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor/-pdf-document-editor/discard-transaction.html) discards the current batch of document operations.

- [`PdfDocumentEditor#commitTransaction()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor/-pdf-document-editor/commit-transaction.html) ends a batch of Document Editor operations and commits them immediately.

### Creating new pages

While in editing mode, you can press the floating action button to add a new page. By default, tapping this will display the [`NewPageDialog`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-new-page-dialog/index.html), allowing you to configure the new page (various page patterns, background colors, page sizes, and landscape or portrait orientation).

#### New page factories

Document Editor will add [`NewPage`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.processor/-new-page/index.html) instances it receives from the set [`NewPageFactory`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-new-page-factory/index.html). You can set a factory by calling [`PdfThumbnailGrid#setNewPageFactory`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-thumbnail-grid/set-new-page-factory.html). Here are the existing factories:

- [`DialogNewPageFactory`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-dialog-new-page-factory/index.html) — This is the default factory. It uses the [`NewPageDialog`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-new-page-dialog/index.html) to configure and create a [`NewPage`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.processor/-new-page/index.html) that will be added to the document.

- [`ValueNewPageFactory`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-dialog-new-page-factory/index.html) — This factory will immediately return a [`NewPage`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.processor/-new-page/index.html) instance that is set via the factory’s constructor method.

### KOTLIN

```kotlin

val factory = ValueNewPageFactory(NewPage.emptyPage(NewPage.PAGE_SIZE_A4).build())
thumbnailGrid.setNewPageFactory(factory)

```

### JAVA

```java

final NewPageFactory factory = new ValueNewPageFactory(NewPage.emptyPage(NewPage.PAGE_SIZE_A4).build());
thumbnailGrid.setNewPageFactory(factory);

```

#### Custom page templates

To define the list of page templates that should be offered in the [`NewPageDialog`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-new-page-dialog/index.html), pass over a list of [`PageTemplate`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-page-template/index.html) instances when calling [`NewPageDialog.show()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-new-page-dialog/show.html). Inside Document Editor (i.e. when using the [`PdfThumbnailGrid`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui/-pdf-thumbnail-grid/index.html)), you can pass over the list of page templates to the used [`DialogNewPageFactory`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.document.editor.page/-dialog-new-page-factory/index.html):

### KOTLIN

```kotlin

// Load a page template from a PDF file.
val pageIndex = 8
val template = PageTemplate(
    PdfDocumentLoader.openDocument(this, DocumentSource(AssetDataProvider("Guide-v4.pdf"))),
    pageIndex,
    getString(R.string.page_template_title),
    ContextCompat.getDrawable(this, R.drawable.page_template_preview)
))
// Set up the Document Editor to show the desired list of page templates.
thumbnailGrid.setNewPageFactory(DialogNewPageFactory(
    supportFragmentManager,
    null,
    listOf(template)
))

```

### JAVA

```java

// Load a page template from a PDF file.
final int pageIndex = 8;
final PageTemplate template = new PageTemplate(
    PdfDocumentLoader.openDocument(this, new DocumentSource(new AssetDataProvider("Guide-v4.pdf"))),
    pageIndex,
    getString(R.string.page_template_title),
    ContextCompat.getDrawable(this, R.drawable.page_template_preview)
));
// Set up the Document Editor to show the desired list of page templates.
thumbnailGrid.setNewPageFactory(new DialogNewPageFactory(
    getSupportFragmentManager(),
    null,
    pageTemplates
));

```

If you want to see a runnable example for custom page templates, check out `CustomPageTemplatesExample` in our Catalog app.

### Document Editor toolbar

By default, Document Editor uses the [`DocumentEditingToolbar`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-document-editing-toolbar/index.html) to provide the primary editing actions. You can replace this toolbar with your own UI implementation by registering an [`OnEditingModeChangeListener`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.manager/-document-editing-manager/-on-document-editing-mode-change-listener/index.html) using [`addOnDocumentEditingModeChangeListener()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.manager/-document-editing-manager/add-on-document-editing-mode-change-listener.html) on the public document editing manager exposed by your thumbnail grid:

### KOTLIN

```kotlin

thumbnailGrid.getDocumentEditingManager().addOnDocumentEditingModeChangeListener(myListener)

```

### JAVA

```java

thumbnailGrid.getDocumentEditingManager().addOnDocumentEditingModeChangeListener(myListener);

```

`PdfThumbnailGrid.getDocumentEditingManager()` is the supported public entry point for receiving a `DocumentEditingController` when Document Editor enters or exits editing mode.

If you prefer, `PdfThumbnailGrid.addOnDocumentEditingModeChangeListener()` and `PdfThumbnailGrid.addOnDocumentEditingPageSelectionChangeListener()` are available as convenience wrappers around that manager.

Whenever your listener’s [`onEnterDocumentEditingMode()`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.special_mode.manager/-document-editing-manager/-on-document-editing-mode-change-listener/on-enter-document-editing-mode.html) is called, your listener is expected to show the custom UI (or the default toolbar). In the following example, the listener will create the default [`DocumentEditingToolbar`](https://www.nutrient.io/api/android/nutrient/com.pspdfkit.ui.toolbar/-document-editing-toolbar/index.html) and bind it to the Document Editor controller:

### KOTLIN

```kotlin

override fun onEnterDocumentEditingMode(controller: DocumentEditingController) {
    val documentEditingToolbar = DocumentEditingToolbar(context)
    documentEditingToolbar.bindController(controller)

    // This is used to display the toolbar within a `ToolbarCoordinatorLayout`.
    toolbarCoordinatorLayout.displayContextualToolbar(documentEditingToolbar, true)
}

```

### JAVA

```java

@Override
public void onEnterDocumentEditingMode(@NonNull DocumentEditingController controller) {
    final DocumentEditingToolbar documentEditingToolbar = new DocumentEditingToolbar(context);
    documentEditingToolbar.bindController(controller);

    // This is used to display the toolbar within a `ToolbarCoordinatorLayout`.
    toolbarCoordinatorLayout.displayContextualToolbar(documentEditingToolbar, true);
}

```
---

## Related pages

- [Annotation editing on Android](/guides/android/user-interface/contextual-toolbars/annotation-editing.md)
- [Effortlessly customize text selection in Android](/guides/android/user-interface/contextual-toolbars/text-selection.md)
- [Customizing the annotation creation toolbar in our Android PDF viewer](/guides/android/annotations/custom-annotation-editing-controls.md)
- [Customizing toolbars in our Android PDF viewer](/guides/android/customizing-the-interface/customizing-the-toolbar.md)
- [Using contextual toolbars within PdfFragment](/guides/android/customizing-the-interface/using-toolbars-within-fragment.md)

