1. 程式人生 > >wpf中xps列印

wpf中xps列印

       /// <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
        }
 
 }

相關推薦

wpfxps列印

       /// <summary>         /// 將多個RichTextBox內容儲存為XPS列印檔案         /// </summary>         /// <param name="PageSize">分

基於C#在WPF使用斑馬印表機進行列印

最近在專案中接手了一個比較有挑戰性的模組——用斑馬印表機將需要列印的內容打印出來。苦苦折騰了兩天,總算有所收穫,就發到網上來騙騙分數-_-||     專案中使用的印表機型號為GX430t的印表機,接手的時候,自己對於印表機這塊兒是眼前一抹黑,啥都不知道。沒辦法一步步來

WPF的簡單列印

在WPF中,Grid、Canvas等介面的佈局都支援利用PrintDialog進行簡單列印。首先新建一個專案,Xmal程式碼如下: <Window x:Class="Print.MainWindow" xmlns="http://schemas.mic

WPF把Canvas儲存為圖片,文字檔案,xps檔案

由於wpf的UI使用xaml來表達的,所以我們們可利用這個優點,把WPF中的xaml元素另存為各樣的檔案,在很多時候我們都不須要這樣的操作。把xaml儲存為圖片、字串、XPS等等。這裡我寫了一些方法,以供大家參考.。 注意:以下儲存操作前,一定要確保引數中的canvas

WPF查看PDF文件 - 基於開源的MoonPdfPanel (無需安裝任何PDF閱讀器)問題匯總

報錯 博客 mil iss 用法 責任 位置 通過 連接 致敬Yang-Fei—有一個簡單的用法示例: http://www.cnblogs.com/yang-fei/p/4885570.html。 寫MoonPdfPanel的哥們關於這個開源軟件的實現介紹

wpf 使用 ttf

打包 logs p s http text 添加 unicode編碼 con 阿裏   因為實現ImageButton接觸ttf在wpf中的使用,ttf是圖標按鈕的集合文件。可在阿裏www.iconfont.cn網站選擇下載。首先選擇所需圖標添加到購物車,選擇完成後從購物車

WPF讓Combobox具有查找功能

xaml 一個 rim 當前 tar component 代碼 opd 添加 需求:由於combobox綁定的數據源數量比較多,如果讓用戶在下拉列中查找,難免會不太方便,於是就需要讓combobox可以輸入內容,並且希望根據用戶輸入的內容去自動匹配,這樣效率就會提高! 先在

C#の----Func,Action,predicate在WPF的應用

邏輯 clas color for edi sender fun document pub 首先介紹下,winform中可以用this.invoke來實現;wpf中要使用調度器Control.Despite.invoke; (Action)(()=> { })和 n

WPF的Command命令詳解

cti system tle command location eric 自己的 pri edit 在WPF中使用命令的步驟很簡單 1.創建命令 2.綁定命令 3.設置命令源 4.設置命令目標 WPF中命令的核心是System.Windows.Input.ICommand接

WPF如何使用RelativeSource綁定

背景 三種 pre light 相同 set bold csharp value 在WPF綁定的時候,指定綁定源時,有一種辦法是使用RelativeSource。 這種辦法的意思是指當前元素和綁定源的位置關系。 第一種關系: Self 舉一個最簡單的例子:在一個StackP

【.net 深呼吸】WPF 的父子窗口

退出 深呼吸 女孩 eww 技術分享 title 關閉窗口 adding clas 與 WinForm 不同,WPF 並沒有 MDI 窗口,但 WPF 的窗口之間是可以存在“父子”關系的。 我們會發現,Window 類公開了一個屬性叫 Owner,

WPF入門教程系列十七——WPF的數據綁定(三)

控件 命名空間 布局 獲得 key gif lns return 生日 四、 XML數據綁定 這次我們來學習新的綁定知識,XML數據綁定。XmlDataProvider 用來綁定 XML 數據,該XML數據可以是嵌入.Xmal文件的 XmlDataProvider

WPF入門教程系列十五——WPF的數據綁定(一)

不同 pan cnblogs 雙向 one tap copy msd tac 使用Windows Presentation Foundation (WPF) 可以很方便的設計出強大的用戶界面,同時 WPF提供了數據綁定功能。WPF的數據綁定跟Winform與ASP.NET

WPFListBox的綁定

效果 ttr sent idt 獲取 我們 數組 inf word WPF中列表式控件派生自ItemsControl類,繼承了ItemsSource屬性。ItemsSource屬性可以接收一個IEnumerable接口派生類的實例作為自己的值(所有可被叠代遍歷的集合都實現了

正確理解WPF的TemplatedParent

ted 不存在 htm each fin sent 直接 .com dpa http://www.cnblogs.com/mgen/archive/2011/08/31/2160581.html (註:Logical Tree中文稱為邏輯樹,Visual Tree中文稱為

ComboBox在WPF的綁定示例:綁定項、集合、轉換,及其源代碼

ati .com med owa panel center generic tle summary 在WPF的Xaml中為ComboBox綁定數據時,由於參數很多,很容易混淆,在ListView中使用更是如此。本文通過對ComboBox在窗口和在ListView中綁定對象的

WPF的數據綁定Data Binding使用小結

表示 ins preview info formate sso eas key 多個 完整的數據綁定的語法說明可以在這裏查看: http://www.nbdtech.com/Free/WpfBinding.pdf MSDN資料: Data Binding: Part 1 h

WPF的Command事件綁定

mode 技術 activit 安裝 xaml 獲取 由於 tcl text 在項目中使用Command綁定能夠使我們的代碼更加的符合MVVM模式。不了解的同學可能不清楚,只有繼承自ButtonBase類的元素才可以直接綁定Command(Button、CheckBox、R

WPF的TreeView入門

.com href ref wpf www. TreeView com blog div https://www.cnblogs.com/izualx/archive/2011/02/03/1949055.htmlWPF中的TreeView入門

WPFPopup上的textbox無法切換到中文輸入法

gist () efault use rom switch -i 輸入 spl As Marco Zhou has said in the msdn forum (http://social.msdn.microsoft.com/Forums/en-US/wpf/threa