/// <summary> /// Creates and adds one print preview page to the internal cache of print preview pages stored in <see cref="_printPreviewPages"/>. /// </summary> /// <param name="element">FrameworkElement used to represent the "printing page"</param> /// <param name="printPageDescription">Printer's page description</param> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private RichTextBlockOverflow AddOnePrintPreviewPage(FrameworkElement element, PrintPageDescription printPageDescription) { var page = new Page(); Grid printableArea; // Check if this is the first page ( no previous RichTextBlockOverflow) if (element == null) { // If this is the first page add the specific scenario content page = _firstPage; printableArea = page.FindName("printableArea") as Grid; FrameworkElement fre = page.Content as FrameworkElement; string name = fre.Name; printableArea = fre.FindName("printableArea") as Grid; //if (_elementsToPrint[0].GetType() == typeof(Grid)) //{ printableArea = _elementsToPrint[0] as Grid; //} //Save state if (!_stateBags.ContainsKey(printableArea)) { var stateBag = new PrintHelperStateBag(); stateBag.Capture(printableArea); _stateBags.Add(printableArea, stateBag); } } else { // Save state if (!_stateBags.ContainsKey(element)) { var stateBag = new PrintHelperStateBag(); stateBag.Capture(element); _stateBags.Add(element, stateBag); } // Flow content (text) from previous pages page = new ContinuationPage(element as RichTextBlockOverflow); printableArea = page.FindName("printableArea") as Grid; } // Set "paper" width page.Width = printPageDescription.PageSize.Width; page.Height = printPageDescription.PageSize.Height; // Get the margins size double marginWidth = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * ApplicationContentMarginLeft * 2); double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * ApplicationContentMarginTop * 2); //printableArea = page.FindName("printableArea") as Grid; if (printableArea != null) { // Set up the "printable area" on the "paper" printableArea.Width = _firstPage.Width - marginWidth; printableArea.Height = _firstPage.Height - marginHeight; } //element.VerticalAlignment = VerticalAlignment.Top; //element.HorizontalAlignment = HorizontalAlignment.Left; //if (element.Width > element.Height) //{ // var newWidth = page.Width - marginWidth; // element.Height = element.Height * (newWidth / element.Width); // element.Width = newWidth; //} //else //{ // var newHeight = page.Height - marginHeight; // element.Width = element.Width * (newHeight / element.Height); // element.Height = newHeight; //} //element.Margin = new Thickness(marginWidth / 2, marginHeight / 2, marginWidth / 2, marginHeight / 2); //page.Content = element; // Add the (newley 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(); // Find the last text container and see if the content is overflowing RichTextBlockOverflow textLink = (RichTextBlockOverflow)page.FindName("ContinuationPageLinkedContainer"); // Check if this is the last page if (!textLink.HasOverflowContent && textLink.Visibility == Visibility.Visible) { _printCanvas.UpdateLayout(); } // Add the page to the page preview collection _printPreviewPages.Add(page); return(textLink); //return DispatcherHelper.ExecuteOnUIThreadAsync( // () => // { // // 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.UpdateLayout(); // _printCanvas.InvalidateMeasure(); // // Add the page to the page preview collection // _printPreviewPages.Add(page); // }, Windows.UI.Core.CoreDispatcherPriority.High); }
/// <summary> /// This function creates and adds one print preview page to the internal cache of print preview /// pages stored in printPreviewPages. /// </summary> /// <param name="lastRTBOAdded">Last RichTextBlockOverflow element added in the current content</param> /// <param name="printPageDescription">Printer's page description</param> protected virtual RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription) { // XAML element that is used to represent to "printing page" FrameworkElement page; // The link container for text overflowing in this page RichTextBlockOverflow textLink; // Check if this is the first page ( no previous RichTextBlockOverflow) if (lastRTBOAdded == null) { // If this is the first page add the specific scenario content page = firstPage; // Hide footer since we don't know yet if it will be displayed (this might not be the last page) - wait for layout //StackPanel footer = (StackPanel)page.FindName("Footer"); //footer.Visibility = Windows.UI.Xaml.Visibility.Collapsed; } else { // Flow content (text) from previous pages page = new ContinuationPage(lastRTBOAdded); } // Set "paper" width page.Width = printPageDescription.PageSize.Width; page.Height = printPageDescription.PageSize.Height; Grid printableArea = (Grid)page.FindName("printPage1"); // Get the margins size // If the ImageableRect is smaller than the app provided margins use the ImageableRect double marginWidth = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * ApplicationContentMarginLeft * 2); double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * ApplicationContentMarginTop * 2); // Set-up "printable area" on the "paper" printableArea.Width = firstPage.Width - marginWidth; printableArea.Height = firstPage.Height - marginHeight; // Add the (newley 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(); //_printCanvas.Children.Add(page); //_printCanvas.InvalidateMeasure(); //_printCanvas.UpdateLayout(); // Find the last text container and see if the content is overflowing textLink = (RichTextBlockOverflow)page.FindName("ContinuationPageLinkedContainer"); // Check if this is the last page if (!textLink.HasOverflowContent && textLink.Visibility == Windows.UI.Xaml.Visibility.Visible) { //StackPanel footer = (StackPanel)page.FindName("Footer"); //footer.Visibility = Windows.UI.Xaml.Visibility.Visible; PrintCanvas.UpdateLayout(); _printCanvas.UpdateLayout(); } // Add the page to the page preview collection _printPreviewPages.Add(page); return(textLink); }