/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event Arguments</param> protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { // Clear the cache of preview pages. PrintPreviewPages.Clear(); // Clear the print canvas of preview pages. PrintCanvas.Children.Clear(); // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed. RichTextBlockOverflow lastRTBOOnPage; // Get the PrintTaskOptions. PrintTaskOptions printingOptions = e.PrintTaskOptions; // Get the page description to deterimine how big the page is. PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); // We know there is at least one page to be printed. passing null as the first parameter to // AddOnePrintPreviewPage tells the function to add the first page. lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription); // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview // page has extra content. while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Visibility.Visible) { lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription); } PreviewPagesCreated?.Invoke(PrintPreviewPages, EventArgs.Empty); PrintDocument printDoc = sender as PrintDocument; // Report the number of preview pages created. printDoc.SetPreviewPageCount(PrintPreviewPages.Count, PreviewPageCountType.Intermediate); }
protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { try { printPreviewPages.Clear(); PrintCanvas.Children.Clear(); } catch { } RichTextBlockOverflow lastRTBOOnPage; PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription); while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible) { lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription); } if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate); }
/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event </param> public virtual async void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { var paperSize = e.PrintTaskOptions.GetPageDescription(0).PageSize; System.Diagnostics.Debug.WriteLine("CreatePrintPreviewPages: {" + paperSize.Width + "," + paperSize.Height + "}"); //lock (printPreviewPages) await _semaphoreSlim.WaitAsync(); { // Clear the cache of preview pages printPreviewPages.Clear(); // Clear the print canvas of preview pages PrintCanvas.Children.Clear(); // Get the PrintTaskOptions PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); // Get the page description to deterimine how big the page is PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); if (await GeneratePagesAsync(pageDescription) is List <UIElement> pages) { foreach (var page in pages) { PrintCanvas.Children.Add(page); } PrintCanvas.InvalidateMeasure(); PrintCanvas.UpdateLayout(); await Task.Delay(1000); printPreviewPages.AddRange(pages); await Task.Delay(1000); } if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; // Report the number of preview pages created printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate); } _semaphoreSlim.Release(); }
/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event Arguments</param> protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e) // Required 2. called { lock (_printPreviewPages) { // Clear the cache of preview pages _printPreviewPages.Clear(); // Clear the print canvas of preview pages //PrintCanvas?.Children.Clear(); // This variable keeps track of the last RichTextBlockOverflow element that was added to a page which will be printed //RichTextBlockOverflow lastRTBOOnPage; // Get the PrintTaskOptions PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); // Get the page description to determine how big the page is PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); // We know there is at least one page to be printed. passing null as the first parameter to // AddOnePrintPreviewPage tells the function to add the first page. //lastRTBOOnPage = AddOnePrintPreviewPage(null, pageDescription); AddOnePrintPreviewPage(null, pageDescription); // We know there are more pages to be added as long as the last RichTextBoxOverflow added to a print preview // page has extra content //while (lastRTBOOnPage.HasOverflowContent && lastRTBOOnPage.Visibility == Windows.UI.Xaml.Visibility.Visible) //{ // lastRTBOOnPage = AddOnePrintPreviewPage(lastRTBOOnPage, pageDescription); //} if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(_printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; // Report the number of preview pages created printDoc.SetPreviewPageCount(_printPreviewPages.Count, PreviewPageCountType.Intermediate); } }
/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event Arguments</param> protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { lock (printPreviewPages) { // Clear the cache of preview pages printPreviewPages.Clear(); // Clear the print canvas of preview pages PrintCanvas.Children.Clear(); // Get the PrintTaskOptions PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); // Get the page description to deterimine how big the page is PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); if (print_Page == 1) { // setup print page 1 AddOnePrintPreviewPage(1, pageDescription, ScoutName, MeritBadge); } if (print_Page == 2) { // setup print page 2 AddOnePrintPreviewPage(2, pageDescription, ScoutName, MeritBadge); } if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; // Report the number of preview pages created printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate); } }
/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event </param> public virtual async void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { var paperSize = e.PrintTaskOptions.GetPageDescription(0).PageSize; System.Diagnostics.Debug.WriteLine("CreatePrintPreviewPages: {" + paperSize.Width + "," + paperSize.Height + "}"); //lock (printPreviewPages) await _semaphoreSlim.WaitAsync(); try { // Clear the cache of preview pages printPreviewPages.Clear(); // Clear the print canvas of preview pages PrintCanvas.Children.Clear(); // Get the PrintTaskOptions PrintTaskOptions printingOptions = ((PrintTaskOptions)e.PrintTaskOptions); // Get the page description to deterimine how big the page is PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); if (await GeneratePagesAsync(pageDescription) is List <UIElement> pages) { foreach (var page in pages) { PrintCanvas.Children.Add(page); } PrintCanvas.InvalidateMeasure(); PrintCanvas.UpdateLayout(); await Task.Delay(1000); printPreviewPages.AddRange(pages); await Task.Delay(1000); } if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; // Report the number of preview pages created printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate); } catch (Exception pve) { P42.Utils.BreadCrumbs.AddException(pve, GetType()); if (pve.Message.Contains("The RPC server is unavailable", StringComparison.OrdinalIgnoreCase)) { using (var toast = Forms9Patch.Toast.Create("The RPC server is unavailable", "Windows failed trying to setup the print preview because it could not connect to its RPC server. There are three basic potential causes for this error message. Either the RPC service is not running, there are issues with the network, or some important registry entries that control the RPC service have been corrupted. In Windows 10, the most common cause for the error is that the RPC service is simply not running. <a id='link' href='https://www.techjunkie.com/rpc-server-is-unavailable/'>Click here to learn more about how to fix this.</a>")) { toast.ActionTagTapped += On_RpcUnavailable_Toast_ActionTagTapped; } } else { using (Forms9Patch.Toast.Create("Cannot print", "Windows failed trying to setup the print preview. Below is some information from Windows about the failure. \n\n" + pve.Message)) { } } } finally { _semaphoreSlim.Release(); } }
/// <summary> /// This is the event handler for PrintDocument.Paginate. It creates print preview pages for the app. /// </summary> /// <param name="sender">PrintDocument</param> /// <param name="e">Paginate Event Arguments</param> protected virtual void CreatePrintPreviewPages(object sender, PaginateEventArgs e) { lock (printPreviewPages) { // Clear the cache of preview pages printPreviewPages.Clear(); // Clear the print canvas of preview pages PrintCanvas.Children.Clear(); // Get the PrintTaskOptions PrintTaskOptions printingOptions = e.PrintTaskOptions; // Get the page description to determine how big the page is PrintPageDescription pageDescription = printingOptions.GetPageDescription(0); var pageWidth = pageDescription.PageSize.Width; var pageHeight = pageDescription.PageSize.Height; // If the ImageableRect is smaller than the app provided margins use the ImageableRect double marginWidth = Math.Max(pageDescription.PageSize.Width - pageDescription.ImageableRect.Width, _printModel.HorizontalMargin * 2); double marginHeight = Math.Max(pageDescription.PageSize.Height - pageDescription.ImageableRect.Height, _printModel.VerticalMargin * 2); // Set-up "printable area" on the "paper" var printableAreaWidth = pageWidth - marginWidth; var printableAreaHeight = pageHeight - marginHeight; int charsPerPage = FontMapPrintPage.CalculateGlyphsPerPage( new Size(Math.Floor(printableAreaWidth), Math.Floor(printableAreaHeight)), _printModel); bool hasMore = true; int currentPage = _printModel.FirstPage - 1; while (hasMore && printPreviewPages.Count < _printModel.PagesToPrint) { var page = new FontMapPrintPage(_printModel, fontMap.CharGrid.ItemTemplate) { Width = pageWidth, Height = pageHeight }; Grid printableArea = page.PrintableArea; // Set-up "printable area" on the "paper" printableArea.Width = printableAreaWidth; printableArea.Height = printableAreaHeight; // Layout page hasMore = page.AddCharacters(currentPage, charsPerPage, _printModel.Characters); currentPage++; // Add the (newly created) page to the print canvas which is part of the visual tree and force it to go // through layout so that the linked containers correctly distribute the content inside them. PrintCanvas.Children.Add(page); PrintCanvas.InvalidateMeasure(); PrintCanvas.UpdateLayout(); printPreviewPages.Add(new PrintPage(printPreviewPages.Count + 1, page)); } if (PreviewPagesCreated != null) { PreviewPagesCreated.Invoke(printPreviewPages, null); } PrintDocument printDoc = (PrintDocument)sender; // Report the number of preview pages created printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Final); } }