7

I'm all new to Xamarin and I'm currently working on a sample or a "prove of concept" app using Xamarin.Forms.

I'm supposed to perform a print task from this app though I'm not at this point sure what to print yet (the screen, content of a label, a file etc.).

Either way, what is the easiest way to print from a Xamarin.Forms app? (current target is primarily Android 4.4+).

I hope this isn't too complicated :)

EDIT:

Ok let me just update this post as the original text might be a bit ambitious/vague.

I have a Xamarin.Forms project (+ an Android part) and I have some HTML available in the XF part of the project that I need to get into a WebView and print it. From what I understand, the thing with the WebView has to be done on the Android part of the project due to the fact that this is where the printing will be handled. I was hoping this could be done from code since I don't really need to display the WebView, just print it's content. The Android part of the project has only the MainActivity and no layouts or XAML files. I don't know where to add the WebView or how to access it (other than DependecyService seems to be a buzz word here) so I'm kinda stuck here.

I'm thinking that this task should be rather trivial to someone with a little more Xamarin experience than me.

Aidal
  • 799
  • 4
  • 8
  • 33
  • This blog might help you: https://blog.xamarin.com/native-printing-with-android/ – stefana Mar 01 '16 at 11:42
  • I have already seen this article and it appears to be about native Android apps (or the Android specific part) and not something done from the Xamarin.Forms (cross-platform) part of a project. Since I'm total newbie I'm trying to stick to the Forms part as much as I can. – Aidal Mar 01 '16 at 11:58
  • It can't be done via Forms. The closest you could be is to abstract some parts via [DependencyService](https://developer.xamarin.com/guides/xamarin-forms/dependency-service/). – stefana Mar 01 '16 at 12:00
  • I bet that is correct but since I'm pretty new to this, I need a little more guidance as to how and where to add code. In my SF project I have the HTML I want to print, what I need is to know how to get this into a WebView in the Android part of the project and then to print it. When I think of this I'm thinking it should be fairly easy to anyone with a little more Xamarin experience than me. – Aidal Mar 03 '16 at 10:20
  • Since the codebehind in xamarin is all c# you could possibly use some native c# libraries as well. This opens up a lot of possibilities. you might need to fiddle a bit to get them to work on Ios though. maybe try https://www.c-sharpcorner.com/UploadFile/mahesh/printdialog-in-C-Sharp/ and https://www.syncfusion.com/kb/8767/how-to-print-pdf-documents-in-xamarin-forms-platform one of em uses the dependency service, which can be daunting but is actually very easy to use. try this: https://xamarinhelp.com/xamarin-forms-dependency-injection/ – Daan Dec 19 '18 at 07:43

4 Answers4

5

Every platform XF supports has it's own mechanism for printing. XF does not provide any abstractions for printing in a cross-platform manner. You will need to write printing logic for each layer and expose it to XF using DependencyService (or some other DI engine).

Jason
  • 86,222
  • 15
  • 131
  • 146
  • Ok, I'm currently looking into the DependencyService thing, as it looks like I will be needing it for a lot of other stuff as well later on. – Aidal Mar 01 '16 at 15:05
4

Here is a good example, of course, using dependency service: https://codemilltech.com/xamarin-forms-e-z-print/

Alexandre
  • 7,004
  • 5
  • 54
  • 72
1

I so wanted to do this but it was too hard. Finally built it into Forms9Patch - a MIT licensed open source project.

Verifying that Printing is available

Before printing, you should verify that printing is available on your device. To do so, call:

if (Forms9Patch.PrintService.CanPrint)
{
    // do the printing here
}

Print the contents of a Xamarin.Forms.WebView

using Forms9Patch;
    ...
    var myWebView = new Xamarin.Forms.WebView
    myWebView.Source = new HtmlWebViewSource 
    { 
        Html = "some HTML text here" 
    };
    ...
    myWebView.Print("my_print_job_name");

Note that your WebView does not have to be attached to a Layout. This allows you to Print without having to display the WebView in your app’s UI.

Printing an HTML string

using Forms9Patch;
    ...
    var myHtmlString =  @"
                            <!DOCTYPE html>
                            <html>
                            <body>
                            <h1>Convert to PNG</h1>
                            <p>This html will be converted to a PNG, PDF, or print.</p>
                            </body>
                            </html>
                            ";
    ...
    myHtmlString.Print("my_print_job_name");

PLEASE NOTE: iOS sometimes places the page breaks in weird places. I have a StackOverflow Bounty on why this happens and how to fix it.

Using EmbeddedResource as a source for a Xamarin.Forms.WebView

This is sort of an experimental feature I’ve built that I’ve found it useful. As such the documentation is sparse. It allow you to put HTML content in a folder in your app’s EmbeddedResources folder and then use it as a source for a WebView. A much nicer solution than using platform specific approach provided by Xamarin. It also supports putting all of the HTML content into a zip file. Please take a look at the source code to see how it works.

baskren
  • 963
  • 8
  • 19
  • i am using your example.. but unable to get the specified html content in my print preview!! its printing blank page. any idea of how to fix it? var myWebView = new Xamarin.Forms.WebView(); WebViewPrintEffect.ApplyTo(myWebView); myWebView.Source = new HtmlWebViewSource { Html = html }; await myWebView.PrintAsync("printingInvoice"); – Ch Usman Dec 26 '21 at 09:42
0

You can handle the printing of lists/ invoices .. with the xfinium pdf component from xamarin componentstore. With that you create your _pdffile and then call the following method which starts the adobereader from where you can select a printer (in my case google cloudprint)

     public void printPdfToCloud(string _pdffile)
        {
            try
            {
                var saveto = System.IO.Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "YourApp/"+_pdffile);
                string file_path = saveto;
                if (System.IO.File.Exists(file_path))
                {
                    Android.Net.Uri pdfFile = Android.Net.Uri.FromFile(new Java.IO.File(file_path));
                    Intent pdfIntent = new Intent(Intent.ActionView);
                    pdfIntent.SetPackage("com.adobe.reader");
                    pdfIntent.SetDataAndType(pdfFile, "application/pdf");
                    pdfIntent.SetFlags(ActivityFlags.NoHistory);
                    StartActivity(pdfIntent);


                }else

                { 
                   // give a note that the file does not exist
                }

            }



            catch (Exception E)
            {
                // Do some Error dialog
            }

        }
user1230268
  • 221
  • 2
  • 14
  • This builds a PDF and prints it? Is there a way to suppress some of the dialogs when printing? - The thing I need to create will be printing a lot so the less dialogs the user is presented for the better. – Aidal Mar 01 '16 at 15:04
  • my piece of code only prints the pdf. But it is really simple to create a pdf with xfinium. Look at their getting startet in the componentstore. – user1230268 Mar 01 '16 at 15:17
  • there is no dialog during creation of the pdf. only messages like page ... added. there is no user interaction necessary until the final printing is startet. that means 1 interaction per document. – user1230268 Mar 01 '16 at 16:56
  • if you prefer to build your print document as html you can do that easily with a webview in which you place your html string yourHtmlString with yourWebView.LoadData(yourHtmlString, "text/html", null); and then call by menu: PrintManager printManager = (PrintManager)GetSystemService (Context.PrintService); PrintDocumentAdapter printDocumentAdapter = yourWebView.CreatePrintDocumentAdapter (); printManager.Print ("YourWebPage", printDocumentAdapter, null); – user1230268 Mar 02 '16 at 08:14
  • The HTML idea might be sufficient for what I need to do and I also did consider such approach earlier. I don't see the LoadData method on the WebView object though. – Aidal Mar 02 '16 at 09:37
  • This is how to load from a string: string htmlSource = @"

    Xamarin.Print

    Welcome to WebView.

    "; myWebView.LoadData(htmlSource, "text/html", null);
    – user1230268 Mar 02 '16 at 11:31
  • I'm trying to make the WebView approach work but I'm not doing it right. In my XF project MainPage I have a ListView. This ListView has an OnItemSelected event handler and it is from here I want to (from code if possible) create a web view, load a html string into it and print it. I realize that the web view part must be done from the MyProjectName.Droid part of the project but I just can't seem to make it work. The Droid part of the project has only MainActivity.cs and some classes I made. No layouts. How and where do I create this web view exactly? – Aidal Mar 02 '16 at 13:50
  • The problem here is, that the WebView on which you call LoadData does not exist. There is no Android WebView only a MainActivity. The html is build in the FX part of the project and is ready, I just don't know where to load it. – Aidal Mar 03 '16 at 12:52
  • the printing itself has to be handled in platform specific code, in your case in the android app, with dependency service – user1230268 Mar 04 '16 at 15:56