# PrintedPdfDocument

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

# PrintedPdfDocument

*** ** * ** ***

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

`
public


class
PrintedPdfDocument
`  
`


``

extends `[PdfDocument](https://developer.android.com/reference/android/graphics/pdf/PdfDocument)`


``


`

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

<br />

*** ** * ** ***

This class is a helper for creating a PDF file for given print attributes. It is useful for implementing printing via the native Android graphics APIs.

This class computes the page width, page height, and content rectangle from the provided print attributes and these precomputed values can be accessed via[getPageWidth()](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageWidth()),[getPageHeight()](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageHeight()), and[getPageContentRect()](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageContentRect()), respectively. The[startPage(int)](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#startPage(int))methods creates pages whose[PageInfo](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.PageInfo)is initialized with the precomputed values for width, height, and content rectangle.

A typical use of the APIs looks like this:  

```
 // open a new document
 PrintedPdfDocument document = new PrintedPdfDocument(context,
         printAttributes);

 // start a page
 Page page = document.startPage(0);

 // 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

|                                                                                                                                                                                            ### Public constructors                                                                                                                                                                                            ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[PrintedPdfDocument](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#PrintedPdfDocument(android.content.Context,%20android.print.PrintAttributes))`(`[Context](https://developer.android.com/reference/android/content/Context)` context, `[PrintAttributes](https://developer.android.com/reference/android/print/PrintAttributes)` attributes) ` Creates a new document. |

|                                                                                                                        ### Public methods                                                                                                                         ||
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[Rect](https://developer.android.com/reference/android/graphics/Rect)                             | ` `[getPageContentRect](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageContentRect())`() ` Gets the content rectangle. |
| ` int`                                                                                               | ` `[getPageHeight](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageHeight())`() ` Gets the page height.                 |
| ` int`                                                                                               | ` `[getPageWidth](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#getPageWidth())`() ` Gets the page width.                    |
| ` `[PdfDocument.Page](https://developer.android.com/reference/android/graphics/pdf/PdfDocument.Page) | ` `[startPage](https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument#startPage(int))`(int pageNumber) ` Starts a new page.           |

| ### Inherited methods |
|-----------------------|---|
| From class` `[android.graphics.pdf.PdfDocument](https://developer.android.com/reference/android/graphics/pdf/PdfDocument)` ` |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` void`                                                                                                                                                                          | ` `[close](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#close())`() ` Closes this document.                                                                                                                                                                                                                                                                               | | ` 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.                                                                                                                                                              | | ` 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.                                                                                                                                                  | ||
| 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

### PrintedPdfDocument

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

```
public PrintedPdfDocument (Context context, 
                PrintAttributes attributes)
```

Creates a new document.

**Note:** You must close the document after you are done by calling[PdfDocument.close()](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#close()).

<br />

<br />

|                                              Parameters                                               ||
|--------------|-----------------------------------------------------------------------------------------|
| `context`    | `Context`: Context instance for accessing resources. This value cannot be`null`. <br /> |
| `attributes` | `PrintAttributes`: The print attributes. This value cannot be`null`. <br />             |

## Public methods

### getPageContentRect

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

```
public Rect getPageContentRect ()
```

Gets the content rectangle. This is the area of the page that contains printed data and is relative to the page top left.

<br />

|                                                             Returns                                                              ||
|-----------------------------------------------------------------------|-----------------------------------------------------------|
| [Rect](https://developer.android.com/reference/android/graphics/Rect) | The content rectangle. This value cannot be`null`. <br /> |

### getPageHeight

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

```
public int getPageHeight ()
```

Gets the page height.

<br />

|                                            Returns                                            ||
|-------|----------------------------------------------------------------------------------------|
| `int` | The page height in PostScript points (1/72th of an inch). Value is 0 or greater <br /> |

### getPageWidth

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

```
public int getPageWidth ()
```

Gets the page width.

<br />

|                                           Returns                                            ||
|-------|---------------------------------------------------------------------------------------|
| `int` | The page width in PostScript points (1/72th of an inch). Value is 0 or greater <br /> |

### startPage

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

```
public PdfDocument.Page startPage (int pageNumber)
```

Starts a new page. The page is created using width, height and content rectangle computed from the print attributes passed in the constructor and the given page number to create an appropriate[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[PdfDocument.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[PdfDocument.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[PdfDocument.finishPage(android.graphics.pdf.PdfDocument.Page)](https://developer.android.com/reference/android/graphics/pdf/PdfDocument#finishPage(android.graphics.pdf.PdfDocument.Page)).

<br />

<br />

|                                         Parameters                                         ||
|--------------|------------------------------------------------------------------------------|
| `pageNumber` | `int`: The page number. Must be a non negative. Value is 0 or greater <br /> |

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

**See also:**

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