Android APIs
public final class

PdfRenderer

extends Object
implements AutoCloseable
java.lang.Object
   ↳ android.graphics.pdf.PdfRenderer

Class Overview

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();
 

Print preview and print output

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.

  • Respect the property whether the document would like to be scaled for printing as per shouldScaleForPrinting().
  • When scaling a document for printing the aspect ratio should be preserved.
  • Do not inset the content with any margins from the PrintAttributes as the application is responsible to render it such that the margins are respected.
  • If document page size is greater than the printed media size the content should be anchored to the upper left corner of the page for left-to-right locales and top right corner for right-to-left locales.

See Also

Summary

Nested Classes
class PdfRenderer.Page This class represents a PDF document page for rendering. 
Public Constructors
PdfRenderer(ParcelFileDescriptor input)
Creates a new instance.
Public Methods
void close()
Closes this renderer.
int getPageCount()
Gets the number of pages in the document.
PdfRenderer.Page openPage(int index)
Opens a page for rendering.
boolean shouldScaleForPrinting()
Gets whether the document prefers to be scaled for printing.
Protected Methods
void finalize()
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

Public Constructors

public PdfRenderer (ParcelFileDescriptor input)

Added in API level 21

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.

Parameters
input Seekable file descriptor to read from.
Throws
IOException If an error occurs while reading the file.
SecurityException If the file requires a password or the security scheme is not supported.

Public Methods

public void close ()

Added in API level 21

Closes this renderer. You should not use this instance after this method is called.

public int getPageCount ()

Added in API level 21

Gets the number of pages in the document.

Returns
  • The page count.

public PdfRenderer.Page openPage (int index)

Added in API level 21

Opens a page for rendering.

Parameters
index The page index.
Returns
  • A page that can be rendered.

public boolean shouldScaleForPrinting ()

Added in API level 21

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.

Returns
  • If to scale the document.

Protected Methods

protected void finalize ()

Added in API level 21

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.

Throws
Throwable