Ejemplo n.º 1
0
        private static async Task <bool> PrintObjectAsync(NSObject nsObject, PrintJobConfiguration printJobConfiguration)
        {
            bool result = false;

            UIPrintInteractionController printer = await PopulateCommonPrinterDetailsAsync(printJobConfiguration);

            if (!(printer is null))
            {
                printer.PrintingItem = nsObject;
                printer.Present(true, PrintCompletion);
                printer.Dispose();
                result = true;
            }

            return(result);
        }
Ejemplo n.º 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");
                }
            });
        }
        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);
        }
        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);
            }
        }
Ejemplo n.º 5
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            float width = (float)View.Frame.Width;

            float height = (float)View.Frame.Height;

            if (orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight)
            {
                width  = (float)View.Frame.Height;
                height = (float)View.Frame.Width;
            }

            WritePadAPI.setRecoFlag(WritePadAPI.recoGetFlags(), true, WritePadAPI.FLAG_USERDICT);
            WritePadAPI.setRecoFlag(WritePadAPI.recoGetFlags(), true, WritePadAPI.FLAG_ANALYZER);
            WritePadAPI.setRecoFlag(WritePadAPI.recoGetFlags(), true, WritePadAPI.FLAG_CORRECTOR);

            inkView                  = new InkView();
            inkView.Frame            = new CGRect(button_gap, header_height + button_gap, width - button_gap * 2, height);
            inkView.ContentMode      = UIViewContentMode.Redraw;
            inkView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
            View.Add(inkView);

            formattedText           = new UITextView();
            formattedText.Frame     = new CGRect(0, UIScreen.MainScreen.Bounds.Height * 0.02, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
            formattedText.TextColor = UIColor.Black;
            formattedText.Font      = UIFont.FromName("Baskerville", 28f);
            formattedText.Hidden    = true;
            View.Add(formattedText);

            inkView.OnReturnGesture += () => recognized_text = inkView.Recognize(true);
            inkView.OnReturnGesture += () => inkView.cleanView(true);
            inkView.OnCutGesture    += () => inkView.cleanView(true);
            inkView.OnUndoGesture   += () => inkView.cleanView(true);

            float x = (width - (button_width * 5 + button_gap * 5)) / 2;

            switchViewButton       = new UIButton(UIButtonType.Custom);
            switchViewButton.Frame = new CGRect(x, height - bottom_gap, button_width, button_height);
            switchViewButton.SetTitle("Switch View", UIControlState.Normal);
            switchViewButton.Font = UIFont.SystemFontOfSize(button_font_size);
            switchViewButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            switchViewButton.SetTitleColor(UIColor.White, UIControlState.Highlighted);
            switchViewButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            switchViewButton.TouchUpInside   += (object sender, EventArgs e) => {
                if (inkView.getNumPaths() > 0)
                {
                    recognized_text = inkView.Recognize(false);
                }
                if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                {
                    formattedText.Text   = recognized_text;
                    formattedText.Text   = FormatLatex(recognized_text);
                    formattedText.Hidden = false;
                    inkView.Hidden       = true;
                    displayingFormatted  = true;
                    printButton.SetTitle("Print", UIControlState.Normal);
                }
                else if (displayingFormatted)
                {
                    formattedText.Hidden = true;
                    inkView.Hidden       = false;
                    displayingFormatted  = false;
                    printButton.SetTitle("Save", UIControlState.Normal);
                }
            };
            View.Add(switchViewButton);
            x                += (button_gap + button_width);
            clearButton       = new UIButton(UIButtonType.Custom);
            clearButton.Frame = new CGRect(x, height - bottom_gap, button_width, button_height);
            clearButton.SetTitle("Clear", UIControlState.Normal);
            clearButton.Font = UIFont.SystemFontOfSize(button_font_size);
            clearButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            clearButton.SetTitleColor(UIColor.White, UIControlState.Highlighted);
            clearButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            clearButton.TouchUpInside   += (object sender, EventArgs e) => {
                inkView.cleanView(true);
            };
            View.Add(clearButton);

            x += (button_gap + button_width);
            languageButton       = new UIButton(UIButtonType.Custom);
            languageButton.Frame = new CGRect(x, height - bottom_gap, button_width, button_height);
            languageButton.SetTitle("Language", UIControlState.Normal);
            languageButton.Font = UIFont.SystemFontOfSize(button_font_size);
            languageButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            languageButton.SetTitleColor(UIColor.White, UIControlState.Highlighted);
            languageButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            languageButton.TouchUpInside   += (object sender, EventArgs e) => {
                var actionSheet = new UIActionSheet("Select Language:", (IUIActionSheetDelegate)null, "Cancel", null,
                                                    new [] { "English", "English UK", "German", "French", "Spanish", "Portuguese", "Brazilian", "Dutch", "Italian", "Finnish", "Swedish", "Norwegian", "Danish", "Indonesian" });
                actionSheet.Clicked += delegate(object a, UIButtonEventArgs b) {
                    switch (b.ButtonIndex)
                    {
                    case 0:
                        WritePadAPI.language = WritePadAPI.LanguageType.en;
                        from_language        = "en";
                        break;

                    case 1:
                        WritePadAPI.language = WritePadAPI.LanguageType.en_uk;
                        from_language        = "en";
                        break;

                    case 2:
                        WritePadAPI.language = WritePadAPI.LanguageType.de;
                        from_language        = "de";
                        break;

                    case 3:
                        WritePadAPI.language = WritePadAPI.LanguageType.fr;
                        from_language        = "fr";
                        break;

                    case 4:
                        WritePadAPI.language = WritePadAPI.LanguageType.es;
                        from_language        = "es";
                        break;

                    case 5:
                        WritePadAPI.language = WritePadAPI.LanguageType.pt_PT;
                        from_language        = "pt";
                        break;

                    case 6:
                        WritePadAPI.language = WritePadAPI.LanguageType.pt_BR;
                        from_language        = "pt";
                        break;

                    case 7:
                        WritePadAPI.language = WritePadAPI.LanguageType.nl;
                        from_language        = "nl";
                        break;

                    case 8:
                        WritePadAPI.language = WritePadAPI.LanguageType.it;
                        from_language        = "it";
                        break;

                    case 9:
                        WritePadAPI.language = WritePadAPI.LanguageType.fi;
                        from_language        = "fi";
                        break;

                    case 10:
                        WritePadAPI.language = WritePadAPI.LanguageType.sv;
                        from_language        = "sv";
                        break;

                    case 11:
                        WritePadAPI.language = WritePadAPI.LanguageType.nb;
                        from_language        = "nb";
                        break;

                    case 12:
                        WritePadAPI.language = WritePadAPI.LanguageType.da;
                        from_language        = "da";
                        break;

                    case 13:
                        WritePadAPI.language = WritePadAPI.LanguageType.id;
                        from_language        = "id";
                        break;
                    }
                    WritePadAPI.recoFree();
                    WritePadAPI.recoInit();
                    WritePadAPI.initializeFlags();
                };
                actionSheet.ShowInView(View);
            };
            View.Add(languageButton);

            x += (button_gap + button_width);
            translateButton       = new UIButton(UIButtonType.Custom);
            translateButton.Frame = new CGRect(x, height - bottom_gap, button_width, button_height);
            translateButton.SetTitle("Translate", UIControlState.Normal);
            translateButton.Font = UIFont.SystemFontOfSize(button_font_size);
            translateButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            translateButton.SetTitleColor(UIColor.White, UIControlState.Highlighted);
            translateButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            translateButton.TouchUpInside   += (object sender, EventArgs e) => {
                var translateActionSheet = new UIActionSheet("Select Language:", (IUIActionSheetDelegate)null, "Cancel", null,
                                                             new [] { "English", "English UK", "German", "French", "Spanish", "Portuguese", "Brazilian", "Dutch", "Italian", "Finnish", "Swedish", "Norwegian", "Danish", "Indonesian" });
                translateActionSheet.Clicked += delegate(object a, UIButtonEventArgs b) {
                    switch (b.ButtonIndex)
                    {
                    case 0:
                        to_language     = "en";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 1:
                        to_language     = "en";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 2:
                        to_language     = "de";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 3:
                        to_language     = "fr";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 4:
                        to_language     = "es";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 5:
                        to_language     = "pt";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 6:
                        to_language     = "pt";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 7:
                        to_language     = "nl";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 8:
                        to_language     = "it";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 9:
                        to_language     = "fi";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 10:
                        to_language     = "sv";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 11:
                        to_language     = "nb";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 12:
                        to_language     = "da";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;

                    case 13:
                        to_language     = "id";
                        recognized_text = inkView.Recognize(false);
                        if (!displayingFormatted && recognized_text != "" && recognized_text != "*Error*")
                        {
                            Translate(FormatLatex(recognized_text));
                        }
                        break;
                    }
                    WritePadAPI.recoFree();
                    WritePadAPI.recoInit();
                    WritePadAPI.initializeFlags();
                };
                translateActionSheet.ShowInView(View);
            };
            View.Add(translateButton);

            x                += (button_gap + button_width);
            printButton       = new UIButton(UIButtonType.Custom);
            printButton.Frame = new CGRect(x, height - bottom_gap, button_width, button_height);
            printButton.SetTitle("Save", UIControlState.Normal);
            printButton.Font = UIFont.SystemFontOfSize(button_font_size);
            printButton.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            printButton.SetTitleColor(UIColor.White, UIControlState.Highlighted);
            printButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
            printButton.TouchUpInside   += (object sender, EventArgs e) => {
                if (displayingFormatted && formattedText.Text != "")
                {
                    var printInfo = UIPrintInfo.PrintInfo;

                    printInfo.OutputType = UIPrintInfoOutputType.General;
                    printInfo.JobName    = "Printing Tidied-Up Text";

                    UIPrintInteractionController printer = UIPrintInteractionController.SharedPrintController;

                    printer.PrintInfo      = printInfo;
                    printer.PrintingItem   = new NSString(formattedText.Text);
                    printer.ShowsPageRange = true;

                    printer.Present(true, PrintingCompleted);
                }
                else if (!displayingFormatted)
                {
                    switchViewButton.Hidden = true;
                    clearButton.Hidden      = true;
                    languageButton.Hidden   = true;
                    translateButton.Hidden  = true;
                    printButton.Hidden      = true;
                    UIImage data = UIScreen.MainScreen.Capture();
                    data.SaveToPhotosAlbum(null);
                    switchViewButton.Hidden = false;
                    clearButton.Hidden      = false;
                    languageButton.Hidden   = false;
                    translateButton.Hidden  = false;
                    printButton.Hidden      = false;
                }
            };
            View.Add(printButton);

            /*x += (button_gap + button_width);
             * rewriteButton = new UIButton (UIButtonType.Custom);
             * rewriteButton.Frame = new CGRect (x, height - bottom_gap, button_width, button_height);
             * rewriteButton.SetTitle ("Rewrite", UIControlState.Normal);
             * rewriteButton.Font = UIFont.SystemFontOfSize (button_font_size);
             * rewriteButton.SetTitleColor (UIColor.Blue, UIControlState.Normal);
             * rewriteButton.SetTitleColor (UIColor.White, UIControlState.Highlighted);
             * rewriteButton.AutoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin;
             * rewriteButton.TouchUpInside += (object sender, EventArgs e) => {
             *      inkView.cleanView (true);
             *      if (translated_text != "")
             *      {
             *              formattedText.Hidden = true;
             *              inkView.Hidden = false;
             *              displayingFormatted = false;
             *              inkView.writeText (translated_text, (int)(width - button_gap * 2), (int)height);
             *      }
             * };
             * View.Add (rewriteButton);*/
        }