/// <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); }); }
/// <summary> /// This is the event handler for PrintManager option changed. /// </summary> /// <param name="sender">PrintTaskOptionDetails</param> /// <param name="args">PrintTaskOptionChangedEventArgs </param> private static async void PrintDetailedOptions_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args) { bool invalidatePreview = false; string optionId = args.OptionId as string; if (string.IsNullOrEmpty(optionId)) { return; } if (optionId == "HeaderText") { PrintCustomTextOptionDetails headerText = (PrintCustomTextOptionDetails)sender.Options["HeaderText"]; _headerText = headerText.Value.ToString(); invalidatePreview = true; } if (optionId == "FooterText") { PrintCustomTextOptionDetails footerText = (PrintCustomTextOptionDetails)sender.Options["FooterText"]; _footerText = footerText.Value.ToString(); invalidatePreview = true; } if (optionId == "LeftMargin") { PrintCustomTextOptionDetails leftMargin = (PrintCustomTextOptionDetails)sender.Options["LeftMargin"]; var leftMarginValueConverterArg = double.TryParse(leftMargin.Value.ToString(), out var leftMarginValue); if (leftMarginValue > 50 || leftMarginValue < 0 || !leftMarginValueConverterArg) { leftMargin.ErrorText = _resourceLoader.GetString("Print_ErrorMsg_ValueOutOfRange"); return; } else if (Math.Round(leftMarginValue, 1) != leftMarginValue) { leftMargin.ErrorText = _resourceLoader.GetString("Print_ErrorMsg_DecimalOutOfRange"); return; } leftMargin.ErrorText = string.Empty; _applicationContentMarginLeft = (Math.Round(leftMarginValue / 100, 3)); invalidatePreview = true; } if (optionId == "TopMargin") { PrintCustomTextOptionDetails topMargin = (PrintCustomTextOptionDetails)sender.Options["TopMargin"]; var topMarginValueConverterArg = double.TryParse(topMargin.Value.ToString(), out var topMarginValue); if (topMarginValue > 50 || topMarginValue < 0 || !topMarginValueConverterArg) { topMargin.ErrorText = _resourceLoader.GetString("Print_ErrorMsg_ValueOutOfRange"); return; } else if (Math.Round(topMarginValue, 1) != topMarginValue) { topMargin.ErrorText = _resourceLoader.GetString("Print_ErrorMsg_DecimalOutOfRange"); return; } topMargin.ErrorText = string.Empty; _applicationContentMarginTop = (Math.Round(topMarginValue / 100, 3)); invalidatePreview = true; } if (invalidatePreview) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { _printDocument.InvalidatePreview(); }); } }
/// <summary> /// This is the event handler for PrintManager.PrintTaskRequested. /// </summary> /// <param name="sender">PrintManager</param> /// <param name="e">PrintTaskRequestedEventArgs </param> private static void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e) { PrintTask printTask = null; printTask = e.Request.CreatePrintTask("Notepads", sourceRequestedArgs => { var deferral = sourceRequestedArgs.GetDeferral(); PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options); IList <string> displayedOptions = printTask.Options.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(StandardPrintTaskOptions.Orientation); displayedOptions.Add(StandardPrintTaskOptions.Copies); displayedOptions.Add(StandardPrintTaskOptions.MediaSize); displayedOptions.Add(StandardPrintTaskOptions.InputBin); // Add Margin setting in % options PrintCustomTextOptionDetails leftMargin = printDetailedOptions.CreateTextOption("LeftMargin", _resourceLoader.GetString("Print_LeftMarginEntry_Title")); PrintCustomTextOptionDetails topMargin = printDetailedOptions.CreateTextOption("TopMargin", _resourceLoader.GetString("Print_TopMarginEntry_Title")); leftMargin.Description = topMargin.Description = _resourceLoader.GetString("Print_MarginEntry_Description"); leftMargin.TrySetValue(Math.Round(100 * _applicationContentMarginLeft, 1).ToString()); topMargin.TrySetValue(Math.Round(100 * _applicationContentMarginTop, 1).ToString()); displayedOptions.Add("LeftMargin"); displayedOptions.Add("TopMargin"); // Add Header and Footer text options PrintCustomTextOptionDetails headerText = printDetailedOptions.CreateTextOption("HeaderText", _resourceLoader.GetString("Print_HeaderEntry_Title")); PrintCustomTextOptionDetails footerText = printDetailedOptions.CreateTextOption("FooterText", _resourceLoader.GetString("Print_FooterEntry_Title")); headerText.TrySetValue(_headerText); footerText.TrySetValue(_footerText); displayedOptions.Add("HeaderText"); displayedOptions.Add("FooterText"); displayedOptions.Add(StandardPrintTaskOptions.CustomPageRanges); displayedOptions.Add(StandardPrintTaskOptions.Duplex); displayedOptions.Add(StandardPrintTaskOptions.Collation); //displayedOptions.Add(StandardPrintTaskOptions.NUp); displayedOptions.Add(StandardPrintTaskOptions.MediaType); displayedOptions.Add(StandardPrintTaskOptions.Bordering); //displayedOptions.Add(StandardPrintTaskOptions.ColorMode); displayedOptions.Add(StandardPrintTaskOptions.PrintQuality); displayedOptions.Add(StandardPrintTaskOptions.HolePunch); displayedOptions.Add(StandardPrintTaskOptions.Staple); // Preset the default value of the printer option printTask.Options.MediaSize = PrintMediaSize.Default; 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 CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { NotificationCenter.Instance.PostNotification(_resourceLoader.GetString("Print_NotificationMsg_PrintFailed"), 1500); }); } }; sourceRequestedArgs.SetSource(_printDocumentSource); deferral.Complete(); }); }