# PdfDocument

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Nested Classes](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#nestedclasses)\|[Ctors](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#pubctors)\|[Methods](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#inhmethods)  

# PdfDocument

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/graphics/pdf/PdfDocument "View this page in Kotlin")\|Java

`
public


class
PdfDocument
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|----------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.graphics.pdf.PdfDocument |

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known direct subclasses [PrintedPdfDocument](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument) |----------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------| | [PrintedPdfDocument](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument) | This class is a helper for creating a PDF file for given print attributes. | |

<br />

*** ** * ** ***

<br />

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

<br />

## Summary

|                                                                                                                       ### Nested classes                                                                                                                       ||
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` class` | [PdfDocument.Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page) This class represents a PDF document page.                                                                                                         |
| ` class` | [PdfDocument.PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo) This class represents meta-data that describes a PDF[Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page). |

|                                                         ### Public constructors                                                          ||
|---------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[PdfDocument](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#PdfDocument())`() ` Creates a new instance. |

|                                                                                                                                                                                                                                                                                   ### Public methods                                                                                                                                                                                                                                                                                    ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void`                                                                                                                                                                          | ` `[close](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#close())`() ` Closes this document.                                                                                                                                                                                                                                                                               |
| ` void`                                                                                                                                                                          | ` `[finishPage](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finishPage(android.graphics.pdf.PdfDocument.Page))`(`[PdfDocument.Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page)` page) ` Finishes a started page.                                                                                                                     |
| ` `[List](https://developer.android.com/reference/java/util/List)`<`[PdfDocument.PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo)`>` | ` `[getPages](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#getPages())`() ` Gets the pages of the document.                                                                                                                                                                                                                                                               |
| ` `[PdfDocument.Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page)                                                                             | ` `[startPage](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#startPage(android.graphics.pdf.PdfDocument.PageInfo))`(`[PdfDocument.PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo)` pageInfo) ` Starts a page using the provided[PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo). |
| ` void`                                                                                                                                                                          | ` `[writeTo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#writeTo(java.io.OutputStream))`(`[OutputStream](https://developer.android.com/reference/java/io/OutputStream)` out) ` Writes the document to an output stream.                                                                                                                                                  |

|                                                                                                               ### Protected methods                                                                                                               ||
|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[finalize](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### PdfDocument

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public PdfDocument ()
```

Creates a new instance.

<br />

## Public methods

### close

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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(android.graphics.pdf.PdfDocument.PageInfo)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#startPage(android.graphics.pdf.PdfDocument.PageInfo))is not finished by calling[finishPage(android.graphics.pdf.PdfDocument.Page)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finishPage(android.graphics.pdf.PdfDocument.Page)).

<br />

<br />

### finishPage

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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()](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#close()). You should not finish the same page more than once.

<br />

<br />

|                          Parameters                          ||
|--------|------------------------------------------------------|
| `page` | `PdfDocument.Page`: The page. Cannot be null. <br /> |

**See also:**

- [startPage(PageInfo)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#startPage(android.graphics.pdf.PdfDocument.PageInfo))  

### getPages

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public List<PdfDocument.PageInfo> getPages ()
```

Gets the pages of the document.

<br />

|                                                                                                      Returns                                                                                                      ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|
| [List](https://developer.android.com/reference/java/util/List)`<`[PdfDocument.PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo)`>` | The pages or an empty list. <br /> |

### startPage

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public PdfDocument.Page startPage (PdfDocument.PageInfo pageInfo)
```

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

<br />

<br />

|                                Parameters                                 ||
|------------|---------------------------------------------------------------|
| `pageInfo` | `PdfDocument.PageInfo`: The page info. Cannot be null. <br /> |

|                                                         Returns                                                         ||
|---------------------------------------------------------------------------------------------------|----------------------|
| [PdfDocument.Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page) | A blank page. <br /> |

**See also:**

- [finishPage(Page)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finishPage(android.graphics.pdf.PdfDocument.Page))  

### writeTo

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public 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()](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#close()). Also do not call this method if a page returned by[startPage(android.graphics.pdf.PdfDocument.PageInfo)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#startPage(android.graphics.pdf.PdfDocument.PageInfo))is not finished by calling[finishPage(android.graphics.pdf.PdfDocument.Page)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finishPage(android.graphics.pdf.PdfDocument.Page)).

<br />

<br />

|                            Parameters                            ||
|-------|-----------------------------------------------------------|
| `out` | `OutputStream`: The output stream. Cannot be null. <br /> |

|                                                    Throws                                                     ||
|----------------------------------------------------------------------------|-----------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | If an error occurs while writing. |

## Protected methods

### finalize

Added in[API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected void finalize ()
```

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the`finalize`method to dispose of system resources or to perform other cleanup.

The general contract of`finalize`is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The`finalize`method may take any action, including making this object available again to other threads; the usual purpose of`finalize`, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The`finalize`method of class`Object`performs no special action; it simply returns normally. Subclasses of`Object`may override this definition.

The Java programming language does not guarantee which thread will invoke the`finalize`method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the`finalize`method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The`finalize`method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the`finalize`method causes the finalization of this object to be halted, but is otherwise ignored.

<br />

|                                   Throws                                    ||
|--------------------------------------------------------------------------|---|
| [Throwable](https://developer.android.com/reference/java/lang/Throwable) |   |