SandboxedPdfLoader


class SandboxedPdfLoader : PdfLoader


A PdfLoader implementation that opens PDF documents through a sandboxed Android service.

This class establishes a connection with the PdfDocumentServiceImpl bound service and attempts to load a PDF document using the PdfRenderer. If successful, it returns a SandboxedPdfDocument instance, which provides a remote interface for interacting with the document.

The loading process involves:

  1. Establishing a connection with the service using a PdfServiceConnection.

  2. Opening a ParcelFileDescriptor for the PDF document.

  3. Passing the file descriptor to the service for loading and rendering.

  4. Creating a SandboxedPdfDocument instance upon successful loading.

Summary

Public companion functions

PdfSandboxHandle

Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the SandboxedPdfLoader or PdfDocument.

Public constructors

SandboxedPdfLoader(context: Context, coroutineContext: CoroutineContext)

Creates a new SandboxedPdfLoader instance.

Public functions

open suspend PdfDocument
openDocument(uri: Uri, password: String?)

Asynchronously opens a PDF document from the specified Uri.

Public companion functions

startInitialization

Added in 1.0.0-alpha10
fun startInitialization(context: Context): PdfSandboxHandle

Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the SandboxedPdfLoader or PdfDocument.

The returned PdfSandboxHandle represents a session and must be closed by the caller when no longer needed.

Calling this method is optional. Any document operation via SandboxedPdfLoader and PdfDocument will initialize the resources internally on demand, but may experience increased startup time.

Parameters
context: Context

A Context of component to be associated with pdf session.

Returns
PdfSandboxHandle

A PdfSandboxHandle representing an active pdf session.

See also
PdfSandboxHandle

Public constructors

SandboxedPdfLoader

Added in 1.0.0-alpha10
SandboxedPdfLoader(
    context: Context,
    coroutineContext: CoroutineContext = EmptyCoroutineContext
)

Creates a new SandboxedPdfLoader instance.

Parameters
context: Context

The Context required for accessing system services.

coroutineContext: CoroutineContext = EmptyCoroutineContext

The CoroutineContext used for asynchronous operations. This context is resolved internally to ensure an appropriate ContinuationInterceptor is present.

  • If the provided coroutineContext already contains a ContinuationInterceptor, that interceptor will be used.

  • If the provided coroutineContext does not contain a ContinuationInterceptor, Dispatchers.IO will be automatically added to it to handle I/O-bound tasks such as opening file descriptors and interacting with the PDF service.

  • Providing a Job in this coroutineContext is an error.

Public functions

openDocument

open suspend fun openDocument(uri: Uri, password: String?): PdfDocument

Asynchronously opens a PDF document from the specified Uri.

Parameters
uri: Uri

The URI of the PDF document to open.

password: String?

(Optional) The password to unlock the document if it is encrypted.

Returns
PdfDocument

The opened PdfDocument.

Throws
androidx.pdf.PdfPasswordException

If the provided password is incorrect.

java.io.IOException

If an error occurs while opening the document.