Custom Outline Provider
This example shows how to use an outline provider to display a custom document outline.
/* * Copyright © 2019-2026 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.graphics.Colorimport android.graphics.Typefaceimport android.net.Uriimport com.pspdfkit.annotations.actions.GoToActionimport com.pspdfkit.annotations.actions.UriActionimport com.pspdfkit.catalog.Rimport com.pspdfkit.catalog.SdkExampleimport com.pspdfkit.catalog.tasks.ExtractAssetTaskimport com.pspdfkit.configuration.activity.PdfActivityConfigurationimport com.pspdfkit.document.OutlineElementimport com.pspdfkit.document.PdfDocumentimport com.pspdfkit.ui.PdfActivityimport com.pspdfkit.ui.PdfActivityIntentBuilderimport io.reactivex.rxjava3.core.Single
/** * This example shows how to display a custom outline in [com.pspdfkit.ui.PdfOutlineView]. */class OutlineProviderExample(context: Context) : SdkExample(context, R.string.outlineProviderExample, R.string.outlineProviderExampleDescription) {
override fun launchExample(context: Context, configuration: PdfActivityConfiguration.Builder) { // We use a custom utility class to extract the example document from the assets. ExtractAssetTask.extract(WELCOME_DOC, title, context) { documentFile -> val intent = PdfActivityIntentBuilder.fromUri(context, Uri.fromFile(documentFile)) .configuration(configuration.build()) .activityClass(OutlineProviderActivity::class) .build()
// Start the OutlineProviderActivity for the extracted document. context.startActivity(intent) } }}
/** * This subclass of [PdfActivity] sets a custom outline that replaces default document outline. */class OutlineProviderActivity : PdfActivity() {
override fun onDocumentLoaded(document: PdfDocument) { super.onDocumentLoaded(document)
// Set outline provider that returns our custom outline elements on Activity's outline view. pspdfKitViews.outlineView?.setDocumentOutlineProvider { Single.just(getCustomOutline(document)) } }
private fun getCustomOutline(document: PdfDocument): List<OutlineElement> { val outlineElements = mutableListOf<OutlineElement>()
// The simplest outline elements is just a title with no action. outlineElements.add(OutlineElement.Builder("No Action").build())
// Outline elements can have children - outline is a tree structure. outlineElements.add( OutlineElement.Builder("With Children") .setChildren( listOf( OutlineElement.Builder("Children 1").build(), OutlineElement.Builder("Children 2").build(), OutlineElement.Builder("Children 3").build() ) ) // This property controls whether this element will be expanded or not when shown. .setExpanded(true) .build() )
// Outline elements can have arbitrary actions. outlineElements.add( OutlineElement.Builder("Uri Action") // Outline text color and style are configurable. .setColor(Color.BLUE) .setStyle(Typeface.ITALIC) // Set action that opens Nutrient's website after clicking on the outline element. .setAction(UriAction("https://nutrient.io")) .build() )
// You can directly create outline elements using their constructor too. outlineElements.add(OutlineElement.Builder(document, "Go to page 3", 3).build()) // This is equivalent to the above outline element. outlineElements.add( OutlineElement.Builder("Go to page 3") .setAction(GoToAction(3)) // The page label is optional. If set, it will be displayed in addition to title. .setPageLabel(document.getPageLabel(3, false)) .build() )
return outlineElements }}This code sample is an example that illustrates how to use our SDK. Please adapt it to your specific use case.