Android APIs
public class

PdfDocument

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

Class Overview

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.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public PdfDocument ()

Added in API level 19

Creates a new instance.

Public Methods

public void close ()

Added in API level 19

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

public void finishPage (PdfDocument.Page page)

Added in API level 19

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 The page. Cannot be null.

public List<PdfDocument.PageInfo> getPages ()

Added in API level 19

Gets the pages of the document.

Returns
  • The pages or an empty list.

public PdfDocument.Page startPage (PdfDocument.PageInfo pageInfo)

Added in API level 19

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 The page info. Cannot be null.
Returns
  • A blank page.
See Also

public void writeTo (OutputStream out)

Added in API level 19

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 The output stream. Cannot be null.
Throws
IOException If an error occurs while writing.

Protected Methods

protected void finalize ()

Added in API level 19

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