FileDocCategorySizeDatePackage
PdfDocument.javaAPI DocAndroid 5.1 API15222Thu Mar 12 22:22:30 GMT 2015android.graphics.pdf

PdfDocument

public class PdfDocument extends Object

This class enables generating a PDF document from native Android content. You create a new document and then for every page you want to add you start a page, write content to the page, and finish the page. After you are done with all pages, you write the document to an output stream and close the document. After a document is closed you should not use it anymore. Note that pages are created one by one, i.e. you can have only a single page to which you are writing at any given time. This class is not thread safe.

A typical use of the APIs looks like this:

// create a new document
PdfDocument document = new PdfDocument();

// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();

// start a page
Page page = document.startPage(pageInfo);

// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());

// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());

// close the document
document.close();

Fields Summary
private final byte[]
mChunk
private final dalvik.system.CloseGuard
mCloseGuard
private final List
mPages
private long
mNativeDocument
private Page
mCurrentPage
Constructors Summary
public PdfDocument()
Creates a new instance.


             
      
        mNativeDocument = nativeCreateDocument();
        mCloseGuard.open("close");
    
Methods Summary
public voidclose()
Closes this document. This method should be called after you are done working with the document. After this call the document is considered closed and none of its methods should be called.

Note: Do not call this method if the page returned by {@link #startPage(PageInfo)} is not finished by calling {@link #finishPage(Page)}.

        throwIfCurrentPageNotFinished();
        dispose();
    
private voiddispose()

        if (mNativeDocument != 0) {
            nativeClose(mNativeDocument);
            mCloseGuard.close();
            mNativeDocument = 0;
        }
    
protected voidfinalize()

        try {
            mCloseGuard.warnIfOpen();
            dispose();
        } finally {
            super.finalize();
        }
    
public voidfinishPage(android.graphics.pdf.PdfDocument$Page page)
Finishes a started page. You should always finish the last started page.

Note: Do not call this method after {@link #close()}. You should not finish the same page more than once.

param
page The page. Cannot be null.
see
#startPage(PageInfo)

        throwIfClosed();
        if (page == null) {
            throw new IllegalArgumentException("page cannot be null");
        }
        if (page != mCurrentPage) {
            throw new IllegalStateException("invalid page");
        }
        if (page.isFinished()) {
            throw new IllegalStateException("page already finished");
        }
        mPages.add(page.getInfo());
        mCurrentPage = null;
        nativeFinishPage(mNativeDocument);
        page.finish();
    
public java.util.ListgetPages()
Gets the pages of the document.

return
The pages or an empty list.

        return Collections.unmodifiableList(mPages);
    
private native voidnativeClose(long nativeDocument)

private native longnativeCreateDocument()

private native voidnativeFinishPage(long nativeDocument)

private static native longnativeStartPage(long nativeDocument, int pageWidth, int pageHeight, int contentLeft, int contentTop, int contentRight, int contentBottom)

private native voidnativeWriteTo(long nativeDocument, java.io.OutputStream out, byte[] chunk)

public android.graphics.pdf.PdfDocument$PagestartPage(android.graphics.pdf.PdfDocument$PageInfo pageInfo)
Starts a page using the provided {@link PageInfo}. After the page is created you can draw arbitrary content on the page's canvas which you can get by calling {@link Page#getCanvas()}. After you are done drawing the content you should finish the page by calling {@link #finishPage(Page)}. After the page is finished you should no longer access the page or its canvas.

Note: Do not call this method after {@link #close()}. Also do not call this method if the last page returned by this method is not finished by calling {@link #finishPage(Page)}.

param
pageInfo The page info. Cannot be null.
return
A blank page.
see
#finishPage(Page)

        throwIfClosed();
        throwIfCurrentPageNotFinished();
        if (pageInfo == null) {
            throw new IllegalArgumentException("page cannot be null");
        }
        Canvas canvas = new PdfCanvas(nativeStartPage(mNativeDocument, pageInfo.mPageWidth,
                pageInfo.mPageHeight, pageInfo.mContentRect.left, pageInfo.mContentRect.top,
                pageInfo.mContentRect.right, pageInfo.mContentRect.bottom));
        mCurrentPage = new Page(canvas, pageInfo);
        return mCurrentPage;
    
private voidthrowIfClosed()
Throws an exception if the document is already closed.

        if (mNativeDocument == 0) {
            throw new IllegalStateException("document is closed!");
        }
    
private voidthrowIfCurrentPageNotFinished()
Throws an exception if the last started page is not finished.

        if (mCurrentPage != null) {
            throw new IllegalStateException("Current page not finished!");
        }
    
public voidwriteTo(java.io.OutputStream out)
Writes the document to an output stream. You can call this method multiple times.

Note: Do not call this method after {@link #close()}. Also do not call this method if a page returned by {@link #startPage( PageInfo)} is not finished by calling {@link #finishPage(Page)}.

param
out The output stream. Cannot be null.
throws
IOException If an error occurs while writing.

        throwIfClosed();
        throwIfCurrentPageNotFinished();
        if (out == null) {
            throw new IllegalArgumentException("out cannot be null!");
        }
        nativeWriteTo(mNativeDocument, out, mChunk);