Esempio n. 1
0
        /// <summary>
        /// This function creates and adds one print page to the internal cache of print pages
        /// pages stored in printPages.
        /// </summary>
        /// <param name="lastRTBOAdded">Last RichTextBlockOverflow element added in the current content</param>
        /// <param name="printPageDescription">Printer's page description</param>
        protected RichTextBlockOverflow AddOnePrintPage(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 = 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 (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();

            // 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 = Visibility.Visible;
            }

            // Add the page to the print page collection
            printPages.Add(page);

            return(textLink);
        }
Esempio n. 2
0
 private void _webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
 {
     PrintCanvas.Children.Add(new TextBlock {
         Text = "THIS IS A TEST"
     });
     PrintCanvas.InvalidateMeasure();
     PrintCanvas.UpdateLayout();
 }
Esempio n. 3
0
        private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
        {
            printPreviewPages = new List <UIElement>();
            PrintCanvas.Children.Clear();
            var           printOptions    = e.PrintTaskOptions;
            var           pageDescription = printOptions.GetPageDescription(0);
            PrintDocument printDoc        = (PrintDocument)sender;



            var printPage = CreateNewPrintPage(pageDescription.PageSize);

            SquadronPrintHeader printHeader = new SquadronPrintHeader()
            {
                ViewModel = this.ViewModel.SquadronViewModel,
                Margin    = new Thickness(10)
            };

            printPage.AddContent(printHeader);
            var Pilots = ViewModel.SquadronViewModel.Pilots;

            for (int i = 0; i < Pilots.Count(); i++)
            {
                PilotPrintControl pilotPrint = new PilotPrintControl()
                {
                    ViewModel = Pilots[i],
                    Margin    = new Thickness(10)
                };
                printPage.AddContent(pilotPrint);
                PrintCanvas.InvalidateMeasure();
                PrintCanvas.UpdateLayout();


                if (printPage.ActualHeight >= pageDescription.PageSize.Height)
                {
                    printPage.RemoveLastContentItem();
                    printPreviewPages.Add(printPage);
                    printPage = CreateNewPrintPage(pageDescription.PageSize);
                    i--;
                }
            }
            printPreviewPages.Add(printPage);
            PrintCanvas.Children.Clear();


            foreach (var item in printPreviewPages)
            {
                PrintCanvas.Children.Add(item);
            }
            PrintCanvas.InvalidateMeasure();
            PrintCanvas.UpdateLayout();
            // Report the number of preview pages created
            printDoc.SetPreviewPageCount(printPreviewPages.Count, PreviewPageCountType.Intermediate);
        }
        private void ShowContent(string selectionText)
        {
            bool hasSelection = !string.IsNullOrEmpty(selectionText);

            selectionMode = hasSelection;

            // Hide/show images depending by the selected text
            StackPanel header = (StackPanel)firstPage.FindName("Header");

            header.Visibility = hasSelection ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
            Grid pageContent = (Grid)firstPage.FindName("PrintableArea");

            pageContent.RowDefinitions[0].Height = GridLength.Auto;

            Image scenarioImage = (Image)firstPage.FindName("ScenarioImage");

            scenarioImage.Visibility = hasSelection ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;

            // Expand the middle paragraph on the full page if printing only selected text
            RichTextBlockOverflow firstLink = (RichTextBlockOverflow)firstPage.FindName("FirstLinkedContainer");

            firstLink.SetValue(Grid.ColumnSpanProperty, hasSelection ? 2 : 1);

            // Clear(hide) current text and add only the selection if a selection exists
            RichTextBlock mainText = (RichTextBlock)firstPage.FindName("TextContent");

            RichTextBlock textSelection = (RichTextBlock)firstPage.FindName("TextSelection");

            // Main (default) scenario text
            mainText.Visibility            = hasSelection ? Windows.UI.Xaml.Visibility.Collapsed : Windows.UI.Xaml.Visibility.Visible;
            mainText.OverflowContentTarget = hasSelection ? null : firstLink;

            // Scenario text-blocks used for displaying selection
            textSelection.OverflowContentTarget = hasSelection ? firstLink : null;
            textSelection.Visibility            = hasSelection ? Windows.UI.Xaml.Visibility.Visible : Windows.UI.Xaml.Visibility.Collapsed;
            textSelection.Blocks.Clear();

            // Force the visual root to go through layout so that the linked containers correctly distribute the content inside them.
            PrintCanvas.InvalidateArrange();
            PrintCanvas.InvalidateMeasure();
            PrintCanvas.UpdateLayout();

            // Add the text selection if any
            if (hasSelection)
            {
                Run inlineText = new Run();
                inlineText.Text = selectionText;

                Paragraph paragraph = new Paragraph();
                paragraph.Inlines.Add(inlineText);

                textSelection.Blocks.Add(paragraph);
            }
        }
Esempio n. 5
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  </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.GetPrintPreviewPage. It provides a specific print page preview,
        /// in the form of an UIElement, to an instance of PrintDocument.
        /// PrintDocument subsequently converts the UIElement into a page that the Windows print system can deal with.
        /// </summary>
        /// <param name="sender">The print documet.</param>
        /// <param name="e">Arguments containing the requested page preview.</param>
        protected async override void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
        {
            // Store a local copy of the request count to use later to determine if the computed page is out of date.
            // If the page preview is unavailable an async operation will generate the content.
            // When the operation completes there is a chance that a pagination request was already made therefore making this page obsolete.
            // If the page is obsolete throw away the result (don't call SetPreviewPage) since a new GetPrintPreviewPage will server that request.
            long requestNumber = 0;

            Interlocked.Exchange(ref requestNumber, requestCount);
            int pageNumber = e.PageNumber;

            UIElement page;
            bool      pageReady = false;

            // Try to get the page if it was previously generated.
            lock (printSync)
            {
                pageReady = pageCollection.TryGetValue(pageNumber - 1, out page);
            }

            if (!pageReady)
            {
                // The page is not available yet.
                page = await GeneratePageAsync(pageNumber, currentPageDescription);

                // If the ticket changed discard the result since the content is outdated.
                if (Interlocked.CompareExchange(ref requestNumber, requestNumber, requestCount) != requestCount)
                {
                    return;
                }

                // Store the page in the list in case an invalidate happens but the content doesn't need to be regenerated.

                lock (printSync)
                {
                    pageCollection[pageNumber - 1] = page;

                    // 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.
                    PrintCanvas.Children.Add(page);
                    PrintCanvas.InvalidateMeasure();
                    PrintCanvas.UpdateLayout();
                }
            }

            PrintDocument printDoc = (PrintDocument)sender;

            // Send the page to preview.
            printDoc.SetPreviewPage(pageNumber, page);
        }
        private void OnPrint(object sender, RoutedEventArgs e)
        {
            #region commented

            //PrintDialog printDialog = new PrintDialog();
            //if (printDialog.ShowDialog() == true)
            //{
            //    printDialog.PrintVisual(canvas, "A Simple Drawing");
            //}

            #endregion

            var printDialog = new PrintDialog();
            if (printDialog.ShowDialog() == true)
            {
                // Scale the TextBlock in both dimensions.
                double zoom;
                if (double.TryParse(ScaleTextBox.Text, out zoom))
                {
                    grid.Visibility = Visibility.Hidden;

                    // Add a background to make it easier to see the canvas bounds.
                    PrintCanvas.Background = Brushes.LightSteelBlue;

                    // Resize it.
                    PrintCanvas.LayoutTransform = new ScaleTransform(zoom / 100, zoom / 100);

                    // Get the size of the page.
                    var pageSize = new Size(printDialog.PrintableAreaWidth - 20, printDialog.PrintableAreaHeight - 20);

                    // Trigger the sizing of the element.
                    PrintCanvas.Measure(pageSize);
                    PrintCanvas.Arrange(new Rect(10, 10, pageSize.Width, pageSize.Height));

                    // Print the element.
                    printDialog.PrintVisual(PrintCanvas, "A Scaled Drawing");

                    // Reset the canvas.
                    PrintCanvas.Background      = null;
                    PrintCanvas.LayoutTransform = null;
                    grid.Visibility             = Visibility.Visible;
                }
                else
                {
                    MessageBox.Show("Invalid scale value.");
                }
            }
        }
Esempio n. 8
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  </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();
            }
        }