/// <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(); }); }
/// <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">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(); }); }