Changes

Jump to: navigation, search
no edit summary
It is also important to note that the WebViewClient configuration is only necessary if a web page is to automatically print as soon as it has loaded. If the printing is to be initiated by the user selecting a menu option after the page has loaded, only the code in the createWebPrintJob() method outlined above need be included in the application code. The next chapter, entitled [[An Android Studio HTML and Web Content Printing Example]], will demonstrate just such a scenario.
 
=== Printing a Custom Document ===
 
Whilst the HTML and web printing features introduced by the Printing framework provide an easy path to printing content from within an Android application, it is clear that these options will be overly simplistic for more advanced printing requirements. For more complex printing tasks, the Printing framework also provides custom document printing support. This allows content in the form of text and graphics to be drawn onto a canvas and then printed.
 
Unlike HTML and image printing, which can be implemented with relative ease, custom document printing is a more complex, multi-stage process which will be outlined in the [[An Android Studio Custom Document Printing Example]] chapter of this book. These steps can be summarized as follows:
 
* Connect to the Android Print Manager
* Create a Custom Print Adapter sub-classed from the PrintDocumentAdapter class
* Create a PdfDocument instance to represent the document pages
* Obtain a reference to the pages of the PdfDocument instance, each of which has associated with it a Canvas instance
* Draw the content on the page canvases
* Notify the print framework that the document is ready to print
 
The custom print adapter outlined in the above steps needs to implement a number of methods which will be called upon by the Android system to perform specific tasks during the printing process. The most important of these are the onLayout() method which is responsible for re-arranging the document layout in response to the user changing settings such as paper size or page orientation, and the onWrite() method which is responsible for rendering the pages to be printed. This topic will be covered in detail in the chapter entitled [[An Android Studio Custom Document Printing Example]].
== Summary ==
The Android 4.4 KitKat release introduced the ability to print content from Android devices. Print output can be directed to suitably configured printers, a local PDF file or to the cloud via Google Drive. From the perspective of the Android application developer, these capabilities are available for use in applications by making use of the Printing framework. By far the easiest printing options to implement are those involving content in the form of images and HTML. More advanced printing may, however, be implemented using the custom document printing features of the framework.

Navigation menu