Added in API level 19

PdfDocument


open class PdfDocument
kotlin.Any
   ↳ android.graphics.pdf.PdfDocument

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();
 
  // create a page description
  PageInfo pageInfo = new PageInfo.Builder(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

This class represents a PDF document page.

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

Public constructors

Creates a new instance.

Public methods
open Unit

Closes this document.

open Unit

Finishes a started page.

open MutableList<PdfDocument.PageInfo!>!

Gets the pages of the document.

open PdfDocument.Page!

Starts a page using the provided PageInfo.

open Unit

Writes the document to an output stream.

Protected methods
open Unit

Public constructors

PdfDocument

Added in API level 19
PdfDocument()

Creates a new instance.

Public methods

close

Added in API level 19
open fun close(): Unit

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(android.graphics.pdf.PdfDocument.PageInfo) is not finished by calling finishPage(android.graphics.pdf.PdfDocument.Page).

finishPage

Added in API level 19
open fun finishPage(page: PdfDocument.Page!): Unit

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.

getPages

Added in API level 19
open fun getPages(): MutableList<PdfDocument.PageInfo!>!

Gets the pages of the document.

Return
MutableList<PdfDocument.PageInfo!>! The pages or an empty list.

startPage

Added in API level 19
open fun startPage(pageInfo: PdfDocument.PageInfo!): PdfDocument.Page!

Starts a page using the provided PageInfo. After the page is created you can draw arbitrary content on the page's canvas which you can get by calling Page#getCanvas(). After you are done drawing the content you should finish the page by calling finishPage(android.graphics.pdf.PdfDocument.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(android.graphics.pdf.PdfDocument.Page).

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

writeTo

Added in API level 19
open fun writeTo(out: OutputStream!): Unit

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(android.graphics.pdf.PdfDocument.PageInfo) is not finished by calling finishPage(android.graphics.pdf.PdfDocument.Page).

Parameters
out OutputStream!: The output stream. Cannot be null.
Exceptions
java.io.IOException If an error occurs while writing.

Protected methods

finalize

Added in API level 19
protected open fun finalize(): Unit
Exceptions
java.lang.Throwable the Exception raised by this method