1. 程式人生 > >ActiveReports之直接打印報表

ActiveReports之直接打印報表

mage private read display 多個 more 邊距 brush 分享

前提概要參考:http://www.cnblogs.com/liandy0906/p/7936524.html

技術分享圖片
        private int m_currentPageIndex;

        /// <summary>
        /// 聲明一個Stream對象的列表用來保存報表的輸出數據,LocalReport對象的Render方法會將報表按頁輸出為多個Stream對象。
        /// </summary>
        private IList<Stream> m_streams;

        private bool isLandSapces = false
; private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { //如果需要將報表輸出的數據保存為文件,請使用FileStream對象。 Stream stream = new MemoryStream(); m_streams.Add(stream); return stream; }
public void PrintStream(LocalReport rvDoc) { //獲取LocalReport中的報表頁面方向 isLandSapces = rvDoc.GetDefaultPageSettings().IsLandscape; Export(rvDoc); PrintSetting(); Dispose(); } private void Export(LocalReport report) {
string deviceInfo = @"<DeviceInfo> <OutputFormat>EMF</OutputFormat> </DeviceInfo>"; Warning[] warnings; m_streams = new List<Stream>(); //將報表的內容按照deviceInfo指定的格式輸出到CreateStream函數提供的Stream中。 report.Render("Image", deviceInfo, CreateStream, out warnings); foreach (Stream stream in m_streams) stream.Position = 0; } private void PrintSetting() { if (m_streams == null || m_streams.Count == 0) throw new Exception("錯誤:沒有檢測到打印數據流"); //聲明PrintDocument對象用於數據的打印 PrintDocument printDoc = new PrintDocument(); //獲取配置文件的清單打印機名稱 //System.Configuration.AppSettingsReader appSettings = new System.Configuration.AppSettingsReader(); printDoc.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();//指定打印機不顯示頁碼 //判斷指定的打印機是否可用 if (!printDoc.PrinterSettings.IsValid) { throw new Exception("錯誤:找不到打印機"); } else { //設置打印機方向遵從報表方向 printDoc.DefaultPageSettings.Landscape = isLandSapces; //聲明PrintDocument對象的PrintPage事件,具體的打印操作需要在這個事件中處理。 printDoc.PrintPage += new PrintPageEventHandler(PrintPage); m_currentPageIndex = 0; //設置打印機打印份數 printDoc.PrinterSettings.Copies = 1; //執行打印操作,Print方法將觸發PrintPage事件。 printDoc.Print(); } } private void PrintPage(object sender, PrintPageEventArgs ev) { //Metafile對象用來保存EMF或WMF格式的圖形, //我們在前面將報表的內容輸出為EMF圖形格式的數據流。 Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); //調整打印機區域的邊距 System.Drawing.Rectangle adjustedRect = new System.Drawing.Rectangle( ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, ev.PageBounds.Width, ev.PageBounds.Height); //繪制一個白色背景的報告 //ev.Graphics.FillRectangle(Brushes.White, adjustedRect); //獲取報告內容 //這裏的Graphics對象實際指向了打印機 ev.Graphics.DrawImage(pageImage, adjustedRect); //ev.Graphics.DrawImage(pageImage, ev.PageBounds); // 準備下一個頁,已確定操作尚未結束 m_currentPageIndex++; //設置是否需要繼續打印 ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } public void Dispose() { if (m_streams != null) { foreach (Stream stream in m_streams) stream.Close(); m_streams = null; } }
View Code

客戶端調用:PrintStream(rvDoc.LocalReport);

ActiveReports之直接打印報表