123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Drawing.Printing;
- using System.IO;
- using Microsoft.Reporting.WebForms;
- using System.Drawing.Imaging;
- using System.Drawing;
- using System.Collections.Specialized;
- using System.Text;
- using System.Globalization;
- namespace EmisTerminal
- {
-
-
-
-
-
- public class ReportPrintDocument : PrintDocument
- {
- private PageSettings m_pageSettings;
- private int m_currentPage;
- private List<Stream> m_pages = new List<Stream>();
- public ReportPrintDocument(ServerReport serverReport)
- : this((Report)serverReport)
- {
- RenderAllServerReportPages(serverReport);
- }
- public ReportPrintDocument(LocalReport localReport)
- : this((Report)localReport)
- {
- RenderAllLocalReportPages(localReport);
- }
- private ReportPrintDocument(Report report)
- {
-
- ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();
-
-
-
- m_pageSettings = new PageSettings();
- m_pageSettings.PaperSize = reportPageSettings.PaperSize;
- m_pageSettings.Margins = reportPageSettings.Margins;
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- foreach (Stream s in m_pages)
- {
- s.Dispose();
- }
- m_pages.Clear();
- }
- }
- protected override void OnBeginPrint(PrintEventArgs e)
- {
- base.OnBeginPrint(e);
- m_currentPage = 0;
- }
- protected override void OnPrintPage(PrintPageEventArgs e)
- {
- base.OnPrintPage(e);
- Stream pageToPrint = m_pages[m_currentPage];
- pageToPrint.Position = 0;
-
- using (Metafile pageMetaFile = new Metafile(pageToPrint))
- {
- Rectangle adjustedRect = new Rectangle(
- e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
- e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
- e.PageBounds.Width,
- e.PageBounds.Height);
-
- e.Graphics.FillRectangle(Brushes.White, adjustedRect);
-
- e.Graphics.DrawImage(pageMetaFile, adjustedRect);
-
- m_currentPage++;
- e.HasMorePages = m_currentPage < m_pages.Count;
- }
- }
- protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
- {
- e.PageSettings = (PageSettings)m_pageSettings.Clone();
- }
- private void RenderAllServerReportPages(ServerReport serverReport)
- {
- try
- {
- string deviceInfo = CreateEMFDeviceInfo();
-
-
-
-
- NameValueCollection firstPageParameters = new NameValueCollection();
- firstPageParameters.Add("rs:PersistStreams", "True");
-
-
- NameValueCollection nonFirstPageParameters = new NameValueCollection();
- nonFirstPageParameters.Add("rs:GetNextStream", "True");
- string mimeType;
- string fileExtension;
- Stream pageStream = serverReport.Render("Image", deviceInfo, firstPageParameters, out mimeType, out fileExtension);
-
- while (pageStream.Length > 0)
- {
- m_pages.Add(pageStream);
- pageStream = serverReport.Render("Image", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
- }
- }
- catch (Exception e)
- {
- throw new Exception("possible missing information :: " + e.Message);
- }
- }
- private void RenderAllLocalReportPages(LocalReport localReport)
- {
- try
- {
- string deviceInfo = CreateEMFDeviceInfo();
- Warning[] warnings;
- localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
- }
- catch (Exception e)
- {
- throw new Exception("error :: " + e);
- }
- }
- private Stream LocalReportCreateStreamCallback(
- string name,
- string extension,
- Encoding encoding,
- string mimeType,
- bool willSeek)
- {
- MemoryStream stream = new MemoryStream();
- m_pages.Add(stream);
- return stream;
- }
- private string CreateEMFDeviceInfo()
- {
- PaperSize paperSize = m_pageSettings.PaperSize;
- Margins margins = m_pageSettings.Margins;
-
-
- return string.Format(
- CultureInfo.InvariantCulture,
- "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
- ToInches(margins.Top + 20),
- ToInches(margins.Left),
- ToInches(margins.Right),
- ToInches(margins.Bottom),
- ToInches(paperSize.Height),
- ToInches(paperSize.Width));
- }
- private static string ToInches(int hundrethsOfInch)
- {
- double inches = hundrethsOfInch / 100.0;
- return inches.ToString(CultureInfo.InvariantCulture) + "in";
- }
- }
- }
|