public class PrintFileDocumentAdapter extends PrintDocumentAdapter
PrintDocumentAdapter.onFinish()
and delete the file yourself.PrintDocumentAdapter.LayoutResultCallback, PrintDocumentAdapter.WriteResultCallback
EXTRA_PRINT_PREVIEW
Constructor and Description |
---|
PrintFileDocumentAdapter(Context context,
File file,
PrintDocumentInfo documentInfo)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
onLayout(PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
PrintDocumentAdapter.LayoutResultCallback callback,
Bundle metadata)
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 |
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.
|
onFinish, onStart
public PrintFileDocumentAdapter(Context context, File file, PrintDocumentInfo documentInfo)
context
- Context for accessing resources.file
- The PDF file to print.documentInfo
- The information about the printed file.public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, PrintDocumentAdapter.LayoutResultCallback callback, Bundle metadata)
PrintDocumentAdapter
After you are done laying out, you must invoke: PrintDocumentAdapter.LayoutResultCallback.onLayoutFinished(PrintDocumentInfo, boolean)
with
the last argument true
or false
depending on
whether the layout changed the content or not, respectively; or PrintDocumentAdapter.LayoutResultCallback.onLayoutFailed(CharSequence)
, if an error occurred;
or PrintDocumentAdapter.LayoutResultCallback.onLayoutCancelled()
if layout was
cancelled in a response to a cancellation request via the passed in
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 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 CancellationSignal
upon invocation of which you should stop the
layout.
onLayout
in class PrintDocumentAdapter
oldAttributes
- The old print attributes.newAttributes
- The new print attributes.cancellationSignal
- Signal for observing cancel layout requests.callback
- Callback to inform the system for the layout result.metadata
- Additional information about how to layout the content.PrintDocumentAdapter.LayoutResultCallback
,
CancellationSignal
,
PrintDocumentAdapter.EXTRA_PRINT_PREVIEW
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, PrintDocumentAdapter.WriteResultCallback callback)
PrintDocumentAdapter
After you are done writing, you should close the file descriptor and
invoke PrintDocumentAdapter.WriteResultCallback.onWriteFinished(PageRange[])
, if writing
completed successfully; or PrintDocumentAdapter.WriteResultCallback.onWriteFailed(
CharSequence)
, if an error occurred; or PrintDocumentAdapter.WriteResultCallback.onWriteCancelled()
,
if writing was cancelled in a response to a cancellation request via the passed
in 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 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 CancellationSignal
upon invocation of
which you should stop writing.
onWrite
in class PrintDocumentAdapter
pages
- The pages whose content to print - non-overlapping in ascending order.destination
- The destination file descriptor to which to write.cancellationSignal
- Signal for observing cancel writing requests.callback
- Callback to inform the system for the write result.PrintDocumentAdapter.WriteResultCallback
,
CancellationSignal