Printing on iPad from WebView

GercoBrandwijkGercoBrandwijk USMember ✭✭

Hello guys,

I want to print a webview with the default printer functionalities which are on iOS. The problem is that it is not working on iPad, but it works on iPhones.

The code for iOS is the following:

UIWebView platformWebView = (UIWebView)webView.PlatformControl;

UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController;

printer.ShowsPageRange = true;

printer.PrintInfo = UIPrintInfo.PrintInfo;
printer.PrintInfo.OutputType = UIPrintInfoOutputType.General;
printer.PrintInfo.JobName = name;

printer.PrintPageRenderer = new UIPrintPageRenderer()
{
    HeaderHeight = 40,
    FooterHeight = 40
};
printer.PrintPageRenderer.AddPrintFormatter(platformWebView.ViewPrintFormatter, 0);

printer.Present(true, (handler, completed, err) =>
{
});

I now that the printer.Present function is primary made for iPhones (http://iosapi.xamarin.com/?link=M:UIKit.UIPrintInteractionController.Present) but it should work when you use True als the first parameter (that's also conform the documentation, not in the doc at the link, but visible when you press F12 on the method in Visual Studio). The problem is that nothing is happening on the iPad when I call this method. No error, nothing.
I am aware of the method printer.PresentFromRectInView, but I don't know what I need to pass as the first two parameters (http://iosapi.xamarin.com/index.aspx?link=M:UIKit.UIPrintInteractionController.PresentFromRectInView).

Does anyone have a working printing solution for iPad?

Thanks in advance.

Regards,
Gerco

Tagged:

Answers

  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Can someone help me with this please?

  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Please, can anyone took a look at this! :) Thanks in advance!

  • NADIYADAMANINADIYADAMANI USMember

    In iPad you have to use PresentFromRect - idk why but the view only shows up in a UIPopOverController-type view.

  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Do you have a code example for that?

  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Can someone please help me with this problem?

  • VasbiVasbi USUniversity ✭✭

    You need to do this with the key part of code being the first line.

            UIViewController controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
    
            printer.PresentFromRectInView(new CGRect(0,0,10,10),controller.View,true, (handler, completed, err) => {
                if(!completed && err !=null) {
                    Console.WriteLine("Printer Error");
                }
            });
    
  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Thanks @Vasbi, I see the popup now! How can I center is in the middle of the screen, also after rotation? Than I need to trigger that first parameter that the dimensions are changed?

  • VasbiVasbi USUniversity ✭✭
    edited July 2015

    Hey @GercoBrandwijk , I haven't worked out how to centre it on the screen, its a bit of a pain and was struggling with your original problem myself..

    The key is the CGRect(0,0,10,10), it is supposed to reference the location and bounds of a button that you expect the print dialog to pop out from, thats why the print dialog has that little arrow shape that comes out the top or bottom. The CGRect is the actual start point and width and height of the supposed button that initialises the print dialog, iOS determines where to place the control based off that.

    You could probably do some device screen measurements and work out the best CGRect to centre the screen, however I haven't tried this yet and think it will be a hit and miss.

    My best suggestion (which is not what you asked to do) is to allow this dialog to pop out from the print button as IOS IPAD wants it to do by passing in the SCREEN position of the button as its CGRect, I put the screen in capitals as this is important as Button.Bounds will only get you the bounds of the button relative to its direct parent, not the screen.

    As for rotation, I haven't gotten that far yet in my project.

    @GercoBrandwijk please update this thread if you find any other information about this topic.

    --- For anyone else confused by this problem---

    It is only an IPad problem as IPhone will always show the print selection screen in full screen mode, also from my research there is absolutely no way of bypassing this printer selection screen when working with iOS.

    Also don't be fooled by this example on the Xamarin Website

    https://developer.xamarin.com/samples/monotouch/PrintSample

    It will work have a nice result when working from the project on an iPad, however if you copy the code it won't work on your project. I think the example is old and is native to iPhone and uses some scaling for iPad.

  • GercoBrandwijkGercoBrandwijk USMember ✭✭

    Yes, I figured out that that GRect is the position where that arrow is pointing to. I prefer that it is in the middle of the screen of course, because the WebView is almost the whole screen. I will see what I can make ;)

  • @GercoBrandwijk said:
    Yes, I figured out that that GRect is the position where that arrow is pointing to. I prefer that it is in the middle of the screen of course, because the WebView is almost the whole screen. I will see what I can make ;)

    have you any luck with that ?

  • GercoBrandwijkGercoBrandwijk USMember ✭✭
    edited February 2016

    @NazariyPelyushkevych

    Yes, this was my final code:

    try
    {
        UIWebView platformWebView = (UIWebView)webView.PlatformControl;
    
        UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController;
    
        printer.ShowsPageRange = true;
    
        printer.PrintInfo = UIPrintInfo.PrintInfo;
        printer.PrintInfo.OutputType = UIPrintInfoOutputType.General;
        printer.PrintInfo.JobName = name;
    
        printer.PrintPageRenderer = new UIPrintPageRenderer()
        {
            HeaderHeight = 40,
            FooterHeight = 40
        };
        printer.PrintPageRenderer.AddPrintFormatter(platformWebView.ViewPrintFormatter, 0);
    
        if (Device.Idiom == TargetIdiom.Phone)
        {
            printer.PresentAsync(true);
        }
        else if (Device.Idiom == TargetIdiom.Tablet)
        {
            printer.PresentFromRectInViewAsync(new CGRect(200, 200, 0, 0), platformWebView, true);
        }
    
        return true;
    }
    catch (Exception)
    {
        return false;
    }
    
Sign In or Register to comment.