/// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// In order to ensure a good user experience, the system requires that the app handle the PrintTaskRequested event within the time specified by PrintTaskRequestedEventArgs.Request.Deadline.
        /// Therefore, we use this handler to only create the print task.
        /// The print settings customization can be done when the print document source is requested.
        /// </summary>
        /// <param name="sender">PrintManager</param>
        /// <param name="e">PrintTaskRequestedEventArgs</param>
        protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequestedArgs =>
            {
                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
                IList <string> displayedOptions             = printDetailedOptions.DisplayedOptions;

                // Choose the printer options to be shown.
                // The order in which the options are appended determines the order in which they appear in the UI
                displayedOptions.Clear();

                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

                // Create a new list option
                PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageRange", "Page Range");
                pageFormat.AddItem("PrintAll", "Print all");
                pageFormat.AddItem("PrintSelection", "Print Selection");
                pageFormat.AddItem("PrintRange", "Print Range");

                // Add the custom option to the option list
                displayedOptions.Add("PageRange");

                // Create new edit option
                PrintCustomTextOptionDetails pageRangeEdit = printDetailedOptions.CreateTextOption("PageRangeEdit", "Range");

                // Register the handler for the option change event
                printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;

                // Register the handler for the PrintTask.Completed event.
                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async(s, args) =>
                {
                    pageRangeEditVisible = false;
                    selectionMode        = false;
                    pageList.Clear();

                    // Notify the user when the print operation fails.
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {
                        await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            LogHelper.Log(LogLevel.Error, "Failed to print.");
                        });
                    }

                    await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        // Restore first page to its default layout.
                        // Undo any changes made by a text selection.
                        ShowContent(null);
                    });
                };

                sourceRequestedArgs.SetSource(printDocumentSource);
            });
        }
Пример #2
0
        private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            //从参数的Request属性中获取与PrintTaskRequest的任务关联
            //创建好打印内容和任务后 在调用Complete方法进行打印
            var deferral = args.Request.GetDeferral();

            // 创建打印任务
            task            = args.Request.CreatePrintTask("Print", OnPrintTaskSourceRequrested);
            task.Completed += PrintTask_Completed;

            PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
            IList <string>         displayedOptions     = printDetailedOptions.DisplayedOptions;
            //displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
            //displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
            //displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

            // Create a new list option
            PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageContent", "Pictures");

            pageFormat.AddItem("PicturesText", "Pictures and text");
            pageFormat.AddItem("PicturesOnly", "Pictures only");
            pageFormat.AddItem("TextOnly", "Text only");

            // Add the custom option to the option list
            displayedOptions.Add("PageContent");

            printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;


            deferral.Complete();
        }
Пример #3
0
        void OnPrintManagerPrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            PrintTask printTask = args.Request.CreatePrintTask("The Tale of Tom Kitten",
                                                               OnPrintTaskSourceRequested);

            // Get PrintTaskOptionDetails for making changes to options
            PrintTaskOptionDetails optionDetails =
                PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);

            // Create the custom item
            PrintCustomItemListOptionDetails pageRange =
                optionDetails.CreateItemListOption("idPrintRange", "Print range");

            pageRange.AddItem("idPrintAll", "Print all pages");
            pageRange.AddItem("idPrintCustom", "Print custom range");

            // Add it to the options
            optionDetails.DisplayedOptions.Add("idPrintRange");

            // Create a page-range edit item also, but this only
            //      comes into play when user selects "Print custom range"
            optionDetails.CreateTextOption("idCustomRangeEdit", "Custom Range");

            // Set a handler for the OptionChanged event
            optionDetails.OptionChanged += OnOptionDetailsOptionChanged;
        }
Пример #4
0
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// </summary>
        /// <param name="sender">PrintManager</param>
        /// <param name="e">PrintTaskRequestedEventArgs </param>
        protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask(JobName, async sourceRequestedArgs =>
            {
                var deferral = sourceRequestedArgs.GetDeferral();

                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
                IList <string> displayedOptions             = printTask.Options.DisplayedOptions;
                displayedOptions.Clear();
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.PrintQuality);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Collation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Duplex);

                // Preset the default value of the printer option
                printTask.Options.MediaSize = PrintMediaSize.NorthAmericaLetter;

                // Create a new list option
                PrintCustomItemListOptionDetails margins = printDetailedOptions.CreateItemListOption("Margins", "Margins");
                margins.AddItem("WideMargins", "Wide", "Each margin is 20% of the paper size", null);
                margins.AddItem("ModerateMargins", "Moderate", "Each margin is 10% of the paper size", null);
                margins.AddItem("NarrowMargins", "Narrow", "Each margin is 5% of the paper size", null);
                // The default is ModerateMargins
                ApplicationContentMarginTop  = 0.1;
                ApplicationContentMarginLeft = 0.1;
                margins.TrySetValue("ModerateMargins");

                // App tells the user some more information about what the feature means.
                margins.Description = "The space between the content of your document and the edge of the paper";

                // Add the custom option to the option list
                displayedOptions.Add("Margins");

                printDetailedOptions.OptionChanged += OnPrintDetailOptionChanged;

                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async(s, args) =>
                {
                    // Notify the user when the print operation fails.
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {
                        await ApplicationPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            //MainPage.Current.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
                            Console.WriteLine("Failed to print.");
                        });
                    }
                    UnregisterForPrinting();
                };

                sourceRequestedArgs.SetSource(printDocumentSource);

                deferral.Complete();
            });
        }
Пример #5
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        async void OnPrintDetailOptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            bool invalidatePreview = false;

            string optionId = args.OptionId as string;

            if (string.IsNullOrEmpty(optionId))
            {
                return;
            }

            if (optionId == "Margins")
            {
                PrintCustomItemListOptionDetails marginsOption = (PrintCustomItemListOptionDetails)sender.Options["Margins"];
                string marginsValue = marginsOption.Value.ToString();

                switch (marginsValue)
                {
                case "WideMargins":
                    ApplicationContentMarginTop  = 0.2;
                    ApplicationContentMarginLeft = 0.2;
                    break;

                case "ModerateMargins":
                    ApplicationContentMarginTop  = 0.1;
                    ApplicationContentMarginLeft = 0.1;
                    break;

                case "NarrowMargins":
                    ApplicationContentMarginTop  = 0.05;
                    ApplicationContentMarginLeft = 0.05;
                    break;
                }

                if (marginsValue == "NarrowMargins")
                {
                    marginsOption.WarningText = "Narrow margins may not be supported by some printers";
                }
                else
                {
                    marginsOption.WarningText = "";
                }

                invalidatePreview = true;
            }

            if (invalidatePreview)
            {
                //await PrintPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                //{
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                {
                    printDocument.InvalidatePreview();
                });
                //});
            }
        }
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// In order to ensure a good user experience, the system requires that the app handle the PrintTaskRequested event within the time specified
        /// by PrintTaskRequestedEventArgs->Request->Deadline.
        /// Therefore, we use this handler to only create the print task.
        /// The print settings customization can be done when the print document source is requested.
        /// </summary>
        /// <param name="sender">The print manager for which a print task request was made.</param>
        /// <param name="e">The print taks request associated arguments.</param>
        protected override void PrintTaskRequested(Windows.Graphics.Printing.PrintManager sender, Windows.Graphics.Printing.PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask("Printing Coloring Page", sourceRequestedArgs =>
            {
                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);

                // Choose the printer options to be shown.
                // The order in which the options are appended determines the order in which they appear in the UI
                printDetailedOptions.DisplayedOptions.Clear();
                printDetailedOptions.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
                printDetailedOptions.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);

                // Create a new list option.
                PrintCustomItemListOptionDetails photoSize = printDetailedOptions.CreateItemListOption("photoSize", "Photo Size");
                photoSize.AddItem("SizeFullPage", "Full Page");
                photoSize.AddItem("Size4x6", "4 x 6 in");
                photoSize.AddItem("Size5x7", "5 x 7 in");
                photoSize.AddItem("Size8x10", "8 x 10 in");

                // Add the custom option to the option list.
                printDetailedOptions.DisplayedOptions.Add("photoSize");

                PrintCustomItemListOptionDetails scaling = printDetailedOptions.CreateItemListOption("scaling", "Scaling");
                scaling.AddItem("ShrinkToFit", "Shrink To Fit");
                scaling.AddItem("Crop", "Crop");

                // Add the custom option to the option list.
                printDetailedOptions.DisplayedOptions.Add("scaling");

                // Set default orientation to landscape.
                printTask.Options.Orientation = PrintOrientation.Landscape;

                // Register for print task option changed notifications.
                printDetailedOptions.OptionChanged += PrintDetailedOptionsOptionChanged;

                // Register for print task Completed notification.
                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async(s, args) =>
                {
                    await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        ClearPageCollection();

                        // Reset image options to default values.
                        this.photoScale = Scaling.ShrinkToFit;
                        this.photoSize  = PhotoSize.SizeFullPage;

                        // Reset the current page description
                        currentPageDescription = null;
                    });
                };

                // Set the document source.
                sourceRequestedArgs.SetSource(printDocumentSource);
            });
        }
Пример #7
0
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// In order to ensure a good user experience, the system requires that the app handle the PrintTaskRequested event within the time specified by PrintTaskRequestedEventArgs.Request.Deadline.
        /// Therefore, we use this handler to only create the print task.
        /// The print settings customization can be done when the print document source is requested.
        /// </summary>
        /// <param name="sender">PrintManager</param>
        /// <param name="e">PrintTaskRequestedEventArgs</param>
        protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask("C# Printing SDK Sample",
                                                  sourceRequestedArgs =>
            {
                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
                IList <string> displayedOptions             = printDetailedOptions.DisplayedOptions;

                // Choose the printer options to be shown.
                // The order in which the options are appended determines the order in which they appear in the UI
                displayedOptions.Clear();

                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

                // Create a new list option

                PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageContent", "Pictures");
                pageFormat.AddItem("PicturesText", "Pictures and text");
                pageFormat.AddItem("PicturesOnly", "Pictures only");
                pageFormat.AddItem("TextOnly", "Text only");

                // Add the custom option to the option list
                displayedOptions.Add("PageContent");

                printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;

                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async(s, args) =>
                {
                    // Notify the user when the print operation fails.
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {
                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            rootPage.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
                        });
                    }
                };

                sourceRequestedArgs.SetSource(printDocumentSource);
            });
        }
Пример #8
0
        private void CreateGraphOptions(PrintTaskOptionDetails printTaskOptions)
        {
            PrintCustomItemListOptionDetails graphSizeOption = printTaskOptions.CreateItemListOption(nameof(GraphSize), _graphSizeOption);

            graphSizeOption.AddItem(nameof(GraphSize.FullPage), _fullPageItem);
            graphSizeOption.AddItem(nameof(GraphSize.Window), _windowSizeItem);
            printTaskOptions.DisplayedOptions.Add(nameof(GraphSize));

            PrintCustomItemListOptionDetails labelLocationOption = printTaskOptions.CreateItemListOption(nameof(LabelLocation), _labelLocationOption);

            labelLocationOption.AddItem(nameof(LabelLocation.TopLeft), _topLeftItem);
            labelLocationOption.AddItem(nameof(LabelLocation.TopRight), _topRightItem);
            labelLocationOption.AddItem(nameof(LabelLocation.BottomLeft), _bottomLeftItem);
            labelLocationOption.AddItem(nameof(LabelLocation.BottomRight), _bottomRightItem);
            labelLocationOption.AddItem(nameof(LabelLocation.None), _noneItem);
            printTaskOptions.DisplayedOptions.Add(nameof(LabelLocation));
        }
Пример #9
0
        protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = e.Request.CreatePrintTask("I Love Notes", sourceRequested => sourceRequested.SetSource(printDocumentSource));
            PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);

            printDetailedOptions.DisplayedOptions.Clear();
            printDetailedOptions.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
            printDetailedOptions.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
            printDetailedOptions.DisplayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

            PrintCustomItemListOptionDetails tagsDisplay = printDetailedOptions.CreateItemListOption("Tags", "Tags");

            tagsDisplay.AddItem("ShowTags", "Show Tags");
            tagsDisplay.AddItem("HideTags", "Hide Tags");

            //printDetailedOptions.DisplayedOptions.Add("PageContent");
            printDetailedOptions.DisplayedOptions.Add("Tags");
            printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;
        }
Пример #10
0
        private void Printmgr_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
        {
            var deferral = args.Request.GetDeferral();

            task            = args.Request.CreatePrintTask("Print", OnPrintTaskSourceRequrested);
            task.Completed += PrintTask_Completed;
            PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
            IList <string>         displayedOptions     = printDetailedOptions.DisplayedOptions;
            // Create a new list option
            PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageContent", "Pictures");

            pageFormat.AddItem("PicturesText", "Pictures and text");
            pageFormat.AddItem("PicturesOnly", "Pictures only");
            pageFormat.AddItem("TextOnly", "Text only");
            // Add the custom option to the option list
            displayedOptions.Add("PageContent");

            printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;


            deferral.Complete();
        }
Пример #11
0
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// </summary>
        /// <param name="sender">PrintManager</param>
        /// <param name="e">PrintTaskRequestedEventArgs </param>
        protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask(JobName, sourceRequestedArgs =>
            {
                var deferral = sourceRequestedArgs.GetDeferral();

                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
                IList <string> displayedOptions             = printTask.Options.DisplayedOptions;
                displayedOptions.Clear();
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.PrintQuality);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Collation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Duplex);

                // Preset the default value of the printer option
                printTask.Options.MediaSize = PrintMediaSize.NorthAmericaLetter;

                // Create a new list option
                PrintCustomItemListOptionDetails margins = printDetailedOptions.CreateItemListOption("Margins", "Margins");

                /*
                 * if (Forms9Patch.OsInfoService.Version >= new Version(10, 0, 17134, 0))
                 * {
                 *  margins.AddItem("WideMargins", "Wide", "Each margin is 20% of the paper size", await wideMarginsIconTask);
                 *  margins.AddItem("ModerateMargins", "Moderate", "Each margin is 10% of the paper size", await moderateMarginsIconTask);
                 *  margins.AddItem("NarrowMargins", "Narrow", "Each margin is 5% of the paper size", await narrowMarginsIconTask);
                 *  // App tells the user some more information about what the feature means.
                 *  margins.Description = "The space between the content of your document and the edge of the paper";
                 * }
                 * else
                 */
                {
                    margins.AddItem("WideMargins", "Wide");
                    margins.AddItem("ModerateMargins", "Moderate");
                    margins.AddItem("NarrowMargins", "Narrow");
                }
                // The default is ModerateMargins
                ApplicationContentMarginTop  = 0.1;
                ApplicationContentMarginLeft = 0.1;
                margins.TrySetValue("ModerateMargins");


                // Add the custom option to the option list
                displayedOptions.Add("Margins");

                printDetailedOptions.OptionChanged += OnPrintDetailOptionChanged;

                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += (s, args) =>
                {
                    Xamarin.Essentials.MainThread.BeginInvokeOnMainThread(() =>
                    {
                        // Notify the user when the print operation fails.
                        if (args.Completion == PrintTaskCompletion.Failed)
                        {
                            using (Toast.Create("Printing Failed", null)) { }
                        }
                        //else if (args.Completion == PrintTaskCompletion.Canceled)
                        //    using (Toast.Create("Printing Cancelled", null)) { }
                        else if (args.Completion == PrintTaskCompletion.Submitted)
                        {
                            using (Toast.Create("Printing ...", "Print job submitted to printer.", TimeSpan.FromSeconds(5))) { }
                        }
                        else if (args.Completion == PrintTaskCompletion.Abandoned)
                        {
                            using (Toast.Create("Printing Abandoned", null)) { }
                        }
                    });
                    UnregisterForPrinting();
                };

                sourceRequestedArgs.SetSource(printDocumentSource);

                deferral.Complete();
            });
        }
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// In order to ensure a good user experience, the system requires that the app handle
        /// the PrintTaskRequested event within the time specified
        /// by PrintTaskRequestedEventArgs->Request->Deadline.
        /// Therefore, we use this handler to only create the print task.
        /// The print settings customization can be done when the print document source is requested.
        /// </summary>
        /// <param name="sender">The print manager for which a print task request was made.</param>
        /// <param name="e">The print taks request associated arguments.</param>
        protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask(Tools.GetResourceString("Printer/PrintingTask"), sourceRequestedArgs =>
            {
                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);

                // Choose the printer options to be shown.
                // The order in which the options are appended determines the order in which they appear in the UI.
                printDetailedOptions.DisplayedOptions.Clear();
                printDetailedOptions.DisplayedOptions.Add(StandardPrintTaskOptions.MediaSize);
                printDetailedOptions.DisplayedOptions.Add(StandardPrintTaskOptions.Copies);

                // Create a new list option.
                PrintCustomItemListOptionDetails photoSize = printDetailedOptions.CreateItemListOption(
                    "photoSize", Tools.GetResourceString("Printer/SizeHeading"));
                photoSize.AddItem("SizeFullPage", Tools.GetResourceString("Printer/SizeFullPage"));
                photoSize.AddItem("Size4x6", Tools.GetResourceString("Printer/Size4x6"));
                photoSize.AddItem("Size5x7", Tools.GetResourceString("Printer/Size5x7"));
                photoSize.AddItem("Size8x10", Tools.GetResourceString("Printer/Size8x10"));

                // Add the custom option to the option list.
                printDetailedOptions.DisplayedOptions.Add("photoSize");

                PrintCustomItemListOptionDetails scaling = printDetailedOptions.CreateItemListOption(
                    "scaling", Tools.GetResourceString("Printer/ScalingHeading"));
                scaling.AddItem("ShrinkToFit", Tools.GetResourceString("Printer/Shrink"));
                scaling.AddItem("Crop", Tools.GetResourceString("Printer/Crop"));

                // Add the custom option to the option list.
                printDetailedOptions.DisplayedOptions.Add("scaling");

                // Set default orientation to landscape.
                printTask.Options.Orientation = PrintOrientation.Landscape;

                // Register for print task option changed notifications.
                printDetailedOptions.OptionChanged += OnPrintDetailedOptionsOptionChanged;

                // Register for print task Completed notification.
                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += async(s, args) =>
                {
                    await ScenarioPage.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        ClearPageCollection();

                        // Reset image options to default values.
                        PhotoScale       = Scaling.ShrinkToFit;
                        PhotoSizeSetting = PhotoSize.SizeFullPage;

                        // Reset the current page description.
                        CurrentPageDescription = null;

                        // Notify the user when the print operation fails.
                        if (args.Completion == PrintTaskCompletion.Failed)
                        {
                            ColoringPage.NotifyUserAsync(Tools.GetResourceString("Printer/ErrorMessage")).ContinueWithoutWaiting();
                        }
                    });
                };

                // Set the document source.
                sourceRequestedArgs.SetSource(PrintDocumentSource);
            });
        }
Пример #13
0
        /// <summary>
        /// This is the event handler for PrintManager.PrintTaskRequested.
        /// In order to ensure a good user experience, the system requires that the app handle the PrintTaskRequested event within the time specified by PrintTaskRequestedEventArgs.Request.Deadline.
        /// Therefore, we use this handler to only create the print task.
        /// The print settings customization can be done when the print document source is requested.
        /// </summary>
        /// <param name="sender">PrintManager</param>
        /// <param name="e">PrintTaskRequestedEventArgs</param>
        protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
        {
            PrintTask printTask = null;

            printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", async sourceRequestedArgs =>
            {
                var deferral = sourceRequestedArgs.GetDeferral();
                PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
                IList <string> displayedOptions             = printDetailedOptions.DisplayedOptions;

                // Choose the printer options to be shown.
                // The order in which the options are appended determines the order in which they appear in the UI
                displayedOptions.Clear();

                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
                displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

                // Create a new list option
                PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageContent", "Pictures");
                pageFormat.AddItem("PicturesText", "Pictures and text");
                pageFormat.AddItem("PicturesOnly", "Pictures only");
                pageFormat.AddItem("TextOnly", "Text only");

                // Add the custom option to the option list
                displayedOptions.Add("PageContent");

                // Create a new toggle option "Show header".
                PrintCustomToggleOptionDetails header = printDetailedOptions.CreateToggleOption("Header", "Show header");

                // App tells the user some more information about what the feature means.
                header.Description = "Display a header on the first page";

                // Set the default value
                header.TrySetValue(showHeader);

                // Add the custom option to the option list
                displayedOptions.Add("Header");

                // Create a new list option
                PrintCustomItemListOptionDetails margins = printDetailedOptions.CreateItemListOption("Margins", "Margins");
                margins.AddItem("WideMargins", "Wide", "Each margin is 20% of the paper size", await wideMarginsIconTask);
                margins.AddItem("ModerateMargins", "Moderate", "Each margin is 10% of the paper size", await moderateMarginsIconTask);
                margins.AddItem("NarrowMargins", "Narrow", "Each margin is 5% of the paper size", await narrowMarginsIconTask);

                // The default is ModerateMargins
                ApplicationContentMarginTop  = 0.1;
                ApplicationContentMarginLeft = 0.1;
                margins.TrySetValue("ModerateMargins");

                // App tells the user some more information about what the feature means.
                margins.Description = "The space between the content of your document and the edge of the paper";

                // Add the custom option to the option list
                displayedOptions.Add("Margins");

                printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;

                // Print Task event handler is invoked when the print job is completed.
                printTask.Completed += (s, args) =>
                {
                    // Notify the user when the print operation fails.
                    if (args.Completion == PrintTaskCompletion.Failed)
                    {
                        MainPage.Current.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
                    }
                };

                sourceRequestedArgs.SetSource(printDocumentSource);

                deferral.Complete();
            });
        }
Пример #14
0
        /// <summary>
        /// This is the event handler for whenever the user makes changes to the options.
        /// In this case, the options of interest are PageContent, Margins and Header.
        /// </summary>
        /// <param name="sender">PrintTaskOptionDetails</param>
        /// <param name="args">PrintTaskOptionChangedEventArgs</param>
        async void printDetailedOptions_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
        {
            bool invalidatePreview = false;

            string optionId = args.OptionId as string;

            if (string.IsNullOrEmpty(optionId))
            {
                return;
            }

            if (optionId == "PageContent")
            {
                invalidatePreview = true;
            }

            if (optionId == "Margins")
            {
                PrintCustomItemListOptionDetails marginsOption = (PrintCustomItemListOptionDetails)sender.Options["Margins"];
                string marginsValue = marginsOption.Value.ToString();

                switch (marginsValue)
                {
                case "WideMargins":
                    ApplicationContentMarginTop  = 0.2;
                    ApplicationContentMarginLeft = 0.2;
                    break;

                case "ModerateMargins":
                    ApplicationContentMarginTop  = 0.1;
                    ApplicationContentMarginLeft = 0.1;
                    break;

                case "NarrowMargins":
                    ApplicationContentMarginTop  = 0.05;
                    ApplicationContentMarginLeft = 0.05;
                    break;
                }

                if (marginsValue == "NarrowMargins")
                {
                    marginsOption.WarningText = "Narrow margins may not be supported by some printers";
                }
                else
                {
                    marginsOption.WarningText = "";
                }

                invalidatePreview = true;
            }

            if (optionId == "Header")
            {
                PrintCustomToggleOptionDetails headerOption = (PrintCustomToggleOptionDetails)sender.Options["Header"];
                showHeader        = (bool)headerOption.Value;
                invalidatePreview = true;
            }

            if (invalidatePreview)
            {
                await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    printDocument.InvalidatePreview();
                });
            }
        }