SlideShare a Scribd company logo
Printing Framework
android.print
The gist of it
The gist of it

PrintManager
Prerequisites


SDK platform 19



SDK build-tools 19 or later



Something to print with


A Google Cloud Print Ready printer



A printer shared through Google Cloud Print



Print to PDF
ApiDemos


[sdk]/sampes/android-19/legacy/ApiDemos
PrintManager
PrintManager printManager = (PrintManager)
context.getSystemService(Context.PRINT_SERVICE);
PrintJob printJob = printManager.print(
printJobName, documentAdapter, attributes);

Name for the print job
(shown to the user)

Default print job attributes

Adapter that emits the document
PrintDocumentAdapter
Web pages
PrintDocumentAdapter documentAdapter =
// Directly from an existing WebView
webView.createPrintDocumentAdapter();
PrintDocumentAdapter
Web pages
// Create a WebView and start printing when its ready
WebView webView = new WebView(context);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(
WebView webView, String url) {
// Print to it as before
PrintDocumentAdapter documentAdapter =
webView.createPrintDocumentAdapter();
}
});
webView.loadUrl(url);
PrintDocumentAdapter
Custom documents
PrintDocumentAdapter documentAdapter =
// Define a custom print adapter
new MyPrintDocumentAdapter(context);
Custom Print Adapters
Events:


onStart()




Print process has begun.

onLayout()





A print setting has changed and the layout of the pages needs to be computed.
Must return expected number of pages.

onWrite()




Render printed pages into a file.

onFinish()


Print process has ended.
Custom Print Adapters
onLayout()
public void onLayout(
final PrintAttributes oldAttributes,
final PrintAttributes newAttributes,
final CancellationSignal cancellationSignal,
final LayoutResultCallback callback,
final Bundle metadata) {
CONTRACT
onLayoutFinished(
PrintDocumentInfo, boolean)
}

onLayoutCancelled()

onLayoutFailed(CharSequence)
Custom Print Adapters
onLayout()


Create a document for printing:
PrintedPdfDocument document =
new PrintedPdfDocument(context, newAttributes);



Compute the number of pages using PrintAttributes



Return print information to the print framework:
PrintDocumentInfo info = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(
PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(pages);
.build();
callback.onLayoutFinished(info, true);



Wait for the print framework to invoke onWrite()
Custom Print Adapters
onWrite()
public void onWrite(
final PageRange[] pages,
final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal,
final WriteResultCallback callback) {
CONTRACT
onWriteFinished(PageRange[])
}

onWriteCancelled()

onWriteFailed(CharSequence)
Custom Print Adapters
onWrite()


Start writing a page:
Page page = document.startPage(pageNumber);



Draw onto the page canvas:
page.getCanvas();




Note that elements are specified in points (1/72 inch)

Finish writing the page:
document.finishPage(page);



Inform WriteResultCallback of the PageRange[] printed
Custom Print Adapters
Caveats
If you need a screen context, remember that the printer and screen densities differ!


Obtain the printer density:
printerDensity = Math.max(
newAttributes.getResolution().getHorizontalDpi(),
newAttributes.getResolution().getVerticalDpi());



Create a new Context to cope with a different Configuration:
Configuration configuration = new Configuration();
configuration.densityDpi = printerDensity;
printContext = createConfigurationContext(
configuration);



Use this context for creating new views:
View view = new LinearLayout(printContext);
view.draw(page.getCanvas());
PrintHelper
PrintHelper printHelper = new PrintHelper(context);
// Instruct how the content should scale
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
// Get the bitmap for the ImageView's drawable
Bitmap bitmap = ((BitmapDrawable)
mImageView.getDrawable()).getBitmap();
// Print the bitmap
printHelper.printBitmap("Print Bitmap", bitmap);
Happy printing!

More Related Content

PPTX
Printing photos-html-using-android
Ketan Raval
 
PPTX
Converting Your Mobile App to the Mobile Cloud
Roger Brinkley
 
PDF
Google Maps API 101
Sebastian Roming
 
PPT
Google Maps API
hchen1
 
PDF
Android app material design from dev's perspective
DeSmart Agile Software House
 
PDF
List Views
Ahsanul Karim
 
PDF
Android Lollipop - Webinar vom 11.12.2014
inovex GmbH
 
PPTX
Android 4.4 Kitkat
awaningrum kartikasari
 
Printing photos-html-using-android
Ketan Raval
 
Converting Your Mobile App to the Mobile Cloud
Roger Brinkley
 
Google Maps API 101
Sebastian Roming
 
Google Maps API
hchen1
 
Android app material design from dev's perspective
DeSmart Agile Software House
 
List Views
Ahsanul Karim
 
Android Lollipop - Webinar vom 11.12.2014
inovex GmbH
 
Android 4.4 Kitkat
awaningrum kartikasari
 

Similar to Android Kit Kat - Printing Framework (20)

PDF
Asp.net MVC DI
LearningTech
 
PDF
Introduction to Android Wear
Peter Friese
 
PPT
Intercepting Windows Printing by Modifying GDI Subsystem
Positive Hack Days
 
PPT
Intercepting Windows Printing by Modifying GDI Subsystem
Positive Hack Days
 
PPTX
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
DataLeader.io
 
PDF
The Ring programming language version 1.6 book - Part 73 of 189
Mahmoud Samir Fayed
 
PPT
Ipc clipboard and data copy
Vinoth Raj
 
DOC
Open microsoft visual studio/tutorialoutlet
Mitchinson
 
PDF
Sirius Web Advanced : Customize and Extend the Platform
Obeo
 
PDF
Vaadin 7 CN
jojule
 
PDF
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Obeo
 
PDF
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
PDF
Introduction to Android Wear
Peter Friese
 
PDF
Urban Airship & Android Application Integration Document
mobi fly
 
PDF
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Zeeshan Rahman
 
PDF
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB
 
PPTX
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
Sébastien Levert
 
PPTX
APIs, APIs Everywhere!
BIWUG
 
PDF
Do something in 5 minutes with gas 1-use spreadsheet as database
Bruce McPherson
 
PPT
Android Froyo
Robert Cooper
 
Asp.net MVC DI
LearningTech
 
Introduction to Android Wear
Peter Friese
 
Intercepting Windows Printing by Modifying GDI Subsystem
Positive Hack Days
 
Intercepting Windows Printing by Modifying GDI Subsystem
Positive Hack Days
 
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
DataLeader.io
 
The Ring programming language version 1.6 book - Part 73 of 189
Mahmoud Samir Fayed
 
Ipc clipboard and data copy
Vinoth Raj
 
Open microsoft visual studio/tutorialoutlet
Mitchinson
 
Sirius Web Advanced : Customize and Extend the Platform
Obeo
 
Vaadin 7 CN
jojule
 
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Obeo
 
Android Jetpack - Google IO Extended Singapore 2018
Hassan Abid
 
Introduction to Android Wear
Peter Friese
 
Urban Airship & Android Application Integration Document
mobi fly
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Zeeshan Rahman
 
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB
 
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
Sébastien Levert
 
APIs, APIs Everywhere!
BIWUG
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Bruce McPherson
 
Android Froyo
Robert Cooper
 
Ad

Recently uploaded (20)

PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
The Future of Artificial Intelligence (AI)
Mukul
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Doc9.....................................
SofiaCollazos
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Ad

Android Kit Kat - Printing Framework

  • 3. The gist of it PrintManager
  • 4. Prerequisites  SDK platform 19  SDK build-tools 19 or later  Something to print with  A Google Cloud Print Ready printer  A printer shared through Google Cloud Print  Print to PDF
  • 6. PrintManager PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE); PrintJob printJob = printManager.print( printJobName, documentAdapter, attributes); Name for the print job (shown to the user) Default print job attributes Adapter that emits the document
  • 7. PrintDocumentAdapter Web pages PrintDocumentAdapter documentAdapter = // Directly from an existing WebView webView.createPrintDocumentAdapter();
  • 8. PrintDocumentAdapter Web pages // Create a WebView and start printing when its ready WebView webView = new WebView(context); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished( WebView webView, String url) { // Print to it as before PrintDocumentAdapter documentAdapter = webView.createPrintDocumentAdapter(); } }); webView.loadUrl(url);
  • 9. PrintDocumentAdapter Custom documents PrintDocumentAdapter documentAdapter = // Define a custom print adapter new MyPrintDocumentAdapter(context);
  • 10. Custom Print Adapters Events:  onStart()   Print process has begun. onLayout()    A print setting has changed and the layout of the pages needs to be computed. Must return expected number of pages. onWrite()   Render printed pages into a file. onFinish()  Print process has ended.
  • 11. Custom Print Adapters onLayout() public void onLayout( final PrintAttributes oldAttributes, final PrintAttributes newAttributes, final CancellationSignal cancellationSignal, final LayoutResultCallback callback, final Bundle metadata) { CONTRACT onLayoutFinished( PrintDocumentInfo, boolean) } onLayoutCancelled() onLayoutFailed(CharSequence)
  • 12. Custom Print Adapters onLayout()  Create a document for printing: PrintedPdfDocument document = new PrintedPdfDocument(context, newAttributes);  Compute the number of pages using PrintAttributes  Return print information to the print framework: PrintDocumentInfo info = new PrintDocumentInfo .Builder("print_output.pdf") .setContentType( PrintDocumentInfo.CONTENT_TYPE_DOCUMENT) .setPageCount(pages); .build(); callback.onLayoutFinished(info, true);  Wait for the print framework to invoke onWrite()
  • 13. Custom Print Adapters onWrite() public void onWrite( final PageRange[] pages, final ParcelFileDescriptor destination, final CancellationSignal cancellationSignal, final WriteResultCallback callback) { CONTRACT onWriteFinished(PageRange[]) } onWriteCancelled() onWriteFailed(CharSequence)
  • 14. Custom Print Adapters onWrite()  Start writing a page: Page page = document.startPage(pageNumber);  Draw onto the page canvas: page.getCanvas();   Note that elements are specified in points (1/72 inch) Finish writing the page: document.finishPage(page);  Inform WriteResultCallback of the PageRange[] printed
  • 15. Custom Print Adapters Caveats If you need a screen context, remember that the printer and screen densities differ!  Obtain the printer density: printerDensity = Math.max( newAttributes.getResolution().getHorizontalDpi(), newAttributes.getResolution().getVerticalDpi());  Create a new Context to cope with a different Configuration: Configuration configuration = new Configuration(); configuration.densityDpi = printerDensity; printContext = createConfigurationContext( configuration);  Use this context for creating new views: View view = new LinearLayout(printContext); view.draw(page.getCanvas());
  • 16. PrintHelper PrintHelper printHelper = new PrintHelper(context); // Instruct how the content should scale printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); // Get the bitmap for the ImageView's drawable Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap(); // Print the bitmap printHelper.printBitmap("Print Bitmap", bitmap);