Merge PDF in Kotlin for Android
Merge multiple PDF documents using the document processor. Get additional resources by visiting our guide on merging multiple PDF files in Android.
/* * Copyright © 2020-2025 PSPDFKit GmbH. All rights reserved. * * The PSPDFKit Sample applications are licensed with a modified BSD license. * Please see License for details. This notice may not be removed from this file. */
// We're temporarily suppressing the ProgressDialog being deprecated warning.// Issue: https://github.com/PSPDFKit/PSPDFKit/issues/32215@file:Suppress("DEPRECATION")
package com.pspdfkit.catalog.examples.kotlin
import android.app.ProgressDialogimport android.content.Contextimport android.net.Uriimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.DocumentSourceimport com.pspdfkit.document.PdfDocumentLoaderimport com.pspdfkit.document.processor.NewPageimport com.pspdfkit.document.processor.PdfProcessorimport com.pspdfkit.document.processor.PdfProcessorTaskimport com.pspdfkit.document.providers.AssetDataProviderimport com.pspdfkit.ui.PdfActivityimport io.reactivex.rxjava3.android.schedulers.AndroidSchedulersimport io.reactivex.rxjava3.core.Singleimport io.reactivex.rxjava3.disposables.Disposableimport io.reactivex.rxjava3.schedulers.Schedulersimport java.io.File
/** * This example shows how to merge multiple PDFs using the document processor. */class MergeDocumentsExample(context: Context) : SdkExample(context, R.string.mergeDocumentsExampleTitle, R.string.mergeDocumentsExampleDescription) {
/** Disposable for document merge operation of [com.pspdfkit.document.processor.PdfProcessor].*/ private var mergingDisposable: Disposable? = null
override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { val dialog = ProgressDialog.show(context, "Merging Documents", "Please wait", true, true) { mergingDisposable?.dispose() }
mergingDisposable = Single.fromCallable { // Open the documents we are going to merge. val documents = listOf("AnnualReport.pdf", "Scientific-Report.pdf", WELCOME_DOC) .asSequence() .map { PdfDocumentLoader.openDocument(context, DocumentSource(AssetDataProvider(it))) }
// Start with an empty document. val task = PdfProcessorTask.empty()
// Add all document pages. var totalPageCount = 0 for (document in documents) { for (i in 0 until document.pageCount) { // We use `totalPageCount` here to add the pages to the end. // But you are free to add them at any place in the document you'd like. task.addNewPage(NewPage.fromPage(document, i).build(), totalPageCount) totalPageCount++ } }
// Finally create the resulting document. val mergedDocumentsFile = File(context.getDir("documents", Context.MODE_PRIVATE), "merged-documents.pdf") PdfProcessor.processDocument(task, mergedDocumentsFile)
return@fromCallable Uri.fromFile(mergedDocumentsFile) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doFinally { dialog.cancel() } .subscribe { uri -> PdfActivity.showDocument(context, uri, configuration.build()) } }}
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.