wpf中xps打印

本文档展示了如何在WPF中创建XPS文档,并实现打印预览功能。通过FixedPage数组,将RichTextBox内容转换为XPS文档,使用SaveXPS方法保存并写入文件,最后通过DocumentViewer展示预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       /// <summary>
        /// 将多个RichTextBox内容保存为XPS打印文件
        /// </summary>
        /// <param name="PageSize">分页页数</param>
        /// <param name="PageData">数据源</param>
      private  void PrintDataPage(int PageSize, string  PageData)
        {
           // ICollectionView paraCollection;

            if (PageData == "")
            {
                MessageBox.Show(string.Format("未查询到相应数据,无法打印!"));
                return;
            }
         
            FixedPage[] tt = new FixedPage[PageSize];
         
            int t = sp.Children.Count;


            for (int i = 0; i < t;i++ )//为FixedPage[] tt每页内容赋值
            {
                MyRichTextBox mb = sp.Children[0] as MyRichTextBox;
                sp.Children.Remove(mb);
                tt[i] = new FixedPage();
                tt[i].Children.Add(mb);


            }

 

            if (FileH.SaveXPS(tt, false, PageSize))
            {
                // MessageBox.Show(string.Format("文件保存成功"));
            }

            else
            {
                MessageBox.Show("取消文件保存");
            }
       
        }

 

 

 

 

 

 

 public class FileH
    {
        public string GetXPSFromDialog(bool isSaved)
        {
            if (isSaved)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();

                saveFileDialog.Filter = "XPS Document files (*.xps)|*.xps";
                saveFileDialog.FilterIndex = 1;

                if (saveFileDialog.ShowDialog() == true)
                {
                    return saveFileDialog.FileName;
                }
                else
                {
                    return null;
                }
            }
            else return string.Format("{0}\\printMB.xps", Environment.CurrentDirectory);//制造一个临时存储
        }


        /// <summary>
        /// 这个静态方法主要是显示选择对话框以提供文件的保存位置
        /// 将传入的FixedPage对象数组(多页)写入到.xps文件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="page"></param>
        /// <param name="isSaved"></param>
        /// <returns></returns>
        public static bool SaveXPS(FixedPage[] page, bool isSaved, int iPageCount)
        {
            FixedDocument fixedDoc = new FixedDocument();//创建一个文档
            fixedDoc.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);

            PageContent[] pageContent = new PageContent[iPageCount];
            for (int i = 0; i < iPageCount; i++)
            {
                pageContent[i] = new PageContent();
                ((IAddChild)pageContent[i]).AddChild(page[i]);
                fixedDoc.Pages.Add(pageContent[i]);//将对象加入到当前文档中
            }


            FileH fh = new FileH();
            string containerName = fh.GetXPSFromDialog(isSaved);
            if (containerName != null)
            {
                try
                {
                    File.Delete(containerName);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }

                XpsDocument _xpsDocument = new XpsDocument(containerName, FileAccess.Write);

                XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(_xpsDocument);
                xpsdw.Write(fixedDoc);//写入XPS文件
                _xpsDocument.Close();
                return true;
            }
            else return false;
        }


        static XpsDocument xpsPackage = null;
        public static void LoadDocumentViewer(string xpsFileName, DocumentViewer viewer)
        {
            XpsDocument oldXpsPackage = xpsPackage;//保存原来的XPS包
            xpsPackage = new XpsDocument(xpsFileName, FileAccess.Read, CompressionOption.NotCompressed);//从文件中读取XPS文档

            FixedDocumentSequence fixedDocumentSequence = xpsPackage.GetFixedDocumentSequence();//从XPS文档对象得到FixedDocumentSequence
            viewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;

            if (oldXpsPackage != null)
                oldXpsPackage.Close();
            xpsPackage.Close();
        }
    }

 

 

xps承载页面(打印预览)

<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 x:Class="RichEditorLibrary.PrintWindow"
 x:Name="Window"
 Title="PrintWindow"
 UseLayoutRounding="True"
 Width="640" Height="480" Loaded="Window_Loaded">

 <Grid x:Name="LayoutRoot">
        <DocumentViewer x:Name="docViewer"  />
 </Grid>
</Window>

 

后台代码:

/// <summary>
 /// Interaction logic for PrintWindow.xaml
 /// </summary>
 public partial class PrintWindow : Window
 {
  public PrintWindow()
  {
   this.InitializeComponent();
            setaa();
   // Insert code required on object creation below this point.
  }

        //传递一个公共的数据类
        public string fixedDocFile;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FileH.LoadDocumentViewer(fixedDocFile, docViewer);
           
        }

        public void setaa()
        {
            #region 设置全屏
            this.WindowState = System.Windows.WindowState.Maximized;
            //this.WindowStyle = System.Windows.WindowStyle.None;           
            //this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            this.Topmost = false;
            this.Left = 0.0;
            this.Top = 0.0;
            this.MaxWidth = SystemParameters.WorkArea.Width;
            this.MaxHeight = SystemParameters.WorkArea.Height;
            this.MinWidth = SystemParameters.WorkArea.Width;
            this.MinHeight = SystemParameters.WorkArea.Height;

            #endregion
        }
 
 }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值