WPF预览并打印FlowDocument

打印 上一主题 下一主题

主题 912|帖子 912|积分 2736

在前面的文章中,介绍过通过FixedDocument进行预览并打印。
WPF使用XAML实现报表的一种思绪(支持外部加载) - zhaotianff - 博客园

但是有时候我们可能使用的是FlowDocument,例如在RichTextBox中时。
如何对FlowDocument进行预览并打印呢。

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

留意:需要引用 ReachFramework程序集




例如我们有如下 的FlowDocument

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


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

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


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

  1. 1 private void Button_Click(object sender, RoutedEventArgs e)
  2. 2 {
  3. 3     try
  4. 4     {
  5. 5         FlowDocument flowDocument = (FlowDocument)Application.LoadComponent(new Uri("FlowDocument1.xaml", UriKind.RelativeOrAbsolute));
  6. 6
  7. 7         string temp = System.IO.Path.GetTempFileName();
  8. 8         if (File.Exists(temp) == true)
  9. 9             File.Delete(temp);
  10. 10
  11. 11         XpsDocument xpsDoc = new XpsDocument(temp, FileAccess.ReadWrite);
  12. 12
  13. 13         XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
  14. 14
  15. 15         xpsWriter.Write((flowDocument as IDocumentPaginatorSource).DocumentPaginator);
  16. 16
  17. 17         docv.Document = xpsDoc.GetFixedDocumentSequence();
  18. 18
  19. 19         xpsDoc.Close();
  20. 20     }
  21. 21     catch (Exception ex)
  22. 22     {
  23. 23         MessageBox.Show(ex.Message);
  24. 24     }
  25. 25 }
复制代码


示例代码
下载

参考资料
WPF: “Converting a WPF FlowDocument to an XPS FixedDocument”; Derek Smythe - the funky knowledge base

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

张国伟

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表