当前位置:   article > 正文

WPF之文档与RichTextBox

WPF之文档与RichTextBox

1,文档分类:

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

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

2,文档容器:

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

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

        示例:

  1. <FlowDocumentScrollViewer IsSelectionEnabled="False">
  2. <FlowDocument Background="SkyBlue">
  3. ....
  4. </FlowDocument>
  5. </FlowDocumentScrollViewer>

        效果:

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

        示例

  1. <FlowDocumentPageViewer >
  2. <FlowDocument ColumnWidth="200" IsColumnWidthFlexible="True" ColumnRuleBrush="SkyBlue" ColumnRuleWidth="2" ColumnGap="30" >
  3. <Paragraph>
  4.   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.
  5. </Paragraph>
  6. <Paragraph>
  7.   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.
  8. </Paragraph>
  9. <Paragraph>
  10.   My mother got home.We said,"Happy Birthday!"My mother smiled.We had our supper together.My mother said,"Thank you,my daughter!"
  11. </Paragraph>
  12. <Paragraph>
  13.  How happy we were!
  14. </Paragraph>
  15. <Paragraph>
  16. <Image Source="Img/1.jpg"></Image>
  17. </Paragraph>
  18. </FlowDocument>
  19. </FlowDocumentPageViewer>

         效果:

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

固定文档容器:

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

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

        示例:

 <DocumentViewer  x:Name="viewer"></DocumentViewer>  
  1. public PreView(IDocumentPaginatorSource paginator)
  2. {
  3. InitializeComponent();
  4. viewer.Document = paginator;
  5. }

        效果:

 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:列与列之间空白宽度。

        示例:

  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="293*"/>
  4. <RowDefinition Height="27*"/>
  5. </Grid.RowDefinitions>
  6. <FlowDocumentScrollViewer IsSelectionEnabled="False">
  7. <FlowDocument Background="SkyBlue">
  8. <Paragraph x:Name="para01" TextIndent="32">明月几时有</Paragraph>
  9. <Paragraph>把酒问青天</Paragraph>
  10. <Paragraph >
  11. <Run>Top Programming languages: </Run></Paragraph>
  12. <List MarkerOffset="4" MarkerStyle="LowerRoman">
  13. <ListItem>
  14. <Paragraph >C#</Paragraph>
  15. <Paragraph>VB.Net</Paragraph>
  16. </ListItem>
  17. <ListItem>
  18. <Paragraph>JavaScript</Paragraph>
  19. </ListItem>
  20. </List>
  21. <Table BorderBrush="White" BorderThickness="2">
  22. <Table.Columns>
  23. <TableColumn Width="*"></TableColumn>
  24. <TableColumn Width="*"></TableColumn>
  25. <TableColumn Width="3*"></TableColumn>
  26. </Table.Columns>
  27. <TableRowGroup >
  28. <TableRow Paragraph.TextAlignment="Center" FontWeight="Bold" >
  29. <!--TableCell的内容为块-->
  30. <TableCell BorderBrush="Black" BorderThickness="1">
  31. <Paragraph>Rank</Paragraph>
  32. </TableCell>
  33. <TableCell BorderBrush="Black" BorderThickness="1">
  34. <Paragraph>Name</Paragraph>
  35. </TableCell>
  36. <TableCell BorderBrush="Black" BorderThickness="1" >
  37. <Paragraph>Population</Paragraph>
  38. </TableCell>
  39. </TableRow>
  40. <TableRow Paragraph.TextAlignment="Center">
  41. <TableCell>
  42. <Paragraph>1</Paragraph>
  43. </TableCell>
  44. <TableCell>
  45. <Paragraph>Rome</Paragraph>
  46. </TableCell>
  47. <TableCell>
  48. <Paragraph>45000</Paragraph>
  49. </TableCell>
  50. </TableRow>
  51. <TableRow Paragraph.TextAlignment="Center">
  52. <TableCell>
  53. <Paragraph>1</Paragraph>
  54. </TableCell>
  55. <TableCell ColumnSpan="2" BorderBrush="Black" BorderThickness="1">
  56. <Paragraph>Rome</Paragraph>
  57. </TableCell>
  58. </TableRow>
  59. </TableRowGroup>
  60. </Table>
  61. <Section Background="AliceBlue" >
  62. <Paragraph xml:space="preserve"> 雪山 飞狐</Paragraph>
  63. <Paragraph >连城诀</Paragraph>
  64. <List MarkerStyle="LowerLatin">
  65. <List.ListItems>
  66. <ListItem>
  67. <Paragraph>天龙八部</Paragraph>
  68. </ListItem>
  69. <ListItem>
  70. <Paragraph>射雕英雄传</Paragraph>
  71. </ListItem>
  72. </List.ListItems>
  73. </List>
  74. </Section>
  75. </FlowDocument>
  76. </FlowDocumentScrollViewer>
  77. </Grid>

        效果:

        注明:

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

        例如:

  1. <Paragraph TextIndent="32">
  2. These are the release
  3. <Span Tag="Plural Noun">donkeys</Span>
  4. for
  5. <Span Tag="Proper Noun">New York</Span>
  6. version 1.2.3
  7. </Paragraph>

        自动隐式转换为:

  1. <Paragraph TextIndent="32">
  2. <Run>These are the release</Run>
  3. <Span Tag="Plural Noun">donkeys</Span>
  4. <Run>for</Run>
  5. <Span Tag="Proper Noun">New York</Span>
  6. <Run>version 1.2.3</Run>
  7. </Paragraph>

        在流文档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使其响应。

        示例:

  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="71*"/>
  4. <RowDefinition Height="9*"/>
  5. </Grid.RowDefinitions>
  6. <Grid.ColumnDefinitions>
  7. <ColumnDefinition Width="244*"/>
  8. <ColumnDefinition Width="273*"/>
  9. <ColumnDefinition Width="273*"/>
  10. </Grid.ColumnDefinitions>
  11. <RichTextBox x:Name="richRB01" BorderBrush="SkyBlue" BorderThickness="1" Margin="5" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
  12. </RichTextBox>
  13. <Button x:Name="btnRead" Grid.Row="1" Click="btnRead_Click">Read Rtf Document</Button>
  14. <ScrollViewer Grid.Column="1">
  15. <TextBlock x:Name="txtblock01" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Background="AliceBlue" Margin="5"></TextBlock>
  16. </ScrollViewer>
  17. <ScrollViewer Grid.Column="2" >
  18. <TextBlock x:Name="txtblock02" TextWrapping="Wrap" Background="AliceBlue" Margin="5"></TextBlock>
  19. </ScrollViewer>
  20. </Grid>
  1. private void btnRead_Click(object sender, RoutedEventArgs e)
  2. {
  3. OpenFileDialog ofd = new OpenFileDialog();
  4. ofd.Filter = "Rft文件|*.rtf";
  5. if(ofd.ShowDialog()== true)
  6. {
  7. TextRange range = new TextRange(richRB01.Document.ContentStart, richRB01.Document.ContentEnd);
  8. using (FileStream fs =new FileStream(ofd.FileName,FileMode.Open))
  9. {
  10. range.Load(fs, DataFormats.Rtf);
  11. }
  12. //将RichtextBox中的document转为Xaml
  13. TextRange range2 = new TextRange(richRB01.Document.ContentStart, richRB01.Document.ContentEnd);
  14. StringBuilder sb = new StringBuilder();
  15. using (MemoryStream ms=new MemoryStream())
  16. {
  17. range2.Save(ms, DataFormats.Xaml);
  18. ms.Seek(0, SeekOrigin.Begin);
  19. using (TextReader tr=new StreamReader(ms))
  20. {
  21. txtblock01.Text = tr.ReadToEnd();
  22. }
  23. }
  24. //序列化对象
  25. using (XmlWriter xw=XmlWriter.Create(sb,new XmlWriterSettings { Indent = true }) )
  26. {
  27. XamlWriter.Save(richRB01.Document,xw);
  28. }
  29. txtblock02.Text = sb.ToString();
  30. }
  31. }

        效果:

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

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

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

  1. <Grid>
  2. <DocumentViewer x:Name="viewer01"></DocumentViewer>
  3. </Grid>
  1. //添加程序集:ReachFrameWork
  2. using System.Windows.Xps.Packaging;
  3. public partial class MainWindow : Window
  4. {
  5. public MainWindow()
  6. {
  7. InitializeComponent();
  8. XpsDocument document = new XpsDocument("../../1.xps", System.IO.FileAccess.ReadWrite);
  9. viewer01.Document = document.GetFixedDocumentSequence();
  10. }
  11. }

        效果:

        注明:

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/613359
推荐阅读
相关标签
  

闽ICP备14008679号