WPF之文档与RichTextBox

WPF之文档与RichTextBox

码农世界 2024-05-22 前端 61 次浏览 0个评论

1,文档分类:

        文档分为流文档(FlowDocument,布局可变)与固定文档(xps,pdf等布局不可变)。

        注明:流内容元素继承自ContentElement和FrameworkContenteElement而非常用的UIElement和FrameworkElement

2,文档容器:

流文档(FlowDocument)容器(只读,共三个)。

  •  FlowDocumentScrollViews:不能分页也不能分列显示。

            示例:

     
                
                   ....
                
            

            效果:

    • FlowDocumentPageViews:可进行分页和分列显示。

              示例

       
                  
                      
                            The sixth of August was my mothers birthday.My father and I wanted to give her surprising birthday presents.On their mothersbirthday,I know some of my classmates are going to help their mothers with housework;others are going to the shop and buy some flowers for their mothers.
                      
                      
                            In the morning,my father and I went shopping and bought a very nice cake.Then we went to the market and bought some food.When we got home,my father cooked some nice food in the kitchen.I went to my room to make a birthday card.I wanted to say to my mother that I loved her very much.Then I wrote in the card,"I love you,my dear mother!"I put it on her pillow,then we cleaned the room.We felt very tired,but we were very happy.
                      
                      
                            My mother got home.We said,"Happy Birthday!"My mother smiled.We had our supper together.My mother said,"Thank you,my daughter!"
                      
                      
                           How happy we were!
                      
                      
                         
                              
                        
                      
                  
              

               效果:

      •  FlowDocumentReader:可进行分页和分列显示,同时增加了查找功能,是三个中开销最大的容器。

        固定文档容器:

                显示格式内容已固定的文档(例如xps文档),一般用于文档预览呈现。

                注明:XPS文件实际上是包含压缩文件库的ZIP文件,包含字体,图像以及用于每一页的文本内容,可将XPS的扩展名修改为.zip并打开它。

                示例:

           
          public PreView(IDocumentPaginatorSource paginator)
                {
                    InitializeComponent();
                    viewer.Document = paginator;
                }

                效果:

         3,流文档的相关元素与主要属性:

        元素类型。

                块级元素:Paragraph,List,Table ,Section,BlockUIContainer。

                内联级元素:Run,Span, Hyperlink,InlineUIContainer,Floater等。

        主要属性:

                FlowDocumentScrollView.IsSelectionEnable=false,禁用文本选择。

                Paragraph.TextIndent设置缩进

                List.MarkerStyle:项目符号

                FlowDocument.IsOptimalParagraphEnabled="True" :使用最佳段落布局,默认为False

                FlowDocument.IsHyphenationEnabled="True":自动进行断字并添加连接符,默认为False

                FlowDocument.IsToolbarVisible:缩放滑动条是否显示。

                FlowDocument.ColumnWidth:设置值可实现分页。

                FlowDocument.ColumnRuleBrush, FlowDocument.ColumnRuleWidth="2":分列线,分列线宽度

                FlowDocument.ColumnGap:列与列之间空白宽度。

                示例:

         
                
                    
                    
                
                
                    
                        明月几时有
                        把酒问青天
                        
                            Top Programming languages: 
                        
                            
                                C#
                                VB.Net
                            
                            
                                JavaScript
                            
                        
                        
                                
                                
                                
                            
                                
                                    
                                    
                                        Rank
                                    
                                    
                                        Name
                                    
                                    
                                        Population
                                    
                                
                                
                                    
                                        1
                                    
                                    
                                        Rome
                                    
                                    
                                        45000
                                    
                                
                                
                                    
                                        1
                                    
                                    
                                        Rome
                                    
                                    
                                
                            
        雪山 飞狐 连城诀 天龙八部 射雕英雄传

                效果:

                注明:

                可以直接在paragraph内直接添加文本,它会隐式创建Run元素并用其包裹文本。

                例如:

        
                            These are the release
                            donkeys
                            for
                            New York
                            version 1.2.3
                        

                自动隐式转换为:

        
                            These are the release 
                            donkeys
                            for  
                            New York
                            version 1.2.3   
                        

                在流文档Paragraph中多个空格(包括tab)默认情况下被压缩为单个空格(遵守XML规则)。

                文本格式的设置可通过Paragraph的附加属性完成。

                ListItem:内容为块级元素

                TableCell:内容为块级元素

        4,使用RichTextBox控件编辑流文档:

                RichTextBox可读取rft扩展名的文档(word支持导出rtf)

                RichTextBox.Document :RichTextBox的文档类型为FlowDocument。

                RichTextBox的document为FlowDocument,所以可以在RichTextBox内的BlockUIContainer中添加UIElement,但是添加的UIElement处于DisEnable,可以通RichTextBox.IsDocumentEnabled=true使其响应。

                示例:

         
                
                    
                    
                
                
                    
                    
                    
                
                
                    
                
                
                
                    
                
                
                    
                
               
            
        private void btnRead_Click(object sender, RoutedEventArgs e)
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Rft文件|*.rtf";
                    if(ofd.ShowDialog()== true)
                    {
                        TextRange range = new TextRange(richRB01.Document.ContentStart, richRB01.Document.ContentEnd);
                        using (FileStream fs =new FileStream(ofd.FileName,FileMode.Open))
                        {
                            range.Load(fs, DataFormats.Rtf);
                        }
                        //将RichtextBox中的document转为Xaml
                        TextRange range2 = new TextRange(richRB01.Document.ContentStart, richRB01.Document.ContentEnd);
                        StringBuilder sb = new StringBuilder();
                        using (MemoryStream ms=new MemoryStream())
                        {
                            range2.Save(ms, DataFormats.Xaml);
                            ms.Seek(0, SeekOrigin.Begin);
                            using (TextReader tr=new StreamReader(ms))
                            {
                                txtblock01.Text = tr.ReadToEnd();
                               
                            }
                        }
                        //序列化对象
                       
                        using (XmlWriter xw=XmlWriter.Create(sb,new XmlWriterSettings { Indent = true }) )
                        {
                            XamlWriter.Save(richRB01.Document,xw);
                        }
                        txtblock02.Text = sb.ToString();
                    }
                }

                效果:

        5,不可变更布局的固定文档(可用于打印预览)。

                固定文档有Pdf与微软的xps等,以下以xps文档为例:

                通过只读容器DocumentViewer容器展现xps文件。

        
                
            
        //添加程序集:ReachFrameWork
        using System.Windows.Xps.Packaging;
         public partial class MainWindow : Window
            {
                public MainWindow()
                {
                    InitializeComponent();
                    XpsDocument document = new XpsDocument("../../1.xps", System.IO.FileAccess.ReadWrite);
                    viewer01.Document = document.GetFixedDocumentSequence();
                }
            }

                效果:

                注明:

                DocumentViewer.Document:类型为IDocumentPaginatorSource,虽然FlowDocument实现IDocumentPaginatorSource,但是DocumentViewer 仅支持 FixedDocument 或 FixedDocumentSequence 文档。

                DocumentViewer如何用于打印预览,参考下一章。

转载请注明来自码农世界,本文标题:《WPF之文档与RichTextBox》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,61人围观)参与讨论

还没有评论,来说两句吧...

Top