Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
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>
        /// <returns>The link container for text overflowing in this page.</returns>
        protected virtual RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription)
        {
            // XAML element that is used to represent to "printing page".
            FrameworkElement page = FirstPage;

            // The link container for text overflowing in this page.
            RichTextBlockOverflow textLink;

            // Check if this is the first page ( no previous RichTextBlockOverflow).
            if (lastRTBOAdded == null)
            {
                // Hide footer since we don't know yet if it will be displayed (this might not be the last page) - wait for layout.
                (page.FindName("Footer") as StackPanel).Visibility = Visibility.Collapsed;
            }

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

            var printableArea = page.FindName("PrintableArea") as Grid;

            // Get the margins size.
            // If the ImageableRect is smaller than the app provided margins use the ImageableRect.
            double marginWidth  = 0;
            double marginHeight = 0;

            // 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 = page.FindName("ContinuationPageLinkedContainer") as RichTextBlockOverflow;

            // Check if this is the last page.
            if (!textLink.HasOverflowContent && textLink.Visibility == Visibility.Visible)
            {
                (page.FindName("Footer") as StackPanel).Visibility = Visibility.Visible;
            }

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

            return(textLink);
        }