DocumentView, XpsDocuemnt, FlowDocument, Table, Paragraph, Section
基本概論
在 C# 中要啟用列印功能, 工程實在浩大, 方法也很多. 在此僅列出本人實作的方式.
DocumentViewer <- XpsDocument <- FlowDocument <- Blocks <- Paragraph/Table/Section
DocumentViewer
DocumentViewer是最外圍預覽列印的元件, 同時也具有列印工具列
上圖灰色背景的部份, 即為DocumentViewer的範圍, 具有最上方的工具列, 中間的資料顯示區, 及最下方的搜尋詢功能
此元件可直接使用 DocumentViewer docViewer = new DocumentViewer() 產生物件, 然後將此元件崁入到 Grid之中. 崁入到Grid的方法為 grid.Children.Add(docViewer)
XpsDocument
有了DocumentViewer後, 接下來就是把要顯示的資料崁入其中. 負責處理資料顯示的元件為XpsDocument, , 是由微軟自行開發處理列印的專用格式.
在啟用XPS時, 需於專案按右鍵/加入/參考, 將ReachFramework及System.Printing打勾
XpsDocument其實不是資料的存放處, 真正存放資料的元件是FlowDocument. 也就是說, 要先把資料都安排在FlowDocument之中, 然後再利用XpsDocumentWriter 轉換成XpsDocument.
製作XpsDocument 會花費相當長的時間, 所以需使用執行緒進行此工作, 如下程式碼為製作XpsDocument的方法
