WPF预览并打印FlowDocument

在前面的文章中,介绍过通过FixedDocument进行预览并打印。

WPF使用XAML实现报表的一种思路(支持外部加载) - zhaotianff - 博客园

但是有时候我们可能使用的是FlowDocument ,例如在RichTextBox中时。

如何对FlowDocument进行预览并打印呢。

可以将FlowDocument 转换成XpsDocument ,然后再使用DocumentViewer进行预览并打印。

注意:需要引用 ReachFramework程序集

例如我们有如下 的FlowDocument

复制代码
 1 <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2               xmlns:local="clr-namespace:FlowDocumentPreviewAndPrint"
 3               ColumnWidth="400" FontSize="14" FontFamily="Georgia">
 4     <Paragraph>
 5         <Image Source="jk.png"></Image>
 6     </Paragraph>
 7     <Paragraph>
 8         <Run>Hello World</Run>
 9     </Paragraph>
10 </FlowDocument>

然后在主窗口中增加一个DocumentViewer

复制代码
 1 <wd:Window x:Class="FlowDocumentPreviewAndPrint.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:FlowDocumentPreviewAndPrint"
 7         xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
 8         mc:Ignorable="d"
 9         Title="MainWindow" Height="450" Width="800">
10     <Grid>
11         <Grid.RowDefinitions>
12             <RowDefinition/>
13             <RowDefinition Height="40"/>
14         </Grid.RowDefinitions>
15 
16         <DocumentViewer Name="docv">
17 
18         </DocumentViewer>
19 
20         <Grid Grid.Row="1">
21             <Button Content="加载" HorizontalAlignment="Center" VerticalAlignment="Center" Width="88" Height="28" Click="Button_Click"></Button>
22         </Grid>
23     </Grid>
24 </wd:Window>

当点击打印时,我们将FlowDocument转换成XpsDocument,并设置到DocumentViewer的Document中去。

复制代码
 1 private void Button_Click(object sender, RoutedEventArgs e)
 2 {
 3     try
 4     {
 5         FlowDocument flowDocument = (FlowDocument)Application.LoadComponent(new Uri("FlowDocument1.xaml", UriKind.RelativeOrAbsolute));
 6 
 7         string temp = System.IO.Path.GetTempFileName();

 8         if (File.Exists(temp) == true)
 9             File.Delete(temp);
10 
11         XpsDocument xpsDoc = new XpsDocument(temp, FileAccess.ReadWrite);
12 
13         XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
14 
15         xpsWriter.Write((flowDocument as IDocumentPaginatorSource).DocumentPaginator);
16 
17         docv.Document = xpsDoc.GetFixedDocumentSequence();
18 
19         xpsDoc.Close();
20     }
21     catch (Exception ex)
22     {
23         MessageBox.Show(ex.Message);
24     }
25 }

示例代码

下载

参考资料

WPF: "Converting a WPF FlowDocument to an XPS FixedDocument"; Derek Smythe - the funky knowledge base

相关推荐
wangnaisheng16 小时前
【WPF】Opacity 属性的使用
wpf
姬激薄21 小时前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)21 小时前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心1 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo2 天前
9.1.领域驱动设计
wpf
大道随心2 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠2 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go2 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet3 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf
冰茶_3 天前
WPF之绑定模式深入
学习·microsoft·微软·c#·wpf·绑定模式