Removing pages from a PDF on Android

Nutrient Android SDK lets you remove the pages of a document using the Processor API or the Document Editor API.

Using the Processor API

To remove pages via the PdfProcessor API, load your document and then configure the parameters for page removal on a PdfProcessorTask instance. The Processor API is a great choice if you want to build an automated document processing operation:

// Create a `PdfProcessorTask` in which to store the operation.
val task = PdfProcessorTask.empty()
// Specify page indexes 1 and 3 for removal.
val pagesToRemove = setOf(1, 3)
task.removePages(pagesToRemove)

// Process to an output document.
val outputFile = File(context.filesDir, "outputDocument.pdf")
PdfProcessor.processDocument(task, outputFile)
// Create a `PdfProcessorTask` in which to store the operation.
final PdfProcessorTask task = PdfProcessorTask.empty();
// Specify page indexes 1 and 3 for removal.
final HashSet<Integer> pagesToRemove = new HashSet<>();
pagesToRemove.add(1);
pagesToRemove.add(3);
task.removePages(pagesToRemove);

// Process to an output document.
final File outputFile = new File(context.getFilesDir(), "outputDocument.pdf");
PdfProcessor.processDocument(task, outputFile);

Using the Document Editor API

Page removal is also available as part of the Document Editor API. The Document Editor API is particularly useful if you want to build a general-purpose UI for manipulating pages, as it provides helpers that make building a document editing UI easy.

Here’s how you can use the Document Editor API to remove the current page of a displayed document:

// Instance method on a `PdfActivity` subclass.
fun removePages() {
    // Use the `PdfDocumentEditorFactory` to create a `PdfDocumentEditor`.
    val documentEditor = PdfDocumentEditorFactory.createForDocument(document)

    // Set up the output file location.
    val outputFile = File(filesDir, "outputDocument.pdf")

    // Specify the current page for removal.
    val pagesToRemove = setOf(pageIndex)

    // Make sure `disposable` is managed by the activity and destroyed correctly
    // when the activity is destroyed.
    val disposable: Disposable = documentEditor.removePages(pagesToRemove)
        .flatMapCompletable { documentEditor.saveDocument(context, outputFile.outputStream(), null) }
        // Use `subscribeOn` to put `saveDocument` on a background thread, as it can be slow.
        .subscribeOn(Schedulers.io())
        // Make sure the resulting document rendering goes back on the main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // You can display the saved document in a new activity with the following action on `subscribe`.
        .subscribe { showDocument(context, Uri.fromFile(outputFile), null) }
}
// Instance method on a `PdfActivity` subclass.
public void removePages() {
    // Use the `PdfDocumentEditorFactory` to create a `PdfDocumentEditor`.
    final PdfDocumentEditor documentEditor =
        PdfDocumentEditorFactory.createForDocument(document);

    // Set up the output file location.
    final File outputFile = new File(getFilesDir(), "outputDocument.pdf");

    // Specify the current page for removal.
    final HashSet<Integer> pagesToRemove = new HashSet<>(Arrays.asList(getPageIndex()));

    // Make sure `disposable` is managed by the activity and destroyed correctly
    // when the activity is destroyed.
    final Disposable disposable = documentEditor.removePages(pagesToRemove)
        .flatMapCompletable(saved -> documentEditor.saveDocument(context, new FileOutputStream(outputFile), null))
        // Use `subscribeOn` to put `saveDocument` on a background thread, as it can be slow.
        .subscribeOn(Schedulers.io())
        // Make sure the resulting document rendering goes back on the main thread.
        .observeOn(AndroidSchedulers.mainThread())
        // You can display the saved document in a new activity with the following action on `subscribe`.
        .subscribe( () -> { PdfActivity.showDocument(context,
            Uri.fromFile(outputFile), null);
        });
}

Using the built-in UI

Nutrient Android SDK comes with a prebuilt user interface for document editing that includes an option to remove pages. To learn more, check out the Document Editor UI overview.