none
WPF RichTextBox另存为RTF丢失分页 RRS feed

  • 问题

  • 通过BreakPageBefore 设置分页,打印成PDF时可以看出确实分页了,但另存为RTF就没分页了

    Section.BreakPageBefore = true;

    //RichTextBox另存为RTF

    public void SaveAs(object sender, RoutedEventArgs e)
            {            
                var sfd = new System.Windows.Forms.SaveFileDialog();            
                sfd.DefaultExt = "rtf";
                sfd.Filter = "RTF 文件 (*.rtf))|*.rtf";            
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK &&sfd.FileName.Length > 0)
                {            

                    TextRange textRange = new TextRange(RichTextBox.Document.ContentStart, RichTextBox.Document.ContentEnd);

                    using (var myStream = new FileStream(sfd.FileName, FileMode.Create))
                    {
                        textRange.Save(myStream, DataFormats.Rtf);
                    }
                }
            }

                                       
    2021年7月9日 8:26

全部回复

  • 你好,

    你能告诉你的BreakPageBefore设置分页指的是什么?是你自己定义的方法吗? 还是指Block.BreakPageBefore?你能把你的打印为pdf的代码分享出来供我分析吗?

    Best regards

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2021年7月12日 8:59
  •  BreakPageBefore设置分页  Block.BreakPageBefore

    打印代码为

    private void Print(object sender, RoutedEventArgs e)
            {
               var dialog = new PrintDialog();
                if (dialog.ShowDialog() == true)
                {                
                    dialog.PrintDocument(((IDocumentPaginatorSource)RichTextBox.Document).DocumentPaginator, Title);
                }
            }

    目前来看是打印正常分页了(内容打印在了不同的页上)但存为RTF时内容就又连在一页了

    2021年7月13日 2:36
  • 你好,

    我测试了你问题中的代码,并不能重现你的问题。我的RichTextBox代码是参照官方文档中的示例来写的,如果方便的话,你可以把你的RichTextBox的相关代码给我参考吗?这样我才能重现你的问题,

    Best Regards

    Daisy Tian


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2021年7月16日 2:37
  •  MainWindow.xaml 代码

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <DockPanel x:Name="Dock">
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="另存为" Click="SaveAs"/>
                <MenuItem Header="打印" Click="Print"/>
            </Menu>
            <RichTextBox x:Name="RichTextBox"  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" IsReadOnly="True" />

        </DockPanel>
    </Window>

     MainWindow.xaml.cs  代码

    using System.IO;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;

    namespace WpfApp1
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                setRichTextBox();

            }

            private void setRichTextBox()//此文本另存为RTF未见分页
            {
               var paragraph = new Paragraph(new Run("第一页文字"));
                RichTextBox.Document.Blocks.Add(paragraph);
                var sec = new Section();
                sec.BreakPageBefore = true;
                 paragraph = new Paragraph(new Run("第二页文字"));
                sec.Blocks.Add(paragraph);
                RichTextBox.Document.Blocks.Add(sec);
                sec = new Section();
                sec.BreakPageBefore = true;
                paragraph = new Paragraph(new Run("第三页文字"));
                sec.Blocks.Add(paragraph);
                RichTextBox.Document.Blocks.Add(sec);
            }

            public void SaveAs(object sender, RoutedEventArgs e)
            {
                var sfd = new System.Windows.Forms.SaveFileDialog();
                sfd.FileName = Title;
                sfd.DefaultExt = "rtf";
                sfd.Filter = "RTF 文件 (*.rtf))|*.rtf";
                sfd.RestoreDirectory = false;
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK && sfd.FileName.Length > 0)
                {
                    TextRange textRange = new TextRange(RichTextBox.Document.ContentStart, RichTextBox.Document.ContentEnd);
                    using (var myStream = new FileStream(sfd.FileName, FileMode.Create))
                    {
                        textRange.Save(myStream, DataFormats.Rtf);
                    }
                }
            }
            private void Print(object sender, RoutedEventArgs e)
            {
                var dialog = new PrintDialog();
                if (dialog.ShowDialog() == true)
                {
                    dialog.PrintDocument(((IDocumentPaginatorSource)RichTextBox.Document).DocumentPaginator, Title);
                }
            }
        }
    }


    2021年8月6日 9:44