FileDocCategorySizeDatePackage
PrintedPdfDocument.javaAPI DocAndroid 5.1 API5855Thu Mar 12 22:22:10 GMT 2015android.print.pdf

PrintedPdfDocument

public class PrintedPdfDocument extends android.graphics.pdf.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 {@link #getPageWidth()}, {@link #getPageHeight()}, and {@link #getPageContentRect()}, respectively. The {@link #startPage(int)} methods creates pages whose {@link 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();

Fields Summary
private static final int
MILS_PER_INCH
private static final int
POINTS_IN_INCH
private final int
mPageWidth
private final int
mPageHeight
private final android.graphics.Rect
mContentRect
Constructors Summary
public PrintedPdfDocument(android.content.Context context, android.print.PrintAttributes attributes)
Creates a new document.

Note: You must close the document after you are done by calling {@link #close()}.

param
context Context instance for accessing resources.
param
attributes The print attributes.


                                         
         
        MediaSize mediaSize = attributes.getMediaSize();

        // Compute the size of the target canvas from the attributes.
        mPageWidth = (int) (((float) mediaSize.getWidthMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);
        mPageHeight = (int) (((float) mediaSize.getHeightMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);

        // Compute the content size from the attributes.
        Margins minMargins = attributes.getMinMargins();
        final int marginLeft = (int) (((float) minMargins.getLeftMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);
        final int marginTop = (int) (((float) minMargins.getTopMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);
        final int marginRight = (int) (((float) minMargins.getRightMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);
        final int marginBottom = (int) (((float) minMargins.getBottomMils() / MILS_PER_INCH)
                * POINTS_IN_INCH);
        mContentRect = new Rect(marginLeft, marginTop, mPageWidth - marginRight,
                mPageHeight - marginBottom);
    
Methods Summary
public android.graphics.RectgetPageContentRect()
Gets the content rectangle. This is the area of the page that contains printed data and is relative to the page top left.

return
The content rectangle.

        return mContentRect;
    
public intgetPageHeight()
Gets the page height.

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

        return mPageHeight;
    
public intgetPageWidth()
Gets the page width.

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

        return mPageWidth;
    
public android.graphics.pdf.PdfDocument.PagestartPage(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 {@link PageInfo}.

After the page is created you can draw arbitrary content on the page's canvas which you can get by calling {@link Page#getCanvas() Page.getCanvas()}. After you are done drawing the content you should finish the page by calling {@link #finishPage(Page)}. After the page is finished you should no longer access the page or its canvas.

Note: Do not call this method after {@link #close()}. Also do not call this method if the last page returned by this method is not finished by calling {@link #finishPage(Page)}.

param
pageNumber The page number. Must be a positive value.
return
A blank page.
see
#finishPage(Page)

        PageInfo pageInfo = new PageInfo
                .Builder(mPageWidth, mPageHeight, pageNumber)
                .setContentRect(mContentRect)
                .create();
        return startPage(pageInfo);