Split PDFs on Android

Nutrient Android SDK lets you split a document into multiple documents using the Processor API or the Document Editor API. You can create documents consisting of any combination of pages from the original document.

Splitting documents with PDF Processor

Nutrient’s PdfProcessor API enables you to split a PDF document into multiple documents.

PdfProcessor combined with a PdfProcessorTask can extract ranges of pages from one document and put them into another document. If you run this operation multiple times with different page indexes, you can effectively split a PDF into as many documents as you require.

In most cases, you’ll want to use PdfProcessor to create PDF documents on disk based on a current PdfDocument. This example code will split a single PDF document into two using the provided page indexes:

val originalDocument: PdfDocument = ...

// First, define the pages you want to split out of the original document.
val pagesToSplit: HashSet<Int> = HashSet(listOf(2, 3))

// Next, create a `PdfProcessor` task to perform the new document operation.
// Start with an empty document for the split pages.
val newDocumentTask = PdfProcessorTask.empty()
var index = 0
for (i in pagesToSplit) {
    if (i < originalDocument.pageCount) {
        newDocumentTask.addNewPage(NewPage.fromPage(originalDocument, i).build(), index++)
    }
}

// Create a new task to remove the pages from the original document.
val originalDocumentTask = PdfProcessorTask.fromDocument(originalDocument).removePages(pagesToSplit)

// Finally, define some files in which to save the split documents.
val fileA = File(context.getFilesDir(), "A.pdf")
val fileB = File(context.getFilesDir(), "B.pdf")
PdfProcessor.processDocument(originalDocumentTask, fileA)
PdfProcessor.processDocument(newDocumentTask, fileB)
PdfDocument originalDocument = ...

// First, define the pages you want to split out of the original document.
HashSet<Integer> pagesToSplit = new HashSet(Arrays.asList(2, 3));

// Next, create a `PdfProcessor` task to perform the new document operation.
// Start with an empty document for the split pages.
PdfProcessorTask newDocumentTask = PdfProcessorTask.empty();
int index = 0;
for (int i : pagesToSplit) {
    if (i < originalDocument.getPageCount()) {
        newDocumentTask.addNewPage(NewPage.fromPage(originalDocument, i).build(),
            index++);
    }
}

// Create a new task to remove the pages from the original document.
PdfProcessorTask originalDocumentTask =
    PdfProcessorTask.fromDocument(originalDocument).removePages(pagesToSplit);

// Finally, define some files in which to save the split documents.
File fileA = new File(context.getFilesDir(), "A.pdf");
File fileB = new File(context.getFilesDir(), "B.pdf");
PdfProcessor.processDocument(originalDocumentTask, fileA);
PdfProcessor.processDocument(newDocumentTask, fileB);

Splitting documents with Document Editor

Splitting pages is also available as part of the Document Editor API. The Document Editor 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.

The document editing operations work with ReactiveX objects, returning io.reactivex.Single observable instances containing a list of EditingChanges:

val document: PdfDocument = ...

// Create some files in which to save the split documents.
val outputFileA = File(filesDir, "A.pdf")
val outputFileB = File(filesDir, "B.pdf")
val outputA = FileOutputStream(outputFileA)
val outputB = FileOutputStream(outputFileB)

// Define the pages to split.
val pagesToSplit = HashSet(Arrays.asList(2, 3))

// Create a Document Editor.
val documentEditor = PdfDocumentEditorFactory.createForDocument(document)

// Use `exportPages` to export the pages to a new file.
val disposableA: Disposable = documentEditor.exportPages(context, outputA, pagesToSplit, null)
    .subscribe()

// Use `removePages` to remove the exported pages from the original document,
// and save the result to a new file.
val disposableB: Disposable = documentEditor.removePages(pagesToSplit)
    .flatMapCompletable { documentEditor.saveDocument(context, outputB, null) }
    .subscribe()
PdfDocument document = ...

// Create some files in which to save the split documents.
final File outputFileA = new File(getFilesDir(), "A.pdf");
final File outputFileB = new File(getFilesDir(), "B.pdf");
FileOutputStream outputA = new FileOutputStream(outputFileA);
FileOutputStream outputB = new FileOutputStream(outputFileB);

// Define the pages to split.
HashSet<Integer> pagesToSplit = new HashSet(Arrays.asList(2, 3));

// Create a Document Editor.
final PdfDocumentEditor documentEditor =
    PdfDocumentEditorFactory.createForDocument(document);

// Use `exportPages` to export the pages to a new file.
final Disposable disposableA = documentEditor.exportPages(context, outputA,
    pagesToSplit, null).subscribe();

// Use `removePages` to remove the exported pages from the original document,
// and save the result to a new file.
final Disposable disposableB = documentEditor.removePages(pagesToSplit)
    .flatMapCompletable(saved -> documentEditor.saveDocument(context, outputB, null))
    .subscribe();

Using the built-in UI

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