/// <summary> /// This function creates and adds one print preview page to the internal cache of print preview /// pages stored in printPreviewPages. /// </summary> /// <param name="printPageDescription">Printer's page description</param> private void AddOnePrintPreviewPage(PrintPageDescription printPageDescription) { // Set "paper" width _printPage.Width = printPageDescription.PageSize.Width; _printPage.Height = printPageDescription.PageSize.Height; var printableArea = (Grid)_printPage.Content; // 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 = _printPage.Width - marginWidth; printableArea.Height = _printPage.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(_printPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); // Add the page to the page preview collection PrintPreviewPages.Add(_printPage); }
/// <summary> /// Provide print content for scenario 1 first page /// </summary> protected override void PreparetPrintContent() { if (ViewModel == null || ViewModel.ReisMogelijkheden == null) { return; } if (firstPage == null) { firstPage = new PrintContentTest(); string html = string.Empty; foreach (var reis in ViewModel.ReisMogelijkheden) { html += reis.GetAsHtmlForPrint(); } firstPage.DataContext = html; } // 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(firstPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
/// <summary> /// Factory method for every scenario that will create/generate print content specific to each scenario /// For scenarios 1-5: it will create the first page from which content will flow /// Scenario 6 uses a different approach /// </summary> private void PreparetPrintContent() { _printPage = new PrintPage(_currentComic); // 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.Clear(); PrintingRoot.Children.Add(_printPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
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. PrintingRoot.InvalidateArrange(); PrintingRoot.InvalidateMeasure(); PrintingRoot.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); } }
/// <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 GeneratePage(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. PrintingRoot.Children.Add(page); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); } } PrintDocument printDoc = (PrintDocument)sender; // Send the page to preview. printDoc.SetPreviewPage(pageNumber, page); }
/// <summary> /// Provide print content for scenario 2 first page /// </summary> protected override void PreparetPrintContent() { if (firstPage == null) { firstPage = new ScenarioOutput2(); StackPanel header = (StackPanel)firstPage.FindName("header"); header.Visibility = Windows.UI.Xaml.Visibility.Visible; } // 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(firstPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
protected override void PreparetPrintContent() { if (firstPage == null) { vm = (PresenterVM)VM; var ht = 400; // itemListView.ActualHeight; var wd = 300; // itemListView.ActualWidth; var imgUrl = string.Format(url, vm.CurrentQuestion.Id, wd, ht); firstPage = new ReportPrint() { ImageUrl = imgUrl }; } PrintingRoot.Children.Add(firstPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
/// <summary> /// Prepares the content for the printer based on the current selection /// </summary> protected override void PreparePrintContent() { // Create a session printout firstPage = new SessionPrintout(); // If there's no selection then stop if (itemListView.SelectedIndex == -1) { return; } // Otherwise load the selected session Session selectedSession = itemListView.SelectedItem as Session; // Set the basic variables (firstPage.FindName("lblTitle") as TextBlock).Text = "Session: " + selectedSession.Title; (firstPage.FindName("lblGames") as TextBlock).Text = "Games Played: " + selectedSession.GameIDs.Count.ToString(); (firstPage.FindName("lblMembers") as TextBlock).Text = "Members attended: " + selectedSession.Members.Count.ToString(); // Finally we fill in the game information // Loop games foreach (Game game in selectedSession.Games.OrderByDescending(x => x.ModelCreationTime)) { // Make a new textblock and set up basic properties TextBlock block = new TextBlock(); block.FontSize = 22; block.Margin = new Thickness(0, 5, 0, 5); block.Foreground = new SolidColorBrush(Windows.UI.Colors.Black); block.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left; // Copy the style from the title block.Style = (firstPage.FindName("lblPnlGamesTitle") as TextBlock).Style; // We take the white player's name first block.Text = game.WhitePlayer.Name; // Then concatenate whether they won or lost block.Text += (game.Winner == "White" ? " won against " : game.Winner == "Black" ? " lost to " : " drew with "); // Finally add the black player's name block.Text += game.BlackPlayer.Name; // Add it to the panel of games (firstPage.FindName("pnlGames") as StackPanel).Children.Add(block); } PrintingRoot.Children.Add(firstPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
private static 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 Window.Current.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); // Add the RowDefinitions to the Grid which is a content to be printed RowDefinition 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); // 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 RichTextBlock rtbl = new RichTextBlock(); rtbl.SetValue(Grid.RowProperty, 0); rtbl = AddContentToRTBl(rtbl); int a = rtbl.Blocks.Count(); rtbl.Foreground = new SolidColorBrush(Windows.UI.Colors.Black); content.Children.Add(rtbl); // Save the RichTextBlock as the last linked container added to this page previousLTCOnPage = rtbl; } else { // 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, 0); 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, 1); // 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, 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); }
/// <summary> /// Load the print content for the page /// </summary> protected override void PreparePrintContent() { firstPage = new MemberPrintout(); if (itemListView.SelectedIndex == -1) { return; } Member selectedMember = itemListView.SelectedItem as Member; // List of controls on print canvas: // imgMemberPhoto, lblName, lblEmail, lblGames, lblSessions, lblElo, lblGame<1-10>, lblSession<1-10> int gamesWon = selectedMember.Games.Count(x => ( // This is a lambda (inline function) collecting all games where the selectedMember won: x.BlackPlayerID == selectedMember.ID && x.Winner == "Black" || x.WhitePlayerID == selectedMember.ID && x.Winner == "White" )); int gamesLost = selectedMember.Games.Count(x => ( // This is a lambda (inline function) collecting all games where the selectedMember lost: x.BlackPlayerID == selectedMember.ID && x.Winner == "White" || x.WhitePlayerID == selectedMember.ID && x.Winner == "Black" )); int eloPercent; // Get number of players with more elo int above = App.db.Members.Count(member => member.ELO > selectedMember.ELO); // Calculate the percentage of members above this player eloPercent = (int)((double)(100 / App.db.Members.Count) * above); (firstPage.FindName("imgMemberPhoto") as Image).Source = selectedMember.Bitmap; (firstPage.FindName("lblName") as TextBlock).Text = "Name: " + selectedMember.Name; (firstPage.FindName("lblEmail") as TextBlock).Text = "Email: " + selectedMember.Email; (firstPage.FindName("lblGames") as TextBlock).Text = "Number of games played: " + selectedMember.Games.Count + " (" + gamesLost.ToString() + " Lost, " + gamesWon.ToString() + " Won)"; // We do a quick inline function here to count the number of sessions rather than extracting it into a variable (firstPage.FindName("lblSessions") as TextBlock).Text = "Number of sessions attended: " + (App.db.Sessions.Count(x => x.Members.Contains(selectedMember))); (firstPage.FindName("lblElo") as TextBlock).Text = "Elo: " + selectedMember.ELO + " (Top " + eloPercent.ToString() + "% of all members)"; // Finally we fill in the game and session information for (int i = 0; i < 10; i++) { if (selectedMember.Games.Count > i) { Game game = (selectedMember.Games.OrderByDescending(x => x.ModelCreationTime).ToList())[i]; string outcome; // Figure out whether they won, lost or drew if (game.Winner == "White" && game.WhitePlayerID == selectedMember.ID || game.Winner == "Black" && game.BlackPlayerID == selectedMember.ID) { outcome = "Won against "; } else if (game.Winner == "White" || game.Winner == "Black") { outcome = "Lost to "; } else { outcome = "Drew with "; } // Append the other player's name if (game.WhitePlayerID == selectedMember.ID) { outcome += game.BlackPlayer.Name; } else { outcome += game.WhitePlayer.Name; } // Set label text (firstPage.FindName("lblGame" + (i + 1)) as TextBlock).Text = outcome; } else { // Set label text (firstPage.FindName("lblGame" + (i + 1)) as TextBlock).Text = ""; } } for (int i = 0; i < 10; i++) { // Put up to ten sessions on the page List <Session> sessions = App.db.Sessions.Where(x => x.Members.Contains(selectedMember)).OrderByDescending(x => x.ModelCreationTime).ToList(); if (sessions.Count > i) { Session session = sessions[i]; (firstPage.FindName("lblSession" + (i + 1)) as TextBlock).Text = session.Title; } else { // If we run out then just make it blank (firstPage.FindName("lblSession" + (i + 1)) as TextBlock).Text = ""; } } // Add the first page to the page root and force it to render PrintingRoot.Children.Add(firstPage); PrintingRoot.InvalidateMeasure(); PrintingRoot.UpdateLayout(); }
private RichTextBlockOverflow AddOnePrintPreviewPage(RichTextBlockOverflow lastRTBOAdded, PrintPageDescription printPageDescription, int index) { // 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 = NeedToPrintPages[index]; } 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); }