Skip to content

Most visited

Recently visited

navigation
Added in API level 19

PdfDocument

public class PdfDocument
extends Object

java.lang.Object
   ↳ android.graphics.pdf.PdfDocument
Known Direct Subclasses


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

Summary

Nested classes

class PdfDocument.Page

This class represents a PDF document page. 

class PdfDocument.PageInfo

This class represents meta-data that describes a PDF PdfDocument.Page

Public constructors

PdfDocument()

Creates a new instance.

Public methods

void close()

Closes this document.

void finishPage(PdfDocument.Page page)

Finishes a started page.

List<PdfDocument.PageInfo> getPages()

Gets the pages of the document.

PdfDocument.Page startPage(PdfDocument.PageInfo pageInfo)

Starts a page using the provided PdfDocument.PageInfo.

void writeTo(OutputStream out)

Writes the document to an output stream.

Protected methods

void finalize()

Invoked when the garbage collector has detected that this instance is no longer reachable.

Inherited methods

From class java.lang.Object

Public constructors

PdfDocument

Added in API level 19
PdfDocument ()

Creates a new instance.

Public methods

close

Added in API level 19
void close ()

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 startPage(PageInfo) is not finished by calling finishPage(Page).

finishPage

Added in API level 19
void finishPage (PdfDocument.Page page)

Finishes a started page. You should always finish the last started page.

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

Parameters
page PdfDocument.Page: The page. Cannot be null.

See also:

getPages

Added in API level 19
List<PdfDocument.PageInfo> getPages ()

Gets the pages of the document.

Returns
List<PdfDocument.PageInfo> The pages or an empty list.

startPage

Added in API level 19
PdfDocument.Page startPage (PdfDocument.PageInfo pageInfo)

Starts a page using the provided PdfDocument.PageInfo. After the page is created you can draw arbitrary content on the page's canvas which you can get by calling getCanvas(). After you are done drawing the content you should finish the page by calling finishPage(Page). After the page is finished you should no longer access the page or its canvas.

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

Parameters
pageInfo PdfDocument.PageInfo: The page info. Cannot be null.
Returns
PdfDocument.Page A blank page.

See also:

writeTo

Added in API level 19
void writeTo (OutputStream out)

Writes the document to an output stream. You can call this method multiple times.

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

Parameters
out OutputStream: The output stream. Cannot be null.
Throws
IOException If an error occurs while writing.

Protected methods

finalize

Added in API level 19
void finalize ()

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
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.