1

All, I am working on a mobile app POC using .Net Maui where we have a requirement to print a page in background. I am able to write the platform specific code to print on Android, however whenever the print job is fired, the screen to select the printer pops up. I want to pass the default printer when printing the page and do not want the user to select the printer everytime, as there will be lot of printing involved in the process. We want the printing to be a background process without any user intervention. I would like to know how to achieve this on .Net Maui Android mobile app using .Net Maui or otherwise.

I tried looking if there is way to pass the default printer when calling the print job on Android, but could not find any option.

Below is what I did so far

  1. Created a partial class to print

  2. In platform specific folder for Android, I added below code for printing

         Context context = Platform.CurrentActivity;
    
         Android.Webkit.WebView  webView = new Android.Webkit.WebView(context);
    
         PrintManager printManager = context.GetSystemService(MainActivity.PrintService).JavaCast<PrintManager>();
    
         string htmlData = "test";
         webView.LoadData(htmlData, null, null);
    
         string jobName = "test";
         PrintAttributes printAttribute = new PrintAttributes.Builder()
                                             .SetMediaSize(PrintAttributes.MediaSize.NaLetter)
                                             .Build();
    
    
         printManager.Print(jobName, webView.CreatePrintDocumentAdapter("test"), printAttribute);
    

0 Answers0