Skip to content

Most visited

Recently visited



public class PrintedPdfDocument
extends PdfDocument

     ↳ android.print.pdf.PrintedPdfDocument

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(), getPageHeight(), and getPageContentRect(), respectively. The startPage(int) methods creates pages whose 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,

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

 // draw something on the page
 View content = getContentView();

 // finish the page
 . . .
 // add more pages
 . . .
 // write the document content

 //close the document


Public constructors

PrintedPdfDocument(Context context, PrintAttributes attributes)

Creates a new document.

Public methods

Rect getPageContentRect()

Gets the content rectangle.

int getPageHeight()

Gets the page height.

int getPageWidth()

Gets the page width.

PdfDocument.Page startPage(int pageNumber)

Starts a new page.

Inherited methods

From class
From class java.lang.Object

Public constructors


added in API level 19
PrintedPdfDocument (Context context, 
                PrintAttributes attributes)

Creates a new document.

Note: You must close the document after you are done by calling close().

context Context: Context instance for accessing resources.

This value must never be null.

attributes PrintAttributes: The print attributes.

This value must never be null.

Public methods


added in API level 19
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.

Rect The content rectangle.

This value will never be null.


added in API level 19
int getPageHeight ()

Gets the page height.

int The page height in PostScript points (1/72th of an inch).


added in API level 19
int getPageWidth ()

Gets the page width.

int The page width in PostScript points (1/72th of an inch).


added in API level 19
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.

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

pageNumber int: The page number. Must be a non negative.

PdfDocument.Page A blank page.

This value will never be null.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)