WPF文档处理及注解功能深度解析
立即解锁
发布时间: 2025-08-26 02:04:07 阅读量: 17 订阅数: 19 

### WPF文档处理及注解功能深度解析
#### 1. 文档加载与保存
在处理文档时,加载和保存是基础操作。加载文档时,若使用如下代码:
```csharp
else
{
documentTextRange.Load(fs, DataFormats.Xaml);
}
```
此代码在文件未找到、无法访问或无法按指定格式加载时会抛出异常,因此需将其包裹在异常处理程序中。无论以何种方式加载文档内容,最终都会转换为`FlowDocument`以便在`RichTextBox`中显示。为研究文档内容,可编写简单例程将`FlowDocument`内容转换为字符串,示例代码如下:
```csharp
// Copy the document content to a MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
TextRange range = new TextRange(richTextBox.Document.ContentStart,
richTextBox.Document.ContentEnd);
range.Save(stream, DataFormats.Xaml);
stream.Position = 0;
// Read the content from the stream and display it in a text box.
using (StreamReader r = new StreamReader(stream))
{
txtFlowDocumentMarkup.Text = r.ReadToEnd();
}
}
```
此技巧在调试时可用于研究文档编辑后标记的变化。
保存文档可使用`TextRange`对象,需提供两个`TextPointer`对象分别标识内容的起始和结束位置,然后调用`TextRange.Save()`方法并指定导出格式,如以下代码:
```csharp
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter =
"XAML Files (*.xaml)|*.xaml|RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";
if (saveFile.ShowDialog() == true)
{
// Create a TextRange around the entire document.
TextRange documentTextRange = new TextRange(
richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
// If this file exists, it's overwritten.
using (FileStream fs = File.Create(saveFile.FileName))
{
if (Path.GetExtension(saveFile.FileName).ToLower() == ".rtf")
{
documentTextRange.Save(fs, DataFormats.Rtf);
}
else
{
documentTextRange.Save(fs, DataFormats.Xaml);
}
}
}
```
使用XAML格式保存文档时,顶级元素是`Section`而非`FlowDocument`。同时,不要尝试使用`TextRange.Load()`方法加载包含顶级`FlowDocument`、`Page`或`Window`元素的XAML文件,因为无法成功解析。若有包含顶级`FlowDocument`元素的XAML文件,可使用`XamlReader.Load()`方法创建相应的`FlowDocument`对象。
#### 2. 选中文本格式化
通过构建简单的富文本编辑器可深入了解`RichTextBox`控件。在编辑器中,工具栏按钮可让用户快速应用加粗、倾斜和下划线格式。这些按钮使用`RichTextBox.Selection`属性获取当前选中文本的`TextSelection`对象。
应用加粗格式可使用如下代码:
```csharp
richTextBox.Selection.ApplyPropertyValue(
TextElement.FontWeightProperty, FontWeights.Bold);
```
若要切换加粗格式,需先检查是否已应用加粗格式:
```csharp
Object obj = richTextBox.Selection.GetPropertyValue(
TextElement.FontWeightProperty);
```
若选中文本包含加粗和正常文本,`GetPropertyValue()`方法会返回`DependencyProperty.UnsetValue`。可按以下代码处理这种情况:
```csharp
Object obj = richTextBox.Selection.GetPropertyValue(
TextElement.FontWeightProperty);
if (obj == DependencyProperty.UnsetValue)
{
TextRange range = new TextRange(richTextBox.Selection.Start,
richTextBox.Selection.Start);
obj = range.GetPropertyValue(TextElement.FontWeightProperty);
}
FontWeight fontWeight = (FontWeight)obj;
if (fontWeight == FontWeights.Bold)
fontWeight = FontWeights.Normal;
else
fontWeight = FontWeights.Bold;
richTextBox.Selection.ApplyPropertyValue(
TextElement.FontWeightProperty, fontWeight);
```
若用户未选中文本就触发加粗命令,可检查包含该文本的段落的字体粗细并进行切换:
```csharp
if (richTextBox.Selection.Text == "")
{
FontWeight fontWeight = richTextBox.Selection.Start.Paragraph.FontWeight;
if (fontWeight == FontWeights.Bold)
fontWeight = FontWeights.Normal;
else
fontWeight = FontWeights.Bold;
richTextBox.Selection.Start.Paragraph.FontWeight = fontWeight;
}
```
获取选中文本的纯文本可使用`TextRange.Text`属性。`TextRange`类和`RichTextBox`类还提供了许多操作文本的方法,如获取字符偏移量、计算行数和导航文档流元素等,可查阅Visual Studio帮助获取更多信息。
##
0
0
复制全文
相关推荐






