FileDocCategorySizeDatePackage
PdfRenderer.javaAPI DocAndroid 5.1 API14530Thu Mar 12 22:22:30 GMT 2015android.graphics.pdf

PdfRenderer

public final class PdfRenderer extends Object implements AutoCloseable

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 {@link #shouldScaleForPrinting()}.
  • When scaling a document for printing the aspect ratio should be preserved.
  • Do not inset the content with any margins from the {@link android.print.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
#close()

Fields Summary
private final dalvik.system.CloseGuard
mCloseGuard
private final android.graphics.Point
mTempPoint
private final long
mNativeDocument
private final int
mPageCount
private android.os.ParcelFileDescriptor
mInput
private Page
mCurrentPage
Constructors Summary
public PdfRenderer(android.os.ParcelFileDescriptor input)
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.

param
input Seekable file descriptor to read from.
throws
java.io.IOException If an error occurs while reading the file.
throws
java.lang.SecurityException If the file requires a password or the security scheme is not supported.


      
    
        
        
    
    
       

                                                                                                   
          
        if (input == null) {
            throw new NullPointerException("input cannot be null");
        }

        final long size;
        try {
            Libcore.os.lseek(input.getFileDescriptor(), 0, OsConstants.SEEK_SET);
            size = Libcore.os.fstat(input.getFileDescriptor()).st_size;
        } catch (ErrnoException ee) {
            throw new IllegalArgumentException("file descriptor not seekable");
        }

        mInput = input;
        mNativeDocument = nativeCreate(mInput.getFd(), size);
        mPageCount = nativeGetPageCount(mNativeDocument);
        mCloseGuard.open("close");
    
Methods Summary
public voidclose()
Closes this renderer. You should not use this instance after this method is called.

        throwIfClosed();
        throwIfPageOpened();
        doClose();
    
private voiddoClose()

        if (mCurrentPage != null) {
            mCurrentPage.close();
        }
        nativeClose(mNativeDocument);
        try {
            mInput.close();
        } catch (IOException ioe) {
            /* ignore - best effort */
        }
        mInput = null;
        mCloseGuard.close();
    
protected voidfinalize()

        try {
            mCloseGuard.warnIfOpen();
            if (mInput != null) {
                doClose();
            }
        } finally {
            super.finalize();
        }
    
public intgetPageCount()
Gets the number of pages in the document.

return
The page count.

        throwIfClosed();
        return mPageCount;
    
private static native voidnativeClose(long documentPtr)

private static native voidnativeClosePage(long pagePtr)

private static native longnativeCreate(int fd, long size)

private static native intnativeGetPageCount(long documentPtr)

private static native longnativeOpenPageAndGetSize(long documentPtr, int pageIndex, android.graphics.Point outSize)

private static native voidnativeRenderPage(long documentPtr, long pagePtr, long destPtr, int destLeft, int destTop, int destRight, int destBottom, long matrixPtr, int renderMode)

private static native booleannativeScaleForPrinting(long documentPtr)

public android.graphics.pdf.PdfRenderer$PageopenPage(int index)
Opens a page for rendering.

param
index The page index.
return
A page that can be rendered.
see
android.graphics.pdf.PdfRenderer.Page#close() PdfRenderer.Page.close()

        throwIfClosed();
        throwIfPageOpened();
        throwIfPageNotInDocument(index);
        mCurrentPage = new Page(index);
        return mCurrentPage;
    
public booleanshouldScaleForPrinting()
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.

return
If to scale the document.

        throwIfClosed();
        return nativeScaleForPrinting(mNativeDocument);
    
private voidthrowIfClosed()

        if (mInput == null) {
            throw new IllegalStateException("Already closed");
        }
    
private voidthrowIfPageNotInDocument(int pageIndex)

        if (pageIndex < 0 || pageIndex >= mPageCount) {
            throw new IllegalArgumentException("Invalid page index");
        }
    
private voidthrowIfPageOpened()

        if (mCurrentPage != null) {
            throw new IllegalStateException("Current page not closed");
        }