当前位置: 首页 > article >正文

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


http://www.kler.cn/a/580872.html

相关文章:

  • Python数据分析之数据处理与分析
  • 修改 Docker 网桥的 IP 范围
  • Oracle RAC配置原理详解:构建高可用与高性能的数据库集群
  • HTML 超链接(简单易懂较详细)
  • NO.29十六届蓝桥杯备战|string九道练习|reverse|翻转|回文(C++)
  • AI算法与应用 全栈开发 前端开发 后端开发 测试开发 运维开发
  • 【阿里云】操作系统控制台——体验与测评
  • c#面试题整理3
  • 探索高性能AI识别和边缘计算 | NVIDIA Jetson Orin Nano 8GB 开发套件的全面测评
  • FreeRTOS第18篇:FreeRTOS链表实现细节06_遍历指针(pxIndex)与调度器的高效协同
  • 2路模拟量同步输出卡、任意波形发生器卡—PCIe9100数据采集卡
  • Flutter中网络图片加载显示Image.network的具体用法
  • [免费]微信小程序(图书馆)自习室座位预约管理系统(SpringBoot后端+Vue管理端)(高级版)【论文+源码+SQL脚本】
  • Vue前端开发-Coupon组件
  • 时序数据库 InfluxDB 3.0 版本性能实测报告:写入吞吐量提升效果验证
  • 鸿蒙跨平台框架ArkUI-X
  • 后 Safe 时代:多签钱包安全新范式与防范前端攻击的新思路
  • Windows控制台函数:设置文字颜色样式函数SetConsoleTextAttribute()
  • SQL 窗口函数之lead() over(partition by ) 和 lag() over(partition by )
  • 批量将 Excel 转换 PDF/Word/CSV以及图片等其它格式