Stay organized with collections
Save and categorize content based on your preferences.
PrintedPdfDocument
open class PrintedPdfDocument : PdfDocument
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,
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();
Summary
| Public constructors |
|
Creates a new document.
|
| Public methods |
| open Rect |
Gets the content rectangle.
|
| open Int |
Gets the page height.
|
| open Int |
Gets the page width.
|
| open PdfDocument.Page |
Starts a new page.
|
| Inherited functions |
From class PdfDocument
Unit |
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) is not finished by calling finishPage(android.graphics.pdf.PdfDocument.Page).
|
Unit |
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.
|
Unit |
finishPage(page: PdfDocument.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.
|
MutableList<PdfDocument.PageInfo!>! |
getPages()
Gets the pages of the document.
|
PdfDocument.Page! |
startPage(pageInfo: PdfDocument.PageInfo!)
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).
|
Unit |
writeTo(out: OutputStream!)
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).
|
|
Public constructors
PrintedPdfDocument
PrintedPdfDocument(
context: Context,
attributes: PrintAttributes)
Creates a new document.
Note: You must close the document after you are done by calling close().
| Parameters |
context |
Context: Context instance for accessing resources. This value cannot be null. |
attributes |
PrintAttributes: The print attributes. This value cannot be null. |
Public methods
getPageContentRect
open fun getPageContentRect(): Rect
Gets the content rectangle. This is the area of the page that contains printed data and is relative to the page top left.
| Return |
Rect |
The content rectangle. This value cannot be null. |
getPageHeight
open fun getPageHeight(): Int
Gets the page height.
| Return |
Int |
The page height in PostScript points (1/72th of an inch). Value is 0 or greater |
getPageWidth
open fun getPageWidth(): Int
Gets the page width.
| Return |
Int |
The page width in PostScript points (1/72th of an inch). Value is 0 or greater |
startPage
open fun startPage(pageNumber: Int): PdfDocument.Page
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(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 |
pageNumber |
Int: The page number. Must be a non negative. Value is 0 or greater |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-02-13 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-02-13 UTC."],[],[]]