private void OnRichTextBlockLoaded(object sender, RoutedEventArgs e) { RichTextBlockOverflow overflow = null; bool isOverFlow = false; if (this.rtTextBlock.HasOverflowContent) { overflow = new RichTextBlockOverflow(); rtTextBlock.OverflowContentTarget = overflow; this.flipview.Items.Add(overflow); overflow.UpdateLayout(); isOverFlow = overflow.HasOverflowContent; while (isOverFlow) { RichTextBlockOverflow temp = overflow; overflow = new RichTextBlockOverflow(); temp.OverflowContentTarget = overflow; this.flipview.Items.Add(overflow); overflow.UpdateLayout(); isOverFlow = overflow.HasOverflowContent; } } }
private static void FillInBlocks(ObservableCollection<SceneViewModel> scenes, ObservableCollection<UIElement> blocks, double width, double height) { foreach (SceneViewModel sceneViewModel in scenes) { if (sceneViewModel is SceneViewModelPad) { continue; } IScene scene = sceneViewModel.CurrentScene; string stringContent = scene.Content.Content; RichTextBlock mainBlock = new StringToRtf().Convert(stringContent, null, null, null) as RichTextBlock; mainBlock.Width = width; mainBlock.Height = height - 20; mainBlock.DataContext = sceneViewModel; mainBlock.Padding = new Thickness(20); mainBlock.Foreground = new SolidColorBrush(Colors.Black); mainBlock.Measure(new Windows.Foundation.Size(width - mainBlock.Padding.Bottom - mainBlock.Padding.Top, height - mainBlock.Padding.Right - mainBlock.Padding.Left)); blocks.Add(mainBlock); if (mainBlock.HasOverflowContent) { RichTextBlockOverflow overflow = new RichTextBlockOverflow() { Width = width, Height = height }; overflow.DataContext = sceneViewModel; mainBlock.OverflowContentTarget = overflow; overflow.Padding = new Thickness(20); overflow.Measure(new Windows.Foundation.Size(width - overflow.Padding.Bottom - overflow.Padding.Top , height - overflow.Padding.Right - overflow.Padding.Left)); blocks.Add(overflow); while (overflow.HasOverflowContent) { RichTextBlockOverflow nextOverflow = new RichTextBlockOverflow() { Width = width, Height = height }; nextOverflow.DataContext = sceneViewModel; overflow.OverflowContentTarget = nextOverflow; nextOverflow.Padding = new Thickness(20); nextOverflow.Measure(new Windows.Foundation.Size(width - nextOverflow.Padding.Bottom - nextOverflow.Padding.Top , height - nextOverflow.Padding.Right - nextOverflow.Padding.Left)); blocks.Add(nextOverflow); overflow = nextOverflow; } } } }
public ContinuationPage(RichTextBlockOverflow textLinkContainer) { InitializeComponent(); textLinkContainer.OverflowContentTarget = continuationPageLinkedContainer; }
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; }
/// <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; }
public PrintPage(RichTextBlockOverflow textLinkContainer) : this() { textLinkContainer.OverflowContentTarget = continuationPageLinkedContainer; }
/// <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) { // 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; }
public ContinuationPage(RichTextBlockOverflow text) { this.InitializeComponent(); text.OverflowContentTarget = textOverflow; }
/// <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; }
private void checkOverflow(object sender, RoutedEventArgs e) { RichTextBlockOverflow textBlock = (RichTextBlockOverflow)sender; if (textBlock.HasOverflowContent) { RichTextBlockOverflow overflow = new RichTextBlockOverflow(); overflow.Margin = new Thickness(50); overflow.Loaded -= loadedEventHandler; overflow.Loaded += loadedEventHandler; textBlock.OverflowContentTarget = overflow; Overflows.Add(overflow); flipView.Items.Add(overflow); } }
private void checkTextBlock(object sender, EventArgs e) { RichTextBlock textBlock = (RichTextBlock)sender; if (textBlock.HasOverflowContent) { if (Overflows.Count == 0) { // If there is no existing overflow columns, create the first one RichTextBlockOverflow overflow = new RichTextBlockOverflow(); overflow.Margin = new Thickness(50); textBlock.OverflowContentTarget = overflow; Overflows.Add(overflow); flipView.Items.Add(overflow); overflow.Loaded -= loadedEventHandler; overflow.Loaded += loadedEventHandler; } else { for (int i = 0; i < Overflows.Count; i++) { if (Overflows[i].HasOverflowContent && i + 1 == Overflows.Count) { // We still have content but we're on the last overflow - create a new one RichTextBlockOverflow overflow = new RichTextBlockOverflow(); overflow.Margin = new Thickness(50); overflow.Loaded -= loadedEventHandler; overflow.Loaded += loadedEventHandler; Overflows[i].OverflowContentTarget = overflow; Overflows.Add(overflow); flipView.Items.Add(overflow); break; } else if (!Overflows[i].HasOverflowContent && i + 1 != Overflows.Count) { // There are more overflows but we ran out of content! - remove the extra ones if (flipView.SelectedIndex > i) { flipView.SelectedIndex = i; } while(Overflows.Count > i) { flipView.Items.Remove(flipView.Items.Last()); Overflows.Remove(Overflows.Last()); } break; } } } } else { // We don't need extra columns Overflows.Clear(); for (int i = 1; i < flipView.Items.Count; i++) { flipView.Items.RemoveAt(i); } } }
// 向 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; }
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; }
//加载文章内容,以代码处理是因为文章长度不定,即处理文本溢出问题 private void loadcontent() { stPanel.Children.Clear(); // 为了支持文本分块,使用RichTextBlock RichTextBlock tbContent = new RichTextBlock(); tbContent.Width = CT_WIDTH; tbContent.Height = CT_HEIGHT; tbContent.TextWrapping = TextWrapping.Wrap; tbContent.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); Paragraph ph = new Paragraph(); Run txtRun = new Run(); txtRun.Text = _viewmodel.one.ContentstrContent; ph.Inlines.Add(txtRun); tbContent.Blocks.Add(ph); if(Window.Current.Bounds.Height == 1080) tbContent.FontSize = Convert.ToDouble(32); else if (Window.Current.Bounds.Height == 1440) tbContent.FontSize = Convert.ToDouble(38); else tbContent.FontSize = Convert.ToDouble(20); stPanel.Children.Add(tbContent); // 更新一下状态,方便获取是否有溢出的文本 tbContent.UpdateLayout(); bool isflow = tbContent.HasOverflowContent; // 因为除了第一个文本块是RichTextBlock, // 后面的都是RichTextBlockOverflow一个一个接起来的 // 所以我们需要两个变量 RichTextBlockOverflow oldFlow = null, newFlow = null; if (isflow) { oldFlow = new RichTextBlockOverflow(); oldFlow.Width = CT_WIDTH; oldFlow.Height = CT_HEIGHT; oldFlow.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); tbContent.OverflowContentTarget = oldFlow; stPanel.Children.Add(oldFlow); oldFlow.UpdateLayout(); // 继续判断是否还有溢出 isflow = oldFlow.HasOverflowContent; } while (isflow) { newFlow = new RichTextBlockOverflow(); newFlow.Height = CT_HEIGHT; newFlow.Width = CT_WIDTH; newFlow.Margin = new Thickness(CT_MARGIN, 0, CT_MARGIN, CT_MARGIN + 20); oldFlow.OverflowContentTarget = newFlow; stPanel.Children.Add(newFlow); newFlow.UpdateLayout(); // 继续判断是否还有溢出的文本 isflow = newFlow.HasOverflowContent; // 当枪一个变量填充了文本后, // 把第一个变量的引用指向当前RichTextBlockOverflow // 确保OverflowContentTarget属性可以前后相接 oldFlow = newFlow; } }
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); }
//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; }