Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public override void ViewDidAppear(bool animated)
        {
            this.appDelegate.tablePDFSearch = 0;
            UIBarButtonItem searchText = new UIBarButtonItem(UIBarButtonSystemItem.Search, (object sender, EventArgs e) =>
            {
                //searches for matching text inside pdf
                this.searchForText.Placeholder       = "Search...";
                this.searchForText.SpellCheckingType = UITextSpellCheckingType.No;
                this.searchForText.SearchBarStyle    = UISearchBarStyle.Prominent;
                this.searchForText.BarStyle          = UIBarStyle.Default;
                this.searchForText.ShowsCancelButton = true;
                this.NavigationItem.TitleView        = this.searchForText;

                this.searchForText.BecomeFirstResponder();
                this.searchForText.CancelButtonClicked += (object sender_2, EventArgs e_2) => {
                    //replaces the navigation item with the title text
                    this.searchForText.ResignFirstResponder();
                    this.NavigationItem.TitleView = null;
                    this.NavigationItem.Title     = "Viewer";
                };
            });

            UIBarButtonItem pageMode = new UIBarButtonItem("Page Mode", UIBarButtonItemStyle.Done, (object sender, EventArgs e) => {
                //page count is adjusted
                //creates a small table which adjusts the number of pages to render

                //an alert controller is best for this
                if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
                {
                    UIAlertController pageModeController = UIAlertController.Create("", "", UIAlertControllerStyle.ActionSheet);

                    UIAlertAction onePage = UIAlertAction.Create("Single Page", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        this.pdfWebReader.PaginationMode = UIWebPaginationMode.TopToBottom;
                        pageModeController.Dispose();
                    });

                    UIAlertAction continousPages = UIAlertAction.Create("Continuous", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        this.pdfWebReader.PaginationMode = UIWebPaginationMode.Unpaginated;
                        pageModeController.Dispose();
                    });

                    UIAlertAction denied = UIAlertAction.Create("Never Mind", UIAlertActionStyle.Cancel, (UIAlertAction obj) =>
                    {
                        pageModeController.Dispose();
                    });

                    pageModeController.AddAction(onePage);
                    pageModeController.AddAction(continousPages);
                    pageModeController.AddAction(denied);

                    if (this.PresentedViewController == null)
                    {
                        this.PresentViewController(pageModeController, true, null);
                    }
                    else
                    {
                        this.PresentedViewController.DismissViewController(true, () =>
                        {
                            this.PresentedViewController.Dispose();
                            this.PresentViewController(pageModeController, true, null);
                        });
                    }
                }
                else
                {
                    UIAlertController pageModeController = UIAlertController.Create("", "", UIAlertControllerStyle.Alert);

                    UIAlertAction onePage = UIAlertAction.Create("Single Page", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        this.pdfWebReader.PaginationMode = UIWebPaginationMode.TopToBottom;
                        pageModeController.Dispose();
                    });

                    UIAlertAction continousPages = UIAlertAction.Create("Full Screen", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        this.pdfWebReader.PaginationMode = UIWebPaginationMode.Unpaginated;
                        pageModeController.Dispose();
                    });

                    UIAlertAction denied = UIAlertAction.Create("Never Mind", UIAlertActionStyle.Cancel, (UIAlertAction obj) =>
                    {
                        pageModeController.Dispose();
                    });

                    pageModeController.AddAction(onePage);
                    pageModeController.AddAction(continousPages);
                    pageModeController.AddAction(denied);

                    if (this.PresentedViewController == null)
                    {
                        this.PresentViewController(pageModeController, true, null);
                    }
                    else
                    {
                        this.PresentedViewController.DismissViewController(true, () =>
                        {
                            this.PresentedViewController.Dispose();
                            this.PresentViewController(pageModeController, true, null);
                        });
                    }
                }
            });
            UIBarButtonItem share = new UIBarButtonItem(UIBarButtonSystemItem.Action, (sender, e) => {
                //introduces an action sheet. with the following options:

                /*
                 * allows the user to share file (which creates an activity controller where the user can choose how to share the file)
                 * allows updating to the cloud
                 * allows an option to open the document in another application of choice (UIDocumentInterationController)
                 * Print the document (In a nearby printer via AirPrint)
                 *
                 */
                if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
                {
                    UIAlertController shareModeController = UIAlertController.Create("", "", UIAlertControllerStyle.ActionSheet);

                    UIAlertAction shareFile = UIAlertAction.Create("Share File", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                        var filePath  = Path.Combine(directory, this.appDelegate.pdfString);

                        NSUrl pdfURL   = NSUrl.FromFilename(filePath);
                        NSData pdfData = NSData.FromUrl(pdfURL);

                        NSObject[] dataToShare            = new NSObject[] { pdfURL };
                        UIActivityViewController activity = new UIActivityViewController(dataToShare, null);

                        //UIActivityType[] activityTypes = new UIActivityType[] { UIActivityType.Print, UIActivityType.SaveToCameraRoll };


                        if (this.PresentedViewController == null)
                        {
                            this.PresentViewController(activity, true, null);
                        }
                        else
                        {
                            this.PresentedViewController.DismissViewController(true, () => {
                                this.PresentedViewController.Dispose();
                                this.PresentViewController(activity, true, null);
                            });
                        }
                        shareModeController.Dispose();
                    });

                    UIAlertAction printDocument = UIAlertAction.Create("Print Document", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                        var filePath  = Path.Combine(directory, this.appDelegate.pdfString);

                        NSUrl pdfURL                = NSUrl.FromFilename(filePath);
                        var printInformation        = UIPrintInfo.PrintInfo;
                        printInformation.OutputType = UIPrintInfoOutputType.General;
                        printInformation.JobName    = "Print Job";

                        var print            = UIPrintInteractionController.SharedPrintController;
                        print.PrintInfo      = printInformation;
                        print.PrintFormatter = this.pdfWebReader.ViewPrintFormatter;
                        print.PrintingItem   = pdfURL;
                        print.ShowsPageRange = true;

                        if (UIPrintInteractionController.CanPrint(pdfURL) == true)
                        {
                            print.Present(true, (UIPrintInteractionController printInteractionController, bool completed, NSError error) =>
                            {
                                if (error != null)
                                {
                                    print.Dismiss(true);
                                    errorPrinting("Print Error!", "Cannot print your document. Check if your printer is connected");
                                }
                                else
                                {
                                    Console.WriteLine("Printer success");
                                }
                            });
                        }
                        else
                        {
                            errorPrinting("Print Error!", "Cannot print your document. Check if your printer is connected");
                        }
                        shareModeController.Dispose();
                    });

                    UIAlertAction denied = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (UIAlertAction obj) =>
                    {
                        shareModeController.Dispose();
                    });

                    shareModeController.AddAction(shareFile);
                    shareModeController.AddAction(printDocument);
                    shareModeController.AddAction(denied);

                    if (this.PresentedViewController == null)
                    {
                        this.PresentViewController(shareModeController, true, null);
                    }
                    else
                    {
                        this.PresentedViewController.DismissViewController(true, () =>
                        {
                            this.PresentedViewController.Dispose();
                            this.PresentViewController(shareModeController, true, null);
                        });
                    }
                }
                else
                {
                    UIAlertController shareModeController = UIAlertController.Create("", "", UIAlertControllerStyle.Alert);

                    UIAlertAction shareFile = UIAlertAction.Create("Share File", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                        var filePath  = Path.Combine(directory, this.appDelegate.pdfString);

                        NSUrl pdfURL   = NSUrl.FromFilename(filePath);
                        NSData pdfData = NSData.FromUrl(pdfURL);

                        NSObject[] dataToShare            = new NSObject[] { pdfURL };
                        UIActivityViewController activity = new UIActivityViewController(dataToShare, null);

                        //UIActivityType[] activityTypes = new UIActivityType[] { UIActivityType.Print, UIActivityType.SaveToCameraRoll };


                        if (this.PresentedViewController == null)
                        {
                            this.PresentViewController(activity, true, null);
                        }
                        else
                        {
                            this.PresentedViewController.DismissViewController(true, () =>
                            {
                                this.PresentedViewController.Dispose();
                                this.PresentViewController(activity, true, null);
                            });
                        }
                        shareModeController.Dispose();
                    });

                    UIAlertAction printDocument = UIAlertAction.Create("Print Document", UIAlertActionStyle.Default, (UIAlertAction obj) =>
                    {
                        var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                        var filePath  = Path.Combine(directory, this.appDelegate.pdfString);

                        NSUrl pdfURL = NSUrl.FromFilename(filePath);

                        var printInformation        = UIPrintInfo.PrintInfo;
                        printInformation.OutputType = UIPrintInfoOutputType.General;
                        printInformation.JobName    = "Print Job";

                        var print            = UIPrintInteractionController.SharedPrintController;
                        print.PrintInfo      = printInformation;
                        print.PrintFormatter = this.pdfWebReader.ViewPrintFormatter;
                        print.PrintingItem   = pdfURL;

                        print.ShowsPageRange = true;
                        print.Present(true, (UIPrintInteractionController printInteractionController, bool completed, NSError error) =>
                        {
                            if (error != null)
                            {
                                print.Dismiss(true);
                                errorPrinting("Print Error!", "Cannot print your document. Check if your printer is connected");
                            }
                            else
                            {
                                Console.WriteLine("Printer success");
                            }
                        });

                        shareModeController.Dispose();
                    });

                    UIAlertAction denied = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (UIAlertAction obj) =>
                    {
                        shareModeController.Dispose();
                    });

                    shareModeController.AddAction(shareFile);
                    shareModeController.AddAction(printDocument);
                    shareModeController.AddAction(denied);

                    if (this.PresentedViewController == null)
                    {
                        this.PresentViewController(shareModeController, true, null);
                    }
                    else
                    {
                        this.PresentedViewController.DismissViewController(true, () =>
                        {
                            this.PresentedViewController.Dispose();
                            this.PresentViewController(shareModeController, true, null);
                        });
                    }
                }
            });
            UIBarButtonItem writeComment = new UIBarButtonItem("Comment", UIBarButtonItemStyle.Done, (sender, e) => {
                //adjusts the toolbar items with drawing items used for drawing items on the PDF view controller
            });

            this.NavigationController.SetToolbarHidden(false, true);
            UIBarButtonItem space_1 = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);
            UIBarButtonItem space_2 = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace, null);

            this.NavigationController.Toolbar.Items = new UIBarButtonItem[] { searchText, space_1, space_2, share };
        }