Disable text selection in PDF using Java for Android
Use ApplicationPolicy to prevent the copying text. Get additional resources by visiting our guide on copying and pasting annotations 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. */
package com.pspdfkit.catalog.examples.kotlin
import android.content.Contextimport android.net.Uriimport com.pspdfkit.Nutrientimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.catalog.tasks.ExtractAssetTaskimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.configuration.policy.ApplicationPolicyimport com.pspdfkit.ui.PdfActivityIntentBuilder
/** * Example of how to customize application policy preventing copy/paste action. */class CustomApplicationPolicyExample(context: Context) : SdkExample(context, R.string.customApplicationPolicyExampleTitle, R.string.customApplicationPolicyExampleDescription) {
override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { // To customize the application policy we extend ApplicationPolicy. val customApplicationPolicy = CustomApplicationPolicy()
// Application policy needs to be set before documents are loaded. Nutrient.setApplicationPolicy(customApplicationPolicy)
// We use a custom utility class to extract the example document from the assets. ExtractAssetTask.extract(WELCOME_DOC, title, context) { documentFile -> // Open the example document in PdfActivity. val intent = PdfActivityIntentBuilder.fromUri(context, Uri.fromFile(documentFile)) .configuration(configuration.build()) .build() context.startActivity(intent) } }}
/** * Custom Application policy that disables text copy and paste. */class CustomApplicationPolicy : ApplicationPolicy() { override fun hasPermissionForEvent(event: PolicyEvent): Boolean { return when (event) { PolicyEvent.TEXT_COPY_PASTE -> // Disable text copy/paste policy. false else -> { // Enable remaining policies. true } } }}
This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.