public void Print(PSPDFPrintBarButtonItem barButton)
        {
            try
            {
                if (UIPrintInteractionController.PrintingAvailable)
                {
                    UIPrintInteractionController pic = UIPrintInteractionController.SharedPrintController;
                    if (pic != null)
                    {
                        // PrintInfo
                        UIPrintInfo printInfo = UIPrintInfo.PrintInfo;
                        printInfo.OutputType = UIPrintInfoOutputType.General;
                        printInfo.JobName    = "Print Job: eBriefing";
                        printInfo.Duplex     = UIPrintInfoDuplex.None;

                        if (Orientation == ORIENTATION.LANDSCAPE)
                        {
                            printInfo.Orientation = UIPrintInfoOrientation.Landscape;
                        }
                        else
                        {
                            printInfo.Orientation = UIPrintInfoOrientation.Portrait;
                        }

                        pic.PrintInfo           = printInfo;
                        pic.ShowsNumberOfCopies = true;
                        pic.ShowsPaperSelectionForLoadedPapers = true;
                        pic.ShowsPageRange = false;

                        pic.PrintPageRenderer = renderer;

                        // Show print options
                        pic.PresentFromBarButtonItem(barButton, true, (printController, completed, error) =>
                        {
                            if (!completed && error != null)
                            {
                                Console.WriteLine("PrintHelper - Print Error Code " + error.Code);
                            }

                            renderer.Dispose();
                            renderer = null;
                            dict.Clear();
                            dict = null;
                        });
                    }
                }
                else
                {
                    new UIAlertView(StringRef.alert, "Print is not available at this time.", null, StringRef.ok, null).Show();
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLineDebugging("PrintHelper - Print: {0}", ex.ToString());
            }
        }
Exemple #2
0
        private void _loadFinished(object sender, EventArgs e)
        {
            var webview = sender as UIWebView;
            UIPrintInteractionController controller = UIPrintInteractionController.SharedPrintController;

            if (null == controller)
            {
                return;
            }

            // 设置打印机的一些默认信息
            UIPrintInfo printInfo = UIPrintInfo.PrintInfo;

            // 输出类型
            printInfo.OutputType = UIPrintInfoOutputType.General;
            // 打印队列名称
            printInfo.JobName = "HtmlDemo";
            // 是否单双面打印
            printInfo.Duplex = UIPrintInfoDuplex.LongEdge;
            // 设置默认打印信息
            controller.PrintInfo = printInfo;

            // 显示页码范围
            controller.ShowsPageRange = true;

            // 预览设置
            UIPrintPageRenderer myRenderer = new UIPrintPageRenderer();

            // To draw the content of each page, a UIViewPrintFormatter is used.
            // 生成html格式
            UIViewPrintFormatter viewFormatter = webview.ViewPrintFormatter;

            myRenderer.AddPrintFormatter(viewFormatter, 0);
            // 渲染html
            controller.PrintPageRenderer = myRenderer;

            controller.Present(true, (handler, completed, err) =>
            {
                if (!completed && err != null)
                {
                    System.Diagnostics.Debug.WriteLine("Printer Error");
                }
            });
        }
Exemple #3
0
        private static bool PrintUrl(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                NSUrl item = new NSUrl(url);
                if (UIPrintInteractionController.CanPrint(item))
                {
                    UIPrintInfo printInfo = UIPrintInfo.PrintInfo;
                    printInfo.JobName = url.Substring(url.LastIndexOf(Path.DirectorySeparatorChar) + 1);

                    UIPrintInteractionController.SharedPrintController.PrintInfo    = printInfo;
                    UIPrintInteractionController.SharedPrintController.PrintingItem = item;

                    return(true);
                }
            }

            return(false);
        }
        void PrintSelectedRecipes(object sender, EventArgs args)
        {
            // Get a reference to the singleton iOS printing concierge
            UIPrintInteractionController printController = UIPrintInteractionController.SharedPrintController;

            // Instruct the printing concierge to use our custom UIPrintPageRenderer subclass when printing this job
            printController.PrintPageRenderer = new RecipePrintPageRenderer(recipes.Recipes);

            // Ask for a print job object and configure its settings to tailor the print request
            UIPrintInfo info = UIPrintInfo.PrintInfo;

            // B&W or color, normal quality output for mixed text, graphics, and images
            info.OutputType = UIPrintInfoOutputType.General;

            // Select the job named this in the printer queue to cancel our print request.
            info.JobName = "Recipes";

            // Instruct the printing concierge to use our custom print job settings.
            printController.PrintInfo = info;

            // Present the standard iOS Print Panel that allows you to pick the target Printer, number of pages, double-sided, etc.
            printController.Present(true, PrintingCompleted);
        }
Exemple #5
0
        public static void Print(string url)
        {
            if (!UIPrintInteractionController.PrintingAvailable)
            {
                new UIAlertView(TouchFactory.Instance.GetResourceString("PrintErrorTitle"),
                                TouchFactory.Instance.GetResourceString("PrintError"), null,
                                TouchFactory.Instance.GetResourceString("Dismiss"), null).Show();

                return;
            }

            string printUrl = null;
            int    index    = url.IndexOf('?');

            if (index >= 0)
            {
                HttpUtility.ParseQueryString(url.Substring(index)).TryGetValue("url", out printUrl);
            }

            UIPrintInfo printInfo = UIPrintInfo.PrintInfo;

            printInfo.OutputType = UIPrintInfoOutputType.General;
            printInfo.Duplex     = UIPrintInfoDuplex.LongEdge;

            if (!string.IsNullOrEmpty(printUrl) && !PrintUrl(printUrl))
            {
                new UIAlertView(TouchFactory.Instance.GetResourceString("PrintErrorTitle"),
                                string.Format(TouchFactory.Instance.GetResourceString("PrintUrlError"), printUrl), null,
                                TouchFactory.Instance.GetResourceString("Dismiss"), null).Show();

                return;
            }

            var navContext = iApp.CurrentNavContext.ActiveLayer.NavContext;
            var stack      = PaneManager.Instance.FromNavContext(navContext.NavigatedActivePane, navContext.NavigatedActiveTab) as BaseNavigationController;

            if (stack == null || stack.CurrentView == null)
            {
                return;
            }

            UIViewController controller = TouchFactory.GetNativeObject <UIViewController>(stack.CurrentView, "view");
            UIView           view       = controller.View;

            if (controller is IBrowserView)
            {
                view = view.Subviews.FirstOrDefault(sv => sv is UIWebView);
            }

            if (!(view is UIWebView) || !PrintUrl(((UIWebView)view).Request.Url.AbsoluteString))
            {
                printInfo.JobName = controller.NavigationItem.Title;
                UIPrintInteractionController.SharedPrintController.PrintInfo = printInfo;

                view.ViewPrintFormatter.StartPage = 0;
                UIPrintInteractionController.SharedPrintController.PrintFormatter = view.ViewPrintFormatter;
            }

            UIPrintInteractionCompletionHandler completionHandler = FinishPrinting;

            if (TouchFactory.Instance.LargeFormFactor)
            {
                UIViewController top = ModalManager.GetTopmostViewController(null);

                nfloat barHeight = TouchFactory.Instance.IsLandscape ? UIApplication.SharedApplication.StatusBarFrame.Width
                                        : UIApplication.SharedApplication.StatusBarFrame.Height;

                nfloat centerX = TouchFactory.Instance.IsLandscape ? top.View.Center.Y : top.View.Center.X;

                UIPrintInteractionController.SharedPrintController.PresentFromRectInView(
                    new CoreGraphics.CGRect(centerX, stack.NavigationBar.Frame.Height + barHeight, 1, 1), top.View, true, completionHandler);
            }
            else
            {
                UIPrintInteractionController.SharedPrintController.Present(true, completionHandler);
            }
        }
        public void Print(object sender, EventArgs args)
        {
            UIPrintInteractionController controller = UIPrintInteractionController.SharedPrintController;

            if (controller == null)
            {
                Console.WriteLine("Couldn't get shared UIPrintInteractionController");
                return;
            }

            controller.CutLengthForPaper = delegate(UIPrintInteractionController printController, UIPrintPaper paper) {
                // Create a font with arbitrary size so that you can calculate the approximate
                // font points per screen point for the height of the text.
                UIFont font = textformatter.Font;

                NSString           str        = new NSString(textField.Text);
                UIStringAttributes attributes = new UIStringAttributes();
                attributes.Font = font;
                CGSize size = str.GetSizeUsingAttributes(attributes);

                nfloat approximateFontPointPerScreenPoint = font.PointSize / size.Height;

                // Create a new font using a size  that will fill the width of the paper
                font = SelectFont((float)(paper.PrintableRect.Size.Width * approximateFontPointPerScreenPoint));

                // Calculate the height and width of the text with the final font size
                attributes.Font = font;
                CGSize finalTextSize = str.GetSizeUsingAttributes(attributes);

                // Set the UISimpleTextFormatter font to the font with the size calculated
                textformatter.Font = font;

                // Calculate the margins of the roll. Roll printers may have unprintable areas
                // before and after the cut.  We must add this to our cut length to ensure the
                // printable area has enough room for our text.
                nfloat lengthOfMargins = paper.PaperSize.Height - paper.PrintableRect.Size.Height;

                // The cut length is the width of the text, plus margins, plus some padding
                return(finalTextSize.Width + lengthOfMargins + paper.PrintableRect.Size.Width * PaddingFactor);
            };

            UIPrintInfo printInfo = UIPrintInfo.PrintInfo;

            printInfo.OutputType  = UIPrintInfoOutputType.General;
            printInfo.Orientation = UIPrintInfoOrientation.Landscape;
            printInfo.JobName     = textField.Text;

            textformatter = new UISimpleTextPrintFormatter(textField.Text)
            {
                Color = SelectedColor,
                Font  = SelectFont()
            };

            controller.PrintInfo      = printInfo;
            controller.PrintFormatter = textformatter;

            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
            {
                controller.PresentFromRectInView(printButton.Frame, View, true, PrintingComplete);
            }
            else
            {
                controller.Present(true, PrintingComplete);
            }
        }