1

I am using iOs default PrinterToPrint in Xamarin to print without showing dialog to choose printer but then also it's showing one dialog which says printing to [PRINTER NAME]. Is there anyway to hide the dialog as well. Like complete silent print functionality?

I am not its possible but I have seen some apps which do that and I am not sure whether they are using the same function or not.

Thanks in advance.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • You need to create the `UIPrinter` url to supply to `PrintToPrinter`, typically you have the user choice a printer via `UIPrinterPickerController` and save the `UIPrinter` in your app settings for future re-use... http://stackoverflow.com/questions/34602302/creating-a-working-uiprinter-object-from-url-for-dialogue-free-printing – SushiHangover Mar 02 '17 at 19:27
  • I am doing same but I an getting dialog box which says "Printing to [Printername]". – Brushfire Meet Mar 02 '17 at 19:39
  • I mis-read your question, and did not realize you were asking about the printjob "Progress" alert, see my updated answer. – SushiHangover Mar 03 '17 at 15:46
  • are u find out this this solution? – Siva Sankar Nov 07 '17 at 04:52

2 Answers2

2

Update:

UIPrinterPickerController comes from UIKit and as such there is no way to push the "printing" process to the background and off the main UI thread.

In the current UIPrintInteractionController.PrintToPrinter implementation (currently up to iOS 10.3 B4) there is no exposed way to disable the print progress (Connecting, Preparing, etc...) alart/dialog (w/ Cancel button) or to modify its appearance.

This interface is high level wrapper using AirPrint and thus Internet Print Protocol (IPP) at a lower level to preform the actual printing, job queue monitoring on the printer, etc... IPP is not currently exposed as a publicly available framework within iOS...

Programs that allow background printing are not using UIPrintInteractionController to do the printing. Most do use UIPrinterPickerController to obtain a UIPrinter selection from the user, but then use the UIPrinter.Url.AbsoluteUrl to "talk" directly to the printer via HTTP/HTTPS Post/Get. Depending upon the printers used, TCP-based sockets are also an option vs. IPP and even USB/serial for direct connected printers.

Re: https://en.wikipedia.org/wiki/Internet_Printing_Protocol

Original:

Pick a Printer:

if (allowUserToSelectDifferentPrinter || printerUrl == null)
{
    UIPrinter uiPrinter = printerUrl != null ? null as UIPrinter : UIPrinter.FromUrl(new NSUrl(printerUrl));
    var uiPrinterPickerController = UIPrinterPickerController.FromPrinter(uiPrinter);
    uiPrinterPickerController.Present(true, (printerPickerController, userDidSelect, error) =>
    {
        if (userDidSelect)
        {
            uiPrinter = uiPrinterPickerController?.SelectedPrinter;
            printerUrl = uiPrinter.Url.AbsoluteUrl.ToString();
            Console.WriteLine($"Save this UIPrinter's Url string for later use: {printerUrl}");
        }
    });
}

Print using UIPrintInteractionController with an existing UIPrinter:

if (printerUrl != null)
{
    // re-create a UIPrinter from a saved NSUrl string
    var uiPrinter = UIPrinter.FromUrl(new NSUrl(printerUrl));
    var printer = UIPrintInteractionController.SharedPrintController;
    printer.ShowsPageRange = false;
    printer.ShowsNumberOfCopies = false;
    printer.ShowsPaperSelectionForLoadedPapers = false;
    var printInfo = UIPrintInfo.PrintInfo;

    printInfo.OutputType = UIPrintInfoOutputType.General;
    printInfo.JobName = "StackOverflow Print Job";
    var textFormatter = new UISimpleTextPrintFormatter("StackOverflow Rocks")
    {
        StartPage = 0,
        ContentInsets = new UIEdgeInsets(72, 72, 72, 72),
        MaximumContentWidth = 6 * 72,
    };
    printer.Delegate = new PrintInteractionControllerDelegate();
    printer.PrintFormatter = textFormatter;
    printer.PrintToPrinter(uiPrinter, (printInteractionController, completed, error) =>
    {
        if ((completed && error != null))
        {
            Console.WriteLine($"Print Error: {error.Code}:{error.Description}");
            PresentViewController(
                UIAlertController.Create("Print Error", "Code: {error.Code} Description: {error.Description}", UIAlertControllerStyle.ActionSheet),
                true, () => { });
        }
        printInfo?.Dispose();
        uiPrinter?.Dispose();

        uiPrinter.
    });
}
else
{
    Console.WriteLine("User has not selected a printer...printing disabled");
}
Community
  • 1
  • 1
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
1

I know this is a somewhat old thread but I had been struggling with implementing a silent printing in iOS for one of my customers and I finally came across an acceptable solution that is very easy to implement.

As mentioned in the accepted answer there is no way to get rid of the popup that displays printing progress. Yet there is a way of hiding it. You can simply change the UIWindowLevel of your key window to UIWindowLevel.Alert + 100. This will guarantee your current window will display above ANY alert view.

Be careful though, as I mentioned, it will be displayed over ANY alert view after the level has been changed. Luckily you can just switch this level back to "Normal" to get the original behavior.

So to recap my solution. I use UIPrintInteractionController.PrintToPrinter in order to print directly to a printer object I created using UIPrinter.FromUrl (this is Xamarin.iOS code btw). Before doing so, I adjust my window level to alert + 100 and once printing is complete I reset my window level to "Normal". Now my printing happens without any visual feedback to my user.

Hope this helps somebody!

Anthony Janssens
  • 211
  • 1
  • 2
  • 8