SlideShare uma empresa Scribd logo
1 de 17
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!

Mais conteúdo relacionado

Semelhante a Printing Framework for Android - PrintManager, PrintDocumentAdapter, Custom Print Adapters

Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android WearPeter Friese
 
Intercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemIntercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemPositive Hack Days
 
Intercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemIntercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemPositive Hack Days
 
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
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
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189Mahmoud Samir Fayed
 
Ipc clipboard and data copy
Ipc  clipboard and  data copyIpc  clipboard and  data copy
Ipc clipboard and data copyVinoth Raj
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletMitchinson
 
Sirius Web Advanced : Customize and Extend the Platform
Sirius Web Advanced : Customize and Extend the PlatformSirius Web Advanced : Customize and Extend the Platform
Sirius Web Advanced : Customize and Extend the PlatformObeo
 
Vaadin 7 CN
Vaadin 7 CNVaadin 7 CN
Vaadin 7 CNjojule
 
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Obeo
 
Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Hassan Abid
 
Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android WearPeter Friese
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Zeeshan Rahman
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Documentmobi fly
 
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQueryMongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQueryMongoDB
 
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!Sébastien Levert
 
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!BIWUG
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseBruce McPherson
 

Semelhante a Printing Framework for Android - PrintManager, PrintDocumentAdapter, Custom Print Adapters (20)

Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
 
Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android Wear
 
Intercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemIntercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI Subsystem
 
Intercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI SubsystemIntercepting Windows Printing by Modifying GDI Subsystem
Intercepting Windows Printing by Modifying GDI Subsystem
 
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
A Microsoft Silverlight User Group Starter Kit Made Available for Everyone to...
 
The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189The Ring programming language version 1.6 book - Part 73 of 189
The Ring programming language version 1.6 book - Part 73 of 189
 
Ipc clipboard and data copy
Ipc  clipboard and  data copyIpc  clipboard and  data copy
Ipc clipboard and data copy
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
Sirius Web Advanced : Customize and Extend the Platform
Sirius Web Advanced : Customize and Extend the PlatformSirius Web Advanced : Customize and Extend the Platform
Sirius Web Advanced : Customize and Extend the Platform
 
Vaadin 7 CN
Vaadin 7 CNVaadin 7 CN
Vaadin 7 CN
 
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
 
Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018Android Jetpack - Google IO Extended Singapore 2018
Android Jetpack - Google IO Extended Singapore 2018
 
Introduction to Android Wear
Introduction to Android WearIntroduction to Android Wear
Introduction to Android Wear
 
Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...Urban Airship and Android Integration for Push Notification and In-App Notifi...
Urban Airship and Android Integration for Push Notification and In-App Notifi...
 
Urban Airship & Android Application Integration Document
Urban Airship & Android Application Integration DocumentUrban Airship & Android Application Integration Document
Urban Airship & Android Application Integration Document
 
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQueryMongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
MongoDB Europe 2016 - Warehousing MongoDB Data using Apache Beam and BigQuery
 
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
SharePoint Saturday Belgium 2018 - APIs, APIs everywhere!
 
APIs, APIs Everywhere!
APIs, APIs Everywhere!APIs, APIs Everywhere!
APIs, APIs Everywhere!
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as database
 
Android Froyo
Android FroyoAndroid Froyo
Android Froyo
 

Último

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 

Último (20)

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 

Printing Framework for Android - PrintManager, PrintDocumentAdapter, Custom Print Adapters

  • 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);