java.lang.Object | |
↳ | android.graphics.pdf.PdfRenderer |
This class enables rendering a PDF document. This class is not thread safe.
If you want to render a PDF, you create a renderer and for every page you want to render, you open the page, render it, and close the page. After you are done with rendering, you close the renderer. After the renderer is closed it should not be used anymore. Note that the pages are rendered one by one, i.e. you can have only a single page opened at any given time.
A typical use of the APIs to render a PDF looks like this:
// create a new renderer PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor()); // let us just render all pages final int pageCount = renderer.getPageCount(); for (int i = 0; i < pageCount; i++) { Page page = renderer.openPage(i); // say we render for showing on the screen page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY); // do stuff with the bitmap // close the page page.close(); } // close the renderer renderer.close();
If you are using this class to rasterize a PDF for printing or show a print preview, it is recommended that you respect the following contract in order to provide a consistent user experience when seeing a preview and printing, i.e. the user sees a preview that is the same as the printout.
shouldScaleForPrinting()
.
PrintAttributes
as the application is responsible to render it such that the margins are respected.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
PdfRenderer.Page | This class represents a PDF document page for rendering. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a new instance.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Closes this renderer.
| |||||||||||
Gets the number of pages in the document.
| |||||||||||
Opens a page for rendering.
| |||||||||||
Gets whether the document prefers to be scaled for printing.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.AutoCloseable
|
Creates a new instance.
Note: The provided file descriptor must be seekable, i.e. its data being randomly accessed, e.g. pointing to a file.
Note: This class takes ownership of the passed in file descriptor and is responsible for closing it when the renderer is closed.
input | Seekable file descriptor to read from. |
---|
IOException | If an error occurs while reading the file. |
---|---|
SecurityException | If the file requires a password or the security scheme is not supported. |
Closes this renderer. You should not use this instance after this method is called.
Gets the number of pages in the document.
Opens a page for rendering.
index | The page index. |
---|
Gets whether the document prefers to be scaled for printing. You should take this info account if the document is rendered for printing and the target media size differs from the page size.
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you must use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
Throwable |
---|