Opening Multiple PDFs in Our Android Viewer

PSPDFKit for Android supports loading multiple documents into a single PdfActivity. This guide will outline how to launch the PdfActivity, as well as how to control which documents are displayed.

Launching PdfActivity

Depending on how many documents you want to display in the PdfActivity, there are different ways to do it.

Displaying One Document

If you’re just displaying a single document, the PdfActivity itself has a convenient method to do this:

// Shows the document found at the given URI.
PdfActivity.showDocument(context, uri, configuration)
// Shows the document found at the given URI.
PdfActivity.showDocument(context, uri, configuration);

Displaying Multiple Documents

If you need to display two or more documents in a PdfActivity, you’ll have to use the PdfActivityIntentBuilder:

// `PdfActivity` uses a document descriptor to identify each document that should be loaded.
// These will be displayed as tabs if the tabs UI is enabled in the configuration.
val intent = PdfActivityIntentBuilder.fromDocumentDescriptor(
        context,
        // Shows the document found at the given URI.
        DocumentDescriptor.fromUri(documentUri),
        // You can also serve the document via `DataProvider`.
        DocumentDescriptor.fromDataProvider(dataProvider),
        // Or you can show an image document.
        DocumentDescriptor.imageDocumentFromUri(documentUri))
    .configuration(configuration)
    // Show the second document (zero-based index) initially.
    .visibleDocument(1)
    .build()
context.startActivity(intent)
// `PdfActivity` uses a document descriptor to identify each document that should be loaded.
// These will be displayed as tabs if the tabs UI is enabled in the configuration.
Intent intent = PdfActivityIntentBuilder.fromDocumentDescriptor(
        context,
        // Shows the document found at the given URI.
        DocumentDescriptor.fromUri(documentUri),
        // You can also serve the document via `DataProvider`.
        DocumentDescriptor.fromDataProvider(dataProvider),
        // Or you can show an image document.
        DocumentDescriptor.imageDocumentFromUri(documentUri))
    .configuration(configuration)
    // Show the second document (zero-based index) initially.
    .visibleDocument(1)
    .build();
context.startActivity(intent);

Displaying PdfActivity without Documents

Finally, you can also start the PdfActivity without any document at all using emptyActivity():

// Shows the empty activity.
val intent = PdfActivityIntentBuilder.emptyActivity(context)
    .configuration(configuration)
    .build()
context.startActivity(intent)
// Shows the empty activity.
Intent intent = PdfActivityIntentBuilder.emptyActivity(context)
    .configuration(configuration)
    .build();
context.startActivity(intent);

Managing Displayed Documents

Once you start the PdfActivity, you can manage the displayed documents by using the DocumentCoordinator. To obtain an instance, use PdfActivity#getDocumentCoordinator(). To see an example of how this can be used, check out DocumentTabsExample in our Catalog app:

// Add a new document.
documentCoordinator.addDocument(descriptor)

// Display the newly added document.
documentCoordinator.setVisibleDocument(descriptor)

// Remove some other document.
documentCoordinator.removeDocument(otherDescriptor)
// Add a new document.
getDocumentCoordinator().addDocument(descriptor);

// Display the newly added document.
getDocumentCoordinator().setVisibleDocument(descriptor);

// Remove some other document.
getDocumentCoordinator.removeDocument(otherDescriptor);

UI for Handling Multiple Documents

By default, PSPDFKit provides a tabbed user interface to allow the user to switch between all the currently opened documents.

You can also build an entirely custom UI to switch between documents using the DocumentCoordinator as shown in DocumentSwitcherExample found in the Catalog app.