{"id":9826,"date":"2014-04-08T08:14:42","date_gmt":"2014-04-08T15:14:42","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=9826"},"modified":"2014-04-08T08:14:42","modified_gmt":"2014-04-08T15:14:42","slug":"native-printing-with-android","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/native-printing-with-android\/","title":{"rendered":"Native Printing with Android"},"content":{"rendered":"<p>\t\t\t\tThe latest version of Android, <a href=\"https:\/\/developer.android.com\/about\/versions\/kitkat.html\" title=\"Android KitKat\" target=\"_blank\">KitKat<\/a>, adds long-awaited printing support to the platform. Android applications can now provide low-level control of print jobs, in addition to print integration for applications incorporating web content. This makes KitKat particularly well-suited to hybrid applications built with Xamarin using <a href=\"http:\/\/weblogs.asp.net\/scottgu\/archive\/2010\/07\/02\/introducing-razor.aspx\" title=\"Razor View Engine\" target=\"_blank\">Razor<\/a> templates.<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/kitkatprinting.png\" alt=\"Android KitKat Printing\" width=\"619\" height=\"242\" class=\"aligncenter size-full wp-image-9831\" \/><\/p>\n<h2>Printing with a WebView<\/h2>\n<p>First let&#8217;s look at the easy to use print support offered through the <strong>WebView<\/strong> control.<\/p>\n<p>Printing requires 3 steps:<\/p>\n<ol>\n<li>In an Activity, get a reference to the <strong>PrintManager<\/strong> system service.<\/li>\n<li>Create an instance of a class that inherits from <strong>PrintDocumentAdapter<\/strong>.<\/li>\n<li>Call the <strong>PrintManager<\/strong>&#8216;s <strong>Print<\/strong> method, passing it the print adapter.<\/li>\n<\/ol>\n<p>The <strong>PrintDocumentAdapter<\/strong> is an abstract class that provides a contract to implement for supplying print content. For web content, the <strong>WebView<\/strong> class includes a <strong>PrintDocumentAdapter<\/strong>, making printing HTML from a WebView incredibly easy to do:<\/p>\n<p>The following example shows a <strong>WebView<\/strong> built using the <em>Android WebView template<\/em>, which uses Razor as an HTML templating engine:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/razorhybrid.png\" alt=\"Android Printing from a Xamarin Razor Template\" width=\"516\" height=\"774\" class=\"aligncenter size-full wp-image-9833\" \/><\/p>\n<p>Although printing can be done using any <strong>WebView<\/strong>, integrating printing with Razor and this project template makes it really easy to build a hybrid application that can leverage native features such as KitKat printing.<\/p>\n<p>Printing from the <strong>WebView<\/strong> with C# is as simple as adding these 2 lines of code in the activity:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar printMgr = (PrintManager)GetSystemService(Context.PrintService);\nprintMgr.Print(&quot;Razor HMTL Hybrid&quot;, webView.CreatePrintDocumentAdapter(), null);\n<\/pre>\n<p>When we call <strong>Print<\/strong> Android presents a system dialog, allowing the user to choose the print destination, as shown below:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/printdialog.png\" alt=\"Android Print Dialog\" width=\"516\" height=\"774\" class=\"aligncenter size-full wp-image-9835\" \/><\/p>\n<h2>Custom Print Adapter<\/h2>\n<p>To print from native Android views, taking low-level control of the print layout, we can implement our own <strong>PrintDocumentAdapter<\/strong>.<\/p>\n<p>For example, let&#8217;s say we would like to print the layout from the following screen:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/printactivity.png\" alt=\"Android Print Activity Low Level\" width=\"516\" height=\"774\" class=\"aligncenter size-full wp-image-9837\" \/><\/p>\n<p>In our implementation of <strong>PrintDocumentAdapter<\/strong>, the required methods to implement are:<\/p>\n<ul>\n<li><strong>OnLayout<\/strong> &#8211; Allows laying out print content based on the <strong>PrintAttributes<\/strong>.<\/li>\n<li><strong>OnWrite<\/strong> &#8211; Allows writing a PDF file with content to print.<\/li>\n<\/ul>\n<p>In <strong>OnLayout<\/strong>, we create a <strong>PrintDocumentInfo<\/strong> instance, which contains metadata about the document being printed.<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override void OnLayout (PrintAttributes oldAttributes, PrintAttributes newAttributes, \n                               CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)\n{\n  document = new PrintedPdfDocument (context, newAttributes);\n\n  CalculateScale (newAttributes);\n\n  var printInfo = new PrintDocumentInfo\n    .Builder (&quot;MyPrint.pdf&quot;)\n    .SetContentType (PrintContentType.Document)\n    .SetPageCount (1)\n    .Build ();\n\n  callback.OnLayoutFinished (printInfo, true);\n}\n<\/pre>\n<p>In <strong>OnWrite<\/strong>, we implement the code to draw printed content and write it to the output stream to be printed.<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override void OnWrite (PageRange[] pages, ParcelFileDescriptor destination, \n                              CancellationSignal cancellationSignal, WriteResultCallback callback)\n{\n  PrintedPdfDocument.Page page = document.StartPage (0);\n\n  page.Canvas.Scale (scale, scale);\n\n  view.Draw (page.Canvas);\n\n  document.FinishPage (page);\n\n  WritePrintedPdfDoc (destination);\n\n  document.Close ();\n\n  document.Dispose ();\n\n  callback.OnWriteFinished (pages);\n}\n<\/pre>\n<p>Printing with our custom adapter is just like using the <strong>WebView<\/strong>&#8216;s adapter. Simply pass an instance of the custom adapter, called <strong>GenericPrintAdapter<\/strong> in this case, to the <strong>PrintManager<\/strong>&#8216;s <strong>Print<\/strong> method:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar printManager = (PrintManager)GetSystemService (Context.PrintService);\nvar content = FindViewById&lt;LinearLayout&gt; (Resource.Id.linearLayout1);\nvar printAdapter = new GenericPrintAdapter (this, content);\n\nprintManager.Print (&quot;MyPrintJob&quot;, printAdapter, null);\n<\/pre>\n<p>When the user clicks print, the same system dialog is displayed. In addition to a local printer, you can select <a href=\"http:\/\/www.google.com\/cloudprint\/learn\/\" title=\"Google Cloud Print\" target=\"_blank\">Google Cloud Print<\/a> or print to a PDF file, resulting in the print shown below:<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/printedpdf.png\" alt=\"Android Printed to PDF\" width=\"482\" height=\"664\" class=\"aligncenter size-full wp-image-9851\" \/><\/p>\n<p>The printing support added to KitKat is a much welcome feature that is easy to get started with. You can download the code used in this post from my <a href=\"https:\/\/github.com\/mikebluestein\/KitKatPrintDemo\" title=\"github repo\" target=\"_blank\">GitHub repo<\/a>.<\/p>\n<p><a href=\"http:\/\/forums.xamarin.com\/discussion\/14472\/\" title=\"Discuss Android KitKat Printing in the Xamarin Forums\"><em>Discuss this post in the Xamarin forums.<\/em><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The latest version of Android, KitKat, adds long-awaited printing support to the platform. Android applications can now provide low-level control of print jobs, in addition to print integration for applications incorporating web content. This makes KitKat particularly well-suited to hybrid applications built with Xamarin using Razor templates. Printing with a WebView First let&#8217;s look at [&hellip;]<\/p>\n","protected":false},"author":1932,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[5,4],"class_list":["post-9826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-android","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>The latest version of Android, KitKat, adds long-awaited printing support to the platform. Android applications can now provide low-level control of print jobs, in addition to print integration for applications incorporating web content. This makes KitKat particularly well-suited to hybrid applications built with Xamarin using Razor templates. Printing with a WebView First let&#8217;s look at [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/9826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1932"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=9826"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/9826\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=9826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=9826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=9826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}