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