protected override RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
        {
            // Check if we need to hide/show text & images for this scenario
            // Since all is rulled by the first page (page flow), here is where we must start
            if (lastRTBOAdded == null)
            {
                // Get a refference to page objects
                Grid pageContent = (Grid)firstPage.FindName("printableArea");
                Image scenarioImage = (Image)firstPage.FindName("scenarioImage");
                RichTextBlock mainText = (RichTextBlock)firstPage.FindName("textContent");
                RichTextBlockOverflow firstLink = (RichTextBlockOverflow)firstPage.FindName("firstLinkedContainer");
                RichTextBlockOverflow continuationLink = (RichTextBlockOverflow)firstPage.FindName("continuationPageLinkedContainer");

                // Hide(collapse) and move elements in different grid cells depending by the viewable content(only text, only pictures)

                scenarioImage.Visibility = ShowImage ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
                firstLink.SetValue(Grid.ColumnSpanProperty, ShowImage ? 1 : 2);

                scenarioImage.SetValue(Grid.RowProperty, ShowText ? 2 : 1);
                scenarioImage.SetValue(Grid.ColumnProperty, ShowText ? 1 : 0);

                pageContent.ColumnDefinitions[0].Width = new GridLength(ShowText ? 6 : 4, GridUnitType.Star);
                pageContent.ColumnDefinitions[1].Width = new GridLength(ShowText ? 4 : 6, GridUnitType.Star);

                // Break the text flow if the app is not printing text in order not to spawn pages with blank content
                mainText.Visibility = ShowText ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
                continuationLink.Visibility = ShowText ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;

                // Hide header if printing only images
                StackPanel header = (StackPanel)firstPage.FindName("header");
                header.Visibility = ShowText ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
            }

            //Continue with the rest of the base printing layout processing (paper size, printable page size)
            return base.AddOnePrintPreviewPage(lastRTBOAdded, printPageDescription);
        }
        private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRtboAdded, PrintPageDescription printPageDescription)
        {
            FrameworkElement page;
            RichTextBlockOverflow link;

            if (lastRtboAdded == null)
            {
                page = page1;
                StackPanel footer = (StackPanel)page.FindName("footer");
                footer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else
            {
                page = new ContinuationPage(lastRtboAdded);
            }

            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            Grid printableArea = (Grid)page.FindName("printableArea");

            double marginWidth = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * left * 2);
            double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * top * 2);

            printableArea.Width = page1.Width - marginWidth;
            printableArea.Height = page1.Height - marginHeight;

            PrintContainer.Children.Add(page);
            PrintContainer.InvalidateMeasure();
            PrintContainer.UpdateLayout();

            // Find the last text container and see if the content is overflowing
            link = (RichTextBlockOverflow)page.FindName("continuationPageLinkedContainer");

            // Check if this is the last page
            if (!link.HasOverflowContent && link.Visibility == Windows.UI.Xaml.Visibility.Visible)
            {
                StackPanel footer = (StackPanel)page.FindName("footer");
                footer.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }

            // Add the page to the page preview collection
            pages.Add(page);

            return link;
        }
        protected override RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
        {
            RichTextBlockOverflow textLink = base.AddOnePrintPreviewPage(lastRTBOAdded, printPageDescription);

            // Don't show footer in selection mode
            if (selectionMode)
            {
                FrameworkElement page = (FrameworkElement)printPreviewPages[printPreviewPages.Count - 1];
                StackPanel footer = (StackPanel)page.FindName("Footer");
                footer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }

            return textLink;
        }
Exemplo n.º 4
0
        /// <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>
        private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded,
            PrintPageDescription printPageDescription)
        {
            // Create a cavase which represents the page 
            var page = new Canvas();
            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            var pageState = new PageLoadState(page, printPreviewPages.Count);
            pageState.ReadyAction = async (pageNumber, currentPage) =>
            {
                // Ignore if this is not the current page
                if (Interlocked.CompareExchange(ref currentPreviewPage, currentPreviewPage, pageNumber) == pageNumber)
                {
                    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        //await new Windows.UI.Popups.MessageDialog("Content loaded").ShowAsync();
                        printDocument.SetPreviewPage(pageNumber + 1, currentPage);
                    });
                }
            };

            // Create a grid which contains the actual content to be printed
            var content = new Grid();

            // 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 content size based on the given margins
            content.Width = printPageDescription.PageSize.Width - marginWidth;
            content.Height = printPageDescription.PageSize.Height - marginHeight;

            // Set content margins
            content.SetValue(Canvas.LeftProperty, marginWidth/2);
            content.SetValue(Canvas.TopProperty, marginHeight/2);

            //// Add the RowDefinitions to the Grid which is a content to be printed
            //RowDefinition rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(0.7, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(0.8, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(2.5, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(3.5, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(1.5, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(0.5, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);
            //rowDef = new RowDefinition();
            //rowDef.Height = new GridLength(0.5, GridUnitType.Star);
            //content.RowDefinitions.Add(rowDef);


            // Add the ColumnDefinitions to the Grid which is a content to be printed
            //ColumnDefinition colDef = new ColumnDefinition();
            //colDef.Width = new GridLength(40, GridUnitType.Star);
            //content.ColumnDefinitions.Add(colDef);
            //colDef = new ColumnDefinition();
            //colDef.Width = new GridLength(60, GridUnitType.Star);
            //content.ColumnDefinitions.Add(colDef);


            //// Create the "Windows 8 SDK Sample" header which consists of an image and text in a stack panel
            //// and add it to the content grid
            //Image windowsLogo = new Image();
            //windowsLogo.Source = new BitmapImage(new Uri("ms-appx:///Images/windows-sdk.png"));
            //pageState.ListenForCompletion((BitmapImage)windowsLogo.Source);

            //TextBlock headerText = new TextBlock();
            //headerText.TextWrapping = TextWrapping.Wrap;
            //headerText.Text = "Windows 8 SDK Sample";
            //headerText.FontSize = 20;
            //headerText.Foreground = new SolidColorBrush(Colors.Black);

            //StackPanel sp = new StackPanel();
            //sp.Orientation = Orientation.Horizontal;
            //sp.Children.Add(windowsLogo);
            //sp.Children.Add(headerText);


            //StackPanel outerPanel = new StackPanel();
            //outerPanel.Orientation = Orientation.Vertical;
            //outerPanel.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
            //outerPanel.Children.Add(sp);
            //outerPanel.SetValue(Grid.RowProperty, 1);
            //outerPanel.SetValue(Grid.ColumnSpanProperty, 2);


            //TextBlock sampleTitle = new TextBlock();
            //sampleTitle.TextWrapping = TextWrapping.Wrap;
            //sampleTitle.Text = "Print Sample";
            //sampleTitle.FontSize = 22;
            //sampleTitle.FontWeight = FontWeights.Bold;
            //sampleTitle.Foreground = new SolidColorBrush(Colors.Black);
            //outerPanel.Children.Add(sampleTitle);

            //content.Children.Add(outerPanel);


            //// Create Microsoft image used to end each page and add it to the content grid
            //Image microsoftLogo = new Image();
            //microsoftLogo.Source = new BitmapImage(new Uri("ms-appx:///Images/microsoft-sdk.png"));
            //pageState.ListenForCompletion((BitmapImage)microsoftLogo.Source);
            //microsoftLogo.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
            //microsoftLogo.SetValue(Grid.RowProperty, 5);
            //content.Children.Add(microsoftLogo);


            //// Add the copywrite notice and add it to the content grid
            //TextBlock copyrightNotice = new TextBlock();
            //copyrightNotice.Text = "© 2011 Microsoft. All rights reserved.";
            //copyrightNotice.FontSize = 16;
            //copyrightNotice.TextWrapping = TextWrapping.Wrap;
            //copyrightNotice.Foreground = new SolidColorBrush(Colors.Black);
            //copyrightNotice.SetValue(Grid.RowProperty, 6);
            //copyrightNotice.SetValue(Grid.ColumnSpanProperty, 2);
            //content.Children.Add(copyrightNotice);


            // If lastRTBOAdded is null then we know we are creating the first page. 
            bool isFirstPage = lastRTBOAdded == null;

            FrameworkElement previousLTCOnPage = null;
            var rtbo = new RichTextBlockOverflow();
            // Create the linked containers and and add them to the content grid
            if (isFirstPage)
            {
                // The first linked container in a chain of linked containers is is always a RichTextBlock
                if (ShowText)
                {
                    var rtbl = new RichTextBlock();
                    rtbl = AddContentToRTBl(rtbl);
                    content.Children.Add(rtbl);
                    // Save the RichTextBlock as the last linked container added to this page
                    previousLTCOnPage = rtbl;
                }

                //if (ShowImage)
                //{
                //    // Add the image to the first page and add it to the content grid
                //    Image pic = new Image();
                //    BitmapImage bitmap = new BitmapImage(new Uri("ms-appx:///Images/print_1.png"));
                //    pageState.ListenForCompletion(bitmap);
                //    pic.Source = bitmap;
                //    pic.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
                //    pic.Margin = new Thickness(10);
                //    if (ShowText)
                //    {
                //        pic.SetValue(Grid.RowProperty, 3);
                //        pic.SetValue(Grid.ColumnProperty, 2);
                //        content.Children.Add(pic);
                //        content.RowDefinitions[2].Height = new GridLength(2.5, GridUnitType.Star);
                //    }
                //    else
                //    {
                //        pic.Width = content.Width;
                //        pic.Height = content.Height;
                //        page.Children.Add(pic);
                //    }
                //}
            }
            else if (ShowText)
            {
                // This is not the first page so the first element on this page has to be a
                // RichTextBoxOverflow that links to the last RichTextBlockOverflow added to
                // the previous page.
                rtbo = new RichTextBlockOverflow();
                rtbo.SetValue(Grid.RowProperty, 2);
                rtbo.SetValue(Grid.ColumnSpanProperty, 2);
                content.Children.Add(rtbo);

                // Keep text flowing from the previous page to this page by setting the linked text container just
                // created (rtbo) as the OverflowContentTarget for the last linked text container from the previous page 
                lastRTBOAdded.OverflowContentTarget = rtbo;

                // Save the RichTextBlockOverflow as the last linked container added to this page
                previousLTCOnPage = rtbo;
            }


            //if (ShowText)
            //{
            //    // Create the next linked text container for on this page.
            //    rtbo = new RichTextBlockOverflow();
            //    rtbo.SetValue(Grid.RowProperty, 3);

            //    // If this linked container is not on the first page make it span 2 columns.
            //    if (!isFirstPage || !ShowImage)
            //        rtbo.SetValue(Grid.ColumnSpanProperty, 2);

            //    // Add the RichTextBlockOverflow to the content to be printed.
            //    content.Children.Add(rtbo);

            //    // Add the new RichTextBlockOverflow to the chain of linked text containers. To do this we much check
            //    // to see if the previous container is a RichTextBlock or RichTextBlockOverflow.
            //    if (previousLTCOnPage is RichTextBlock)
            //        ((RichTextBlock)previousLTCOnPage).OverflowContentTarget = rtbo;
            //    else
            //        ((RichTextBlockOverflow)previousLTCOnPage).OverflowContentTarget = rtbo;

            //    // Save the last linked text container added to the chain
            //    previousLTCOnPage = rtbo;

            //    // Create the next linked text container for on this page.
            //    rtbo = new RichTextBlockOverflow();
            //    rtbo.SetValue(Grid.RowProperty, 4);
            //    rtbo.SetValue(Grid.ColumnSpanProperty, 2);
            //    content.Children.Add(rtbo);

            //    // Add the new RichTextBlockOverflow to the chain of linked text containers. We don't have to check
            //    // the type of the previous linked container this time because we know it's a RichTextBlockOverflow element
            //    ((RichTextBlockOverflow)previousLTCOnPage).OverflowContentTarget = rtbo;
            //}
            // We are done creating the content for this page. Add it to the Canvas which represents the page
            page.Children.Add(content);

            // Add the newley created page to the printing root 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.
            PrintingRoot.Children.Add(page);
            PrintingRoot.InvalidateMeasure();
            PrintingRoot.UpdateLayout();

            // Add the newley created page to the list of pages
            printPreviewPages.Add(pageState);

            // Return the last linked container added to the page
            return rtbo;
        }
Exemplo n.º 5
0
        /// <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;

      
            RichTextBlockOverflow textLink;


            page = firstPage;


            // Set "paper" width
            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            Grid printableArea = (Grid)page.FindName("printableArea");

            // 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 printing root 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.            
            PrintingRoot.Children.Add(page);
            PrintingRoot.InvalidateMeasure();
            PrintingRoot.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;
            }

            // Add the page to the page preview collection
            printPreviewPages.Add(page);

            return textLink;
        }
        /// <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>
        private 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;
            }
            else
            {
                // Flow content (text) from previous pages
                page = new PrintPage(lastRTBOAdded);

                // Remove the duplicate OverflowContentTarget. 
                ((RichTextBlock)page.FindName("textContent")).OverflowContentTarget = null;
            }

            // Set paper width
            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            Grid printableArea = (Grid)page.FindName("printableArea");

            // 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 * HorizontalPrintMargin * 2);
            double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * VerticalPrintMargin * 2);

            // Set-up "printable area" on the "paper"
            printableArea.Width = page.Width - marginWidth;
            printableArea.Height = page.Height - marginHeight;

            // Add the (newly created) page to the printing root 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.            
            PrintingRoot.Children.Add(page);
            PrintingRoot.InvalidateMeasure();
            PrintingRoot.UpdateLayout();

            // Find the last text container and see if the content is overflowing
            textLink = (RichTextBlockOverflow)page.FindName("continuationPageLinkedContainer");

            // Add page number
            this.pageNumber += 1;
            TextBlock pageNumberTextBlock = (TextBlock)page.FindName("pageNumber");
            if (pageNumberTextBlock != null)
            {
                pageNumberTextBlock.Text = string.Format("- {0} -", this.pageNumber);
            }

            // Add the page to the page preview collection
            printPreviewPages.Add(page);

            return textLink;
        }
        /// <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>
        private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
        {
            // Create a cavase which represents the page 
            Canvas page = new Canvas();
            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            PageLoadState pageState = new PageLoadState(page, printPreviewPages.Count);
            pageState.ReadyAction = async (pageNumber, currentPage) =>
            {
                // Ignore if this is not the current page
                if (Interlocked.CompareExchange(ref currentPreviewPage, currentPreviewPage, pageNumber) == pageNumber)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        //await new Windows.UI.Popups.MessageDialog("Content loaded").ShowAsync();
                        printDocument.SetPreviewPage(pageNumber + 1, currentPage);
                    });
                }
            };

            // Create a grid which contains the actual content to be printed
            Grid content = new Grid();

            // 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 content size based on the given margins
            content.Width = printPageDescription.PageSize.Width - marginWidth;
            content.Height = printPageDescription.PageSize.Height - marginHeight;

            // Set content margins
            content.SetValue(Canvas.LeftProperty, marginWidth / 2);
            content.SetValue(Canvas.TopProperty, marginHeight / 2);

            // If lastRTBOAdded is null then we know we are creating the first page. 
            bool isFirstPage = lastRTBOAdded == null;

            FrameworkElement previousLTCOnPage = null;
            RichTextBlockOverflow rtbo = new RichTextBlockOverflow();
            // Create the linked containers and and add them to the content grid
            if (isFirstPage)
            {
                // The first linked container in a chain of linked containers is is always a RichTextBlock
                if (ShowText)
                {
                    RichTextBlock rtbl = new RichTextBlock();
                    rtbl = AddContentToRTBl(rtbl);
                    content.Children.Add(rtbl);
                    // Save the RichTextBlock as the last linked container added to this page
                    previousLTCOnPage = rtbl;
                }
            }
            else if (ShowText)
            {
                // This is not the first page so the first element on this page has to be a
                // RichTextBoxOverflow that links to the last RichTextBlockOverflow added to
                // the previous page.
                rtbo = new RichTextBlockOverflow();
                rtbo.SetValue(Grid.RowProperty, 2);
                rtbo.SetValue(Grid.ColumnSpanProperty, 2);
                content.Children.Add(rtbo);

                // Keep text flowing from the previous page to this page by setting the linked text container just
                // created (rtbo) as the OverflowContentTarget for the last linked text container from the previous page 
                lastRTBOAdded.OverflowContentTarget = rtbo;

                // Save the RichTextBlockOverflow as the last linked container added to this page
                previousLTCOnPage = rtbo;
            }

            // We are done creating the content for this page. Add it to the Canvas which represents the page
            page.Children.Add(content);

            // Add the newley created page to the printing root 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.
            PrintingRoot.Children.Add(page);
            PrintingRoot.InvalidateMeasure();
            PrintingRoot.UpdateLayout();

            // Add the newley created page to the list of pages
            printPreviewPages.Add(pageState);

            // Return the last linked container added to the page
            return rtbo;
        }
        /// <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("PrintableArea");

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

            // 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;
            }

            // Add the page to the page preview collection
            printPreviewPages.Add(page);

            return textLink;
        }
Exemplo n.º 9
0
        // 向 previewPages 添加一个预览页,以及向 printingRoot 添加一个打印内容,同时返回本页所对应的下一页的 RichTextBlockOverflow
        private RichTextBlockOverflow AddPreviewPageAndPrintPage(RichTextBlockOverflow lastRichText, PrintPageDescription printPageDescription)
        {
            // 需要添加到 previewPages 的预览页
            FrameworkElement previewPage;

            // 添加到 previewPages 的打印页所对应的下一页的 RichTextBlockOverflow
            RichTextBlockOverflow textOverflow;

            if (lastRichText == null) // 第一页
            {
                previewPage = printPage;
                StackPanel footer = (StackPanel)previewPage.FindName("footer"); // 第一页不显示页脚
                footer.Visibility = Visibility.Collapsed;
            }
            else // 非第一页
            {
                previewPage = new ContinuationPage(lastRichText);
            }

            // 打印页的宽和高
            previewPage.Width = printPageDescription.PageSize.Width;
            previewPage.Height = printPageDescription.PageSize.Height;

            // 打印页的页边距
            Grid printableArea = (Grid)previewPage.FindName("printableArea");
            double applicationContentMarginLeft = 0.075; // 页的左右边距,即左右边距加在一起占 15% 的空间
            double applicationContentMarginTop = 0.03; // 页的上下边距,即上下边距加在一起占 6% 的空间
            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.Width = printPage.Width - marginWidth;
            printableArea.Height = printPage.Height - marginHeight;

            // 向 printingRoot 添加一个打印内容,以便发送到打印机打印
            printingRoot.Children.Add(previewPage);
            printingRoot.InvalidateMeasure();
            printingRoot.UpdateLayout();

            // 获取本页所对应的下一页的 RichTextBlockOverflow
            textOverflow = (RichTextBlockOverflow)previewPage.FindName("textOverflow");

            if (!textOverflow.HasOverflowContent && textOverflow.Visibility == Visibility.Visible)
            {
                StackPanel footer = (StackPanel)previewPage.FindName("footer"); // 最后一页才显示页脚
                footer.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }

            // 向 previewPages 添加新页
            previewPages.Add(previewPage);

            return textOverflow;
        }
Exemplo n.º 10
0
        //This method does all of the heavy lifting for us, calculating the size of the page,
        //setting margins, and saving them to the pages List<>.
        private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
        {
            //This method does all of the heavy lifting for us, calculating the size of the 
            //page, setting margins, and saving them to the pages List<>.
            FrameworkElement page;
            RichTextBlockOverflow link;

            if (lastRTBOAdded == null)
            {
                page = page1;
            }
            else
            {
                page = new ContinuesPage(lastRTBOAdded);
            }

            page.Width = printPageDescription.PageSize.Width;
            page.Height = printPageDescription.PageSize.Height;

            Grid printableArea = (Grid)page.FindName("printableArea");

            double marginWidth = Math.Max(printPageDescription.PageSize.Width - printPageDescription.ImageableRect.Width, printPageDescription.PageSize.Width * left * 2);
            double marginHeight = Math.Max(printPageDescription.PageSize.Height - printPageDescription.ImageableRect.Height, printPageDescription.PageSize.Height * top * 2);

            printableArea.Width = page1.Width - marginWidth;
            printableArea.Height = page1.Height - marginHeight;

            PrintContainer.Children.Add(page);
            PrintContainer.InvalidateMeasure();
            PrintContainer.UpdateLayout();

            // Find the last text container and see if the content is overflowing
            link = (RichTextBlockOverflow)page.FindName("continuationPageLinkedContainer");

            // Add the page to the page preview collection
            pages.Add(page);

            return link;
        }