Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/print/PrintDocumentAdapter#nestedclasses) \| [Constants](https://developer.android.com/reference/android/print/PrintDocumentAdapter#constants) \| [Ctors](https://developer.android.com/reference/android/print/PrintDocumentAdapter#pubctors) \| [Methods](https://developer.android.com/reference/android/print/PrintDocumentAdapter#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/print/PrintDocumentAdapter#inhmethods)

# PrintDocumentAdapter

*** ** * ** ***


`
public


abstract
class
PrintDocumentAdapter
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.print.PrintDocumentAdapter |

<br />

*** ** * ** ***

Base class that provides the content of a document to be printed.

### Lifecycle


- Initially, you will receive a call to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()`. This callback can be used to allocate resources.
- Next, you will get one or more calls to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)` to inform you that the print attributes (page size, density, etc) changed giving you an opportunity to layout the content to match the new constraints.
- After every call to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)`, you **may** get a call to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onWrite(android.print.PageRange[],%20android.os.ParcelFileDescriptor,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.WriteResultCallback)` asking you to write a PDF file with the content for specific pages.
- Finally, you will receive a call to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish()`. You can use this callback to release resources allocated in `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()`.


The `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()` callback is always the first call you will receive and
is useful for doing one time setup or resource allocation before printing. You
will not receive a subsequent call here.


The `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)` callback requires that you layout the content
based on the current `https://developer.android.com/reference/android/print/PrintAttributes`. The execution of this method is
not considered completed until you invoke one of the methods on the passed in
callback instance. Hence, you will not receive a subsequent call to any other
method of this class until the execution of this method is complete by invoking
one of the callback methods.


The `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onWrite(android.print.PageRange[],%20android.os.ParcelFileDescriptor,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.WriteResultCallback)` requires that you render and write the content of some
pages to the provided destination. The execution of this method is not
considered complete until you invoke one of the methods on the passed in
callback instance. Hence, you will not receive a subsequent call to any other
method of this class until the execution of this method is complete by invoking
one of the callback methods. You will never receive a sequence of one or more
calls to this method without a previous call to `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)`.


The `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish()` callback is always the last call you will receive and
is useful for doing one time cleanup or resource deallocation after printing.
You will not receive a subsequent call here.

<br />

### Implementation


The APIs defined in this class are designed to enable doing part or all
of the work on an arbitrary thread. For example, if the printed content
does not depend on the UI state, i.e. on what is shown on the screen, then
you can offload the entire work on a dedicated thread, thus making your
application interactive while the print work is being performed. Note that
while your activity is covered by the system print UI and a user cannot
interact with it, doing the printing work on the main application thread
may affect the performance of your other application components as they
are also executed on that thread.


You can also do work on different threads, for example if you print UI
content, you can handle `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()` and `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)` on
the UI thread (assuming onStart initializes resources needed for layout).
This will ensure that the UI does not change while you are laying out the
printed content. Then you can handle `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onWrite(android.print.PageRange[],%20android.os.ParcelFileDescriptor,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.WriteResultCallback)` and `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish()` on another
thread. This will ensure that the main thread is busy for a minimal amount of
time. Also this assumes that you will generate the printed content in
`https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)` which is not mandatory. If you use multiple
threads, you are responsible for proper synchronization.

<br />

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback` Base class for implementing a callback for the result of `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)`. |
| ` class` | `https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback` Base class for implementing a callback for the result of `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onWrite(android.print.PageRange[],%20android.os.ParcelFileDescriptor,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.WriteResultCallback)`. |

| ### Constants ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/print/PrintDocumentAdapter#EXTRA_PRINT_PREVIEW` Extra: mapped to a boolean value that is `true` if the current layout is for a print preview, `false` otherwise. |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/print/PrintDocumentAdapter#PrintDocumentAdapter()() ` |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/print/PrintDocumentAdapter#onFinish()() ` Called when printing finishes. |
| ` abstract void` | ` https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)(https://developer.android.com/reference/android/print/PrintAttributes oldAttributes, https://developer.android.com/reference/android/print/PrintAttributes newAttributes, https://developer.android.com/reference/android/os/CancellationSignal cancellationSignal, https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback callback, https://developer.android.com/reference/android/os/Bundle extras) ` Called when the print attributes (page size, density, etc) changed giving you a chance to layout the content such that it matches the new constraints. |
| ` void` | ` https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()() ` Called when printing starts. |
| ` abstract void` | ` https://developer.android.com/reference/android/print/PrintDocumentAdapter#onWrite(android.print.PageRange[],%20android.os.ParcelFileDescriptor,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.WriteResultCallback)(https://developer.android.com/reference/android/print/PageRange pages, https://developer.android.com/reference/android/os/ParcelFileDescriptor destination, https://developer.android.com/reference/android/os/CancellationSignal cancellationSignal, https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback callback) ` Called when specific pages of the content should be written in the form of a PDF file to the given file descriptor. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Constants

### EXTRA_PRINT_PREVIEW

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String EXTRA_PRINT_PREVIEW
```

Extra: mapped to a boolean value that is `true` if
the current layout is for a print preview, `false` otherwise.
This extra is provided in the `https://developer.android.com/reference/android/os/Bundle` argument of the `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)` callback.

<br />

**See also:**

- `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onLayout(android.print.PrintAttributes,%20android.print.PrintAttributes,%20android.os.CancellationSignal,%20android.print.PrintDocumentAdapter.LayoutResultCallback,%20android.os.Bundle)`

Constant Value:

"EXTRA_PRINT_PREVIEW"


## Public constructors

### PrintDocumentAdapter

```
public PrintDocumentAdapter ()
```

<br />

<br />

## Public methods

### onFinish

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onFinish ()
```

Called when printing finishes. You can use this callback to release
resources acquired in `https://developer.android.com/reference/android/print/PrintDocumentAdapter#onStart()`. This method is invoked on
the main thread.

<br />

### onLayout

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onLayout (PrintAttributes oldAttributes, 
                PrintAttributes newAttributes, 
                CancellationSignal cancellationSignal, 
                PrintDocumentAdapter.LayoutResultCallback callback, 
                Bundle extras)
```

Called when the print attributes (page size, density, etc) changed
giving you a chance to layout the content such that it matches the
new constraints. This method is invoked on the main thread.


After you are done laying out, you **must** invoke: `https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback#onLayoutFinished(android.print.PrintDocumentInfo,%20boolean)` with
the last argument `true` or `false` depending on
whether the layout changed the content or not, respectively; or `https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback#onLayoutFailed(java.lang.CharSequence)`, if an error occurred;
or `https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback#onLayoutCancelled()` if layout was
cancelled in a response to a cancellation request via the passed in
`https://developer.android.com/reference/android/os/CancellationSignal`. Note that you **must** call one of
the methods of the given callback for this method to be considered complete
which is you will not receive any calls to this adapter until the current
layout operation is complete by invoking a method on the callback instance.
The callback methods can be invoked from an arbitrary thread.


One of the arguments passed to this method is a `https://developer.android.com/reference/android/os/CancellationSignal`
which is used to propagate requests from the system to your application for
canceling the current layout operation. For example, a cancellation may be
requested if the user changes a print option that may affect layout while
you are performing a layout operation. In such a case the system will make
an attempt to cancel the current layout as another one will have to be performed.
Typically, you should register a cancellation callback in the cancellation
signal. The cancellation callback **will not** be made on the
main thread and can be registered as follows:

```
cancellationSignal.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel() {
        // Cancel layout
    }
});
```


**Note:** If the content is large and a layout will be
performed, it is a good practice to schedule the work on a dedicated
thread and register an observer in the provided `https://developer.android.com/reference/android/os/CancellationSignal` upon invocation of which you should stop the
layout.

<br />

<br />

| Parameters ||
|---|---|
| `oldAttributes` | `PrintAttributes`: The old print attributes. <br /> |
| `newAttributes` | `PrintAttributes`: The new print attributes. <br /> |
| `cancellationSignal` | `CancellationSignal`: Signal for observing cancel layout requests. <br /> |
| `callback` | `PrintDocumentAdapter.LayoutResultCallback`: Callback to inform the system for the layout result. <br /> |
| `extras` | `Bundle`: Additional information about how to layout the content. <br /> |

**See also:**

- `https://developer.android.com/reference/android/print/PrintDocumentAdapter.LayoutResultCallback`
- `https://developer.android.com/reference/android/os/CancellationSignal`
- `https://developer.android.com/reference/android/print/PrintDocumentAdapter#EXTRA_PRINT_PREVIEW`

### onStart

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void onStart ()
```

Called when printing starts. You can use this callback to allocate
resources. This method is invoked on the main thread.

<br />

### onWrite

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void onWrite (PageRange[] pages, 
                ParcelFileDescriptor destination, 
                CancellationSignal cancellationSignal, 
                PrintDocumentAdapter.WriteResultCallback callback)
```

Called when specific pages of the content should be written in the
form of a PDF file to the given file descriptor. This method is invoked
on the main thread.


After you are done writing, you should close the file descriptor and
invoke `https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback#onWriteFinished(android.print.PageRange[])`, if writing
completed successfully; or `https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback#onWriteFailed(java.lang.CharSequence)`, if an error occurred; or `https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback#onWriteCancelled()`,
if writing was cancelled in a response to a cancellation request via the passed
in `https://developer.android.com/reference/android/os/CancellationSignal`. Note that you **must** call one of
the methods of the given callback for this method to be considered complete which
is you will not receive any calls to this adapter until the current write
operation is complete by invoking a method on the callback instance. The callback
methods can be invoked from an arbitrary thread.


One of the arguments passed to this method is a `https://developer.android.com/reference/android/os/CancellationSignal`
which is used to propagate requests from the system to your application for
canceling the current write operation. For example, a cancellation may be
requested if the user changes a print option that may affect layout while
you are performing a write operation. In such a case the system will make
an attempt to cancel the current write as a layout will have to be performed
which then may be followed by a write. Typically, you should register a
cancellation callback in the cancellation signal. The cancellation callback
**will not** be made on the main thread and can be registered
as follows:

```
 cancellationSignal.setOnCancelListener(new OnCancelListener() {
     @Override
     public void onCancel() {
         // Cancel write
     }
 });
 
```


**Note:** If the printed content is large, it is a good
practice to schedule writing it on a dedicated thread and register an
observer in the provided `https://developer.android.com/reference/android/os/CancellationSignal` upon invocation of
which you should stop writing.

<br />

<br />

| Parameters ||
|---|---|
| `pages` | `PageRange`: The pages whose content to print - non-overlapping in ascending order. <br /> |
| `destination` | `ParcelFileDescriptor`: The destination file descriptor to which to write. <br /> |
| `cancellationSignal` | `CancellationSignal`: Signal for observing cancel writing requests. <br /> |
| `callback` | `PrintDocumentAdapter.WriteResultCallback`: Callback to inform the system for the write result. <br /> |

**See also:**

- `https://developer.android.com/reference/android/print/PrintDocumentAdapter.WriteResultCallback`
- `https://developer.android.com/reference/android/os/CancellationSignal`