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();
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
PdfDocument.Page | This class represents a PDF document page. | ||||||||||
PdfDocument.PageInfo |
This class represents meta-data that describes a PDF PdfDocument.Page .
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a new instance.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Closes this document.
| |||||||||||
Finishes a started page.
| |||||||||||
Gets the pages of the document.
| |||||||||||
Starts a page using the provided
PdfDocument.PageInfo .
| |||||||||||
Writes the document to an output stream.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
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)
.
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.
page | The page. Cannot be null. |
---|
Gets the pages of the document.
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)
.
pageInfo | The page info. Cannot be null. |
---|
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)
.
out | The output stream. Cannot be null. |
---|
IOException | If an error occurs while writing. |
---|
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.
Throwable |
---|