1,文档分类:
文档分为流文档(FlowDocument,布局可变)与固定文档(xps,pdf等布局不可变)。
注明:流内容元素继承自ContentElement和FrameworkContenteElement而非常用的UIElement和FrameworkElement
2,文档容器:
流文档(FlowDocument)容器(只读,共三个)。
- FlowDocumentScrollViews:不能分页也不能分列显示。
示例:
<FlowDocumentScrollViewer IsSelectionEnabled="False">
<FlowDocument Background="SkyBlue">
....
</FlowDocument>
</FlowDocumentScrollViewer>
效果:
- FlowDocumentPageViews:可进行分页和分列显示。
示例
<FlowDocumentPageViewer >
<FlowDocument ColumnWidth="200" IsColumnWidthFlexible="True" ColumnRuleBrush="SkyBlue" ColumnRuleWidth="2" ColumnGap="30" >
<Paragraph>
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.
</Paragraph>
<Paragraph>
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.
</Paragraph>
<Paragraph>
My mother got home.We said,"Happy Birthday!"My mother smiled.We had our supper together.My mother said,"Thank you,my daughter!"
</Paragraph>
<Paragraph>
How happy we were!
</Paragraph>
<Paragraph>
<Image Source="Img/1.jpg"></Image>
</Paragraph>
</FlowDocument>
</FlowDocumentPageViewer>
效果:
- FlowDocumentReader:可进行分页和分列显示,同时增加了查找功能,是三个中开销最大的容器。
固定文档容器:
显示格式内容已固定的文档(例如xps文档),一般用于文档预览呈现。
注明:XPS文件实际上是包含压缩文件库的ZIP文件,包含字体,图像以及用于每一页的文本内容,可将XPS的扩展名修改为.zip并打开它。
示例:
<DocumentViewer x:Name="viewer"></DocumentViewer>
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:列与列之间空白宽度。
示例:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="293*"/>
<RowDefinition Height="27*"/>
</Grid.RowDefinitions>
<FlowDocumentScrollViewer IsSelectionEnabled="False">
<FlowDocument Background="SkyBlue">
<Paragraph x:Name="para01" TextIndent="32">明月几时有</Paragraph>
<Paragraph>把酒问青天</Paragraph>
<Paragraph >
<Run>Top Programming languages: </Run></Paragraph>
<List MarkerOffset="4" MarkerStyle="LowerRoman">
<ListItem>
<Paragraph >C#</Paragraph>
<Paragraph>VB.Net</Paragraph>
</ListItem>
<ListItem>
<Paragraph>JavaScript</Paragraph>
</ListItem>
</List>
<Table BorderBrush="White" BorderThickness="2">
<Table.Columns>
<TableColumn Width="*"></TableColumn>
<TableColumn Width="*"></TableColumn>
<TableColumn Width="3*"></TableColumn>
</Table.Columns>
<TableRowGroup >
<TableRow Paragraph.TextAlignment="Center" FontWeight="Bold" >
<!--TableCell的内容为块-->
<TableCell BorderBrush="Black" BorderThickness="1">
<Paragraph>Rank</Paragraph>
</TableCell>
<TableCell BorderBrush="Black" BorderThickness="1">
<Paragraph>Name</Paragraph>
</TableCell>
<TableCell BorderBrush="Black" BorderThickness="1" >
<Paragraph>Population</Paragraph>
</TableCell>
</TableRow>
<TableRow Paragraph.TextAlignment="Center">
<TableCell>
<Paragraph>1</Paragraph>
</TableCell>
<TableCell>
<Paragraph>Rome</Paragraph>
</TableCell>
<TableCell>
<Paragraph>45000</Paragraph>
</TableCell>
</TableRow>
<TableRow Paragraph.TextAlignment="Center">
<TableCell>
<Paragraph>1</Paragraph>
</TableCell>
<TableCell ColumnSpan="2" BorderBrush="Black" BorderThickness="1">
<Paragraph>Rome</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
<Section Background="AliceBlue" >
<Paragraph xml:space="preserve"> 雪山 飞狐</Paragraph>
<Paragraph >连城诀</Paragraph>
<List MarkerStyle="LowerLatin">
<List.ListItems>
<ListItem>
<Paragraph>天龙八部</Paragraph>
</ListItem>
<ListItem>
<Paragraph>射雕英雄传</Paragraph>
</ListItem>
</List.ListItems>
</List>
</Section>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
效果:
注明:
可以直接在paragraph内直接添加文本,它会隐式创建Run元素并用其包裹文本。
例如:
<Paragraph TextIndent="32">
These are the release
<Span Tag="Plural Noun">donkeys</Span>
for
<Span Tag="Proper Noun">New York</Span>
version 1.2.3
</Paragraph>
自动隐式转换为:
<Paragraph TextIndent="32">
<Run>These are the release</Run>
<Span Tag="Plural Noun">donkeys</Span>
<Run>for</Run>
<Span Tag="Proper Noun">New York</Span>
<Run>version 1.2.3</Run>
</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使其响应。
示例:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="71*"/>
<RowDefinition Height="9*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="244*"/>
<ColumnDefinition Width="273*"/>
<ColumnDefinition Width="273*"/>
</Grid.ColumnDefinitions>
<RichTextBox x:Name="richRB01" BorderBrush="SkyBlue" BorderThickness="1" Margin="5" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
</RichTextBox>
<Button x:Name="btnRead" Grid.Row="1" Click="btnRead_Click">Read Rtf Document</Button>
<ScrollViewer Grid.Column="1">
<TextBlock x:Name="txtblock01" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" Background="AliceBlue" Margin="5"></TextBlock>
</ScrollViewer>
<ScrollViewer Grid.Column="2" >
<TextBlock x:Name="txtblock02" TextWrapping="Wrap" Background="AliceBlue" Margin="5"></TextBlock>
</ScrollViewer>
</Grid>
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文件。
<Grid>
<DocumentViewer x:Name="viewer01"></DocumentViewer>
</Grid>
//添加程序集: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如何用于打印预览,参考下一章。