PdfRenderer

public final class PdfRenderer
extends Object implements AutoCloseable

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.

  • 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. 

Constants

int DOCUMENT_LINEARIZED_TYPE_LINEARIZED

Represents a linearized PDF document.

int DOCUMENT_LINEARIZED_TYPE_NON_LINEARIZED

Represents a non-linearized PDF document.

int DOCUMENT_LINEARIZED_TYPE_UNKNOWN

Represents that the linearization of the PDF document cannot be determined.

int PDF_FORM_TYPE_ACRO_FORM

Represents a PDF with form fields specified using the AcroForm spec

int PDF_FORM_TYPE_NONE

Represents a PDF without form fields

int PDF_FORM_TYPE_XFA_FOREGROUND

Represents a PDF with form fields specified using the XFAF subset of the XFA spec

int PDF_FORM_TYPE_XFA_FULL

Represents a PDF with form fields specified using the entire XFA spec

Public constructors

PdfRenderer(ParcelFileDescriptor fileDescriptor)

Creates a new instance.

PdfRenderer(ParcelFileDescriptor fileDescriptor, LoadParams params)

Creates a new instance of PdfRenderer class.

Public methods

void close()

Closes this renderer.

int getDocumentLinearizationType()

Gets the type of the PDF document.

int getPageCount()

Gets the number of pages in the document.

int getPdfFormType()

Returns the form type of the loaded PDF

PdfRenderer.Page openPage(int index)

Opens a page for rendering.

boolean shouldScaleForPrinting()

Gets whether the document prefers to be scaled for printing.

void write(ParcelFileDescriptor destination, boolean removePasswordProtection)

Saves the current state of the loaded PDF document to the given writable ParcelFileDescriptor.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

Constants

DOCUMENT_LINEARIZED_TYPE_LINEARIZED

public static final int DOCUMENT_LINEARIZED_TYPE_LINEARIZED

Represents a linearized PDF document.

Constant Value: 2 (0x00000002)

DOCUMENT_LINEARIZED_TYPE_NON_LINEARIZED

public static final int DOCUMENT_LINEARIZED_TYPE_NON_LINEARIZED

Represents a non-linearized PDF document.

Constant Value: 1 (0x00000001)

DOCUMENT_LINEARIZED_TYPE_UNKNOWN

public static final int DOCUMENT_LINEARIZED_TYPE_UNKNOWN

Represents that the linearization of the PDF document cannot be determined.

Constant Value: 0 (0x00000000)

PDF_FORM_TYPE_ACRO_FORM

public static final int PDF_FORM_TYPE_ACRO_FORM

Represents a PDF with form fields specified using the AcroForm spec

Constant Value: 1 (0x00000001)

PDF_FORM_TYPE_NONE

public static final int PDF_FORM_TYPE_NONE

Represents a PDF without form fields

Constant Value: 0 (0x00000000)

PDF_FORM_TYPE_XFA_FOREGROUND

public static final int PDF_FORM_TYPE_XFA_FOREGROUND

Represents a PDF with form fields specified using the XFAF subset of the XFA spec

Constant Value: 3 (0x00000003)

PDF_FORM_TYPE_XFA_FULL

public static final int PDF_FORM_TYPE_XFA_FULL

Represents a PDF with form fields specified using the entire XFA spec

Constant Value: 2 (0x00000002)

Public constructors

PdfRenderer

Added in API level 21
public PdfRenderer (ParcelFileDescriptor fileDescriptor)

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.

If the file is from an untrusted source it is recommended to run the renderer in a separate, isolated process with minimal permissions to limit the impact of security exploits. Note: The constructor should be instantiated on the ERROR(/android.annotation.WorkerThread) as it can be long-running while loading the document.

Parameters
fileDescriptor ParcelFileDescriptor: Seekable file descriptor to read from. This value cannot be null.

Throws
IOException If an error occurs while reading the file.
SecurityException If the file requires a password or the security scheme is not supported.
IllegalArgumentException If the ParcelFileDescriptor is not seekable.
NullPointerException If the file descriptor is null.

PdfRenderer

Added in API level 21
public PdfRenderer (ParcelFileDescriptor fileDescriptor, 
                LoadParams params)

Creates a new instance of PdfRenderer class.

Note: The provided file descriptor must be seekable, i.e. its data being randomly accessed, e.g. pointing to a file. If the password passed in LoadParams is incorrect, the PdfRenderer will throw a SecurityException.

Note: This class takes ownership of the passed in file descriptor and is responsible for closing it when the renderer is closed.

If the file is from an untrusted source it is recommended to run the renderer in a separate, isolated process with minimal permissions to limit the impact of security exploits. Note: The constructor should be instantiated on the ERROR(/android.annotation.WorkerThread) as it can be long-running while loading the document.

Parameters
fileDescriptor ParcelFileDescriptor: Seekable file descriptor to read from. This value cannot be null.

params LoadParams: Instance of LoadParams specifying params for loading PDF document. This value cannot be null.

Throws
IOException If an error occurs while reading the file.
SecurityException If the file requires a password or the security scheme is not supported by the renderer.
IllegalArgumentException If the ParcelFileDescriptor is not seekable.
NullPointerException If the file descriptor or load params is null.

Public methods

close

Added in API level 21
public void close ()

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

Throws
IllegalStateException If close() is called before invoking this or if any page is opened and not closed

getDocumentLinearizationType

public int getDocumentLinearizationType ()

Gets the type of the PDF document.

Returns
int The PDF document type. Value is DOCUMENT_LINEARIZED_TYPE_UNKNOWN, DOCUMENT_LINEARIZED_TYPE_NON_LINEARIZED, or DOCUMENT_LINEARIZED_TYPE_LINEARIZED

Throws
IllegalStateException If close() is called before invoking this.

getPageCount

Added in API level 21
public int getPageCount ()

Gets the number of pages in the document.

Returns
int The page count.

Throws
IllegalStateException If close() is called before invoking this.

getPdfFormType

public int getPdfFormType ()

Returns the form type of the loaded PDF

Returns
int Value is PDF_FORM_TYPE_NONE, PDF_FORM_TYPE_ACRO_FORM, PDF_FORM_TYPE_XFA_FULL, or PDF_FORM_TYPE_XFA_FOREGROUND

Throws
IllegalStateException if the renderer is closed
IllegalArgumentException if an unexpected PDF form type is returned

openPage

Added in API level 21
public PdfRenderer.Page openPage (int index)

Opens a page for rendering.

Parameters
index int: The page index to open, starting from index 0.

Returns
PdfRenderer.Page A page that can be rendered. This value cannot be null.

Throws
IllegalStateException If close() is called before invoking this.
IllegalArgumentException If the page number is less than 0 or greater than or equal to the total page count.

shouldScaleForPrinting

Added in API level 21
public boolean shouldScaleForPrinting ()

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
boolean If to scale the document.

Throws
IllegalStateException If close() is called before invoking this.

write

public void write (ParcelFileDescriptor destination, 
                boolean removePasswordProtection)

Saves the current state of the loaded PDF document to the given writable ParcelFileDescriptor. If the document is password-protected then setting removePasswordProtection removes the protection before saving. The PDF document should already be decrypted with the correct password before writing. Useful for printing or sharing. Note: This method closes the provided file descriptor.

Parameters
destination ParcelFileDescriptor: The writable ParcelFileDescriptor This value cannot be null.

removePasswordProtection boolean: If true, removes password protection from the PDF before saving.

Throws
IOException If there's a write error, or if 'removePasswordSecurity' is true but the document remains encrypted.
IllegalStateException If close() is called before invoking this.

Protected methods

finalize

Added in API level 21
protected void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable