# SandboxedPdfLoader

Artifact: [androidx.pdf:pdf-document-service](https://developer.android.com/jetpack/androidx/releases/pdf) [View Source](https://cs.android.com/search?q=file:androidx/pdf/SandboxedPdfLoader.kt+class:androidx.pdf.SandboxedPdfLoader) Added in [1.0.0-beta01](https://developer.android.com/jetpack/androidx/releases/pdf#1.0.0-beta01)

*** ** * ** ***


```
public final class SandboxedPdfLoader implements PdfLoader
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/androidx/pdf/PdfLoader` implementation that opens PDF documents through a sandboxed Android service.

This class establishes a connection with the bound service and attempts to load a PDF document using the `https://developer.android.com/reference/android/graphics/pdf/PdfRenderer.html`. If successful, it returns a `https://developer.android.com/reference/androidx/pdf/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 .

2. Opening a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html` for the PDF document.

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

4. Creating a `https://developer.android.com/reference/androidx/pdf/SandboxedPdfDocument` instance upon successful loading.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader#SandboxedPdfLoader(android.content.Context,kotlin.coroutines.CoroutineContext)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext )` Creates a new `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader` instance. |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfDocument` | `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader#openDocument(android.net.Uri,kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/net/Uri.html uri, https://developer.android.com/reference/java/lang/String.html password)` Asynchronously opens a `https://developer.android.com/reference/androidx/pdf/PdfDocument` from the specified `https://developer.android.com/reference/android/net/Uri.html`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfDocument` | `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader#openDocument(android.net.Uri,android.os.ParcelFileDescriptor,kotlin.String,androidx.pdf.RenderParams)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/net/Uri.html uri, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/os/ParcelFileDescriptor.html fileDescriptor, https://developer.android.com/reference/java/lang/String.html password, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/RenderParams renderParams )` Asynchronously opens a `https://developer.android.com/reference/androidx/pdf/PdfDocument` from the specified `https://developer.android.com/reference/androidx/pdf/PdfLoader#openDocument(android.net.Uri,android.os.ParcelFileDescriptor,kotlin.String,androidx.pdf.RenderParams)`. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfSandboxHandle` | `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader#startInitialization(android.content.Context)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context)` Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader` or `https://developer.android.com/reference/androidx/pdf/PdfDocument`. |

## Public constructors

### SandboxedPdfLoader

Added in [1.0.0-beta01](https://developer.android.com/jetpack/androidx/releases/pdf#1.0.0-beta01)

```
public SandboxedPdfLoader(
    @NonNull Context context,
    @NonNull CoroutineContext coroutineContext
)
```

Creates a new `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader` instance.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The `https://developer.android.com/reference/android/content/Context.html` required for accessing system services. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used for asynchronous operations. This context is resolved internally to ensure an appropriate `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation-interceptor/index.html` is present. - If the provided `coroutineContext` already contains a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation-interceptor/index.html`, that interceptor will be used. - If the provided `coroutineContext` does not contain a `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-continuation-interceptor/index.html`, `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html` 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 `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html` in this `coroutineContext` is an error. |

## Public methods

### openDocument

```
public @NonNull PdfDocument openDocument(@NonNull Uri uri, String password)
```

Asynchronously opens a `https://developer.android.com/reference/androidx/pdf/PdfDocument` from the specified `https://developer.android.com/reference/android/net/Uri.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/net/Uri.html uri` | The URI of the PDF document to open. |
| `https://developer.android.com/reference/java/lang/String.html password` | (Optional) The password to unlock the document if it is encrypted. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfDocument` | The opened `https://developer.android.com/reference/androidx/pdf/PdfDocument`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/pdf/PdfPasswordException` | If the provided password is incorrect. |
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs while opening the document. |

### openDocument

```
public @NonNull PdfDocument openDocument(
    @NonNull Uri uri,
    @NonNull ParcelFileDescriptor fileDescriptor,
    String password,
    @NonNull RenderParams renderParams
)
```

Asynchronously opens a `https://developer.android.com/reference/androidx/pdf/PdfDocument` from the specified `https://developer.android.com/reference/androidx/pdf/PdfLoader#openDocument(android.net.Uri,android.os.ParcelFileDescriptor,kotlin.String,androidx.pdf.RenderParams)`. The file descriptor will become owned by the `https://developer.android.com/reference/androidx/pdf/PdfDocument`, and it should be closed using the document, unless an exception is thrown.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/net/Uri.html uri` | This is used only as a unique identifier for the `https://developer.android.com/reference/androidx/pdf/PdfDocument`. The source of truth for accessing file contents is in this case the `https://developer.android.com/reference/androidx/pdf/PdfLoader#openDocument(android.net.Uri,android.os.ParcelFileDescriptor,kotlin.String,androidx.pdf.RenderParams)`. If you don't have access to the `https://developer.android.com/reference/android/net/Uri.html` which produced the file descriptor, or the file descriptor was not produced by opening a URI, it's acceptable to provide a "fake" one here, so long as the value uniquely identifies the document. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/os/ParcelFileDescriptor.html fileDescriptor` | a `https://developer.android.com/reference/android/os/ParcelFileDescriptor.html` pointing at the PDF content to be opened. Must be seekable. |
| `https://developer.android.com/reference/java/lang/String.html password` | (Optional) The password to unlock the document if it is encrypted. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/RenderParams renderParams` | (Optional) Represents a set of parameters that will be used to render a page of the document. If not provided, the default `https://developer.android.com/reference/androidx/pdf/RenderParams` with `https://developer.android.com/reference/androidx/pdf/RenderParams#renderMode()` = `https://developer.android.com/reference/androidx/pdf/RenderParams#RENDER_MODE_FOR_DISPLAY()` will be used. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfDocument` | The opened `https://developer.android.com/reference/androidx/pdf/PdfDocument`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/pdf/PdfPasswordException` | If the provided password is incorrect. |
| `https://developer.android.com/reference/java/io/IOException.html` | If an error occurs while opening the document. |

### startInitialization

Added in [1.0.0-beta01](https://developer.android.com/jetpack/androidx/releases/pdf#1.0.0-beta01)

```
public static final @NonNull PdfSandboxHandle startInitialization(@NonNull Context context)
```

Prepares sandboxing PDF resources ahead of any document operations, to reduce latency during the interaction with the `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader` or `https://developer.android.com/reference/androidx/pdf/PdfDocument`.

The returned `https://developer.android.com/reference/androidx/pdf/PdfSandboxHandle` represents a session and must be closed by the caller when no longer needed.

Calling this method is optional. Any document operation via `https://developer.android.com/reference/androidx/pdf/SandboxedPdfLoader` and `https://developer.android.com/reference/androidx/pdf/PdfDocument` will initialize the resources internally on demand, but may experience increased startup time.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html` of component to be associated with pdf session. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/pdf/PdfSandboxHandle` | A `https://developer.android.com/reference/androidx/pdf/PdfSandboxHandle` representing an active pdf session. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/pdf/PdfSandboxHandle` |   |