PrintControllerBase.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using FastReport.Utils;
  2. using System;
  3. using System.Drawing.Printing;
  4. namespace FastReport.Print
  5. {
  6. internal abstract class PrintControllerBase
  7. {
  8. #region Fields
  9. private Report report;
  10. private PrintDocument doc;
  11. private PageNumbersParser pages;
  12. private ReportPage page;
  13. private int pageNo;
  14. private int printedPageCount;
  15. #endregion
  16. #region Properties
  17. public Report Report
  18. {
  19. get { return report; }
  20. }
  21. public PrintDocument Doc
  22. {
  23. get { return doc; }
  24. }
  25. public PageNumbersParser Pages
  26. {
  27. get { return pages; }
  28. }
  29. public ReportPage Page
  30. {
  31. get { return page; }
  32. set { page = value; }
  33. }
  34. public int PageNo
  35. {
  36. get { return pageNo; }
  37. set { pageNo = value; }
  38. }
  39. #endregion
  40. #region Private Methods
  41. private PaperSize FindPaperSize(PrinterSettings.PaperSizeCollection paperSizes,
  42. float paperWidth, float paperHeight, int rawKind)
  43. {
  44. if (paperSizes == null)
  45. return null;
  46. foreach (PaperSize ps in paperSizes)
  47. {
  48. // convert hundreds of inches to mm
  49. float psWidth = ps.Width / 100f * 25.4f;
  50. float psHeight = ps.Height / 100f * 25.4f;
  51. // check if page has the same kind and size
  52. bool sizeEqual = Math.Abs(paperWidth - psWidth) < 5 && Math.Abs(paperHeight - psHeight) < 5;
  53. if (sizeEqual)
  54. {
  55. if (rawKind == 0 || ps.RawKind == rawKind)
  56. return ps;
  57. }
  58. }
  59. return null;
  60. }
  61. #endregion
  62. #region Protected methods
  63. protected ReportPage GetNextPage()
  64. {
  65. pageNo = 0;
  66. if (Pages.GetPage(ref pageNo))
  67. return Report.PreparedPages.GetPage(pageNo);
  68. return null;
  69. }
  70. protected bool HasMorePages()
  71. {
  72. return Pages.Count > 0;
  73. }
  74. protected void SetPaperSize(ReportPage page, QueryPageSettingsEventArgs e)
  75. {
  76. float width = page.PaperWidth;
  77. float height = page.PaperHeight;
  78. if (page.Landscape)
  79. {
  80. width = page.PaperHeight;
  81. height = page.PaperWidth;
  82. }
  83. SetPaperSize(width, height, page.RawPaperSize, e);
  84. }
  85. protected void SetPaperSize(float paperWidth, float paperHeight, int rawKind, QueryPageSettingsEventArgs e)
  86. {
  87. PaperSize ps = null;
  88. // check PaperWidth, PaperHeight, RawKind
  89. if (rawKind != 0)
  90. ps = FindPaperSize(e.PageSettings.PrinterSettings.PaperSizes, paperWidth, paperHeight, rawKind);
  91. // check PaperWidth, PaperHeight only
  92. if (ps == null)
  93. ps = FindPaperSize(e.PageSettings.PrinterSettings.PaperSizes, paperWidth, paperHeight, 0);
  94. // paper size not found, create custom one
  95. if (ps == null)
  96. {
  97. ps = new PaperSize();
  98. ps.Width = (int)Math.Round(paperWidth / 25.4f * 100);
  99. ps.Height = (int)Math.Round(paperHeight / 25.4f * 100);
  100. }
  101. e.PageSettings.PaperSize = ps;
  102. }
  103. protected void SetPaperSource(ReportPage page, QueryPageSettingsEventArgs e)
  104. {
  105. int rawKind = Report.PrintSettings.PaperSource;
  106. // it's set to Automatic, try page.PaperSource
  107. if (rawKind == 7)
  108. {
  109. if (PageNo == 0)
  110. {
  111. rawKind = page.FirstPageSource;
  112. }
  113. else if (PageNo == Report.PreparedPages.Count - 1)
  114. {
  115. rawKind = page.LastPageSource;
  116. }
  117. else
  118. {
  119. rawKind = page.OtherPagesSource;
  120. }
  121. }
  122. // do not change paper source if it is AutomaticFeed
  123. if (rawKind == 7)
  124. return;
  125. foreach (PaperSource ps in e.PageSettings.PrinterSettings.PaperSources)
  126. {
  127. if (ps.RawKind == rawKind)
  128. {
  129. e.PageSettings.PaperSource = ps;
  130. return;
  131. }
  132. }
  133. }
  134. protected void StartPage(PrintPageEventArgs e)
  135. {
  136. printedPageCount++;
  137. Config.ReportSettings.OnProgress(Report,
  138. String.Format(Res.Get("Messages,PrintingPage"), printedPageCount), printedPageCount, 0);
  139. }
  140. protected void FinishPage(PrintPageEventArgs e)
  141. {
  142. if (Report.Aborted)
  143. e.Cancel = true;
  144. }
  145. #endregion
  146. #region Public methods
  147. public abstract void QueryPageSettings(object sender, QueryPageSettingsEventArgs e);
  148. public abstract void PrintPage(object sender, PrintPageEventArgs e);
  149. #endregion
  150. public PrintControllerBase(Report report, PrintDocument doc, int curPage)
  151. {
  152. this.report = report;
  153. this.doc = doc;
  154. pages = new PageNumbersParser(report, curPage);
  155. // select the printer
  156. if (!String.IsNullOrEmpty(report.PrintSettings.Printer))
  157. this.doc.PrinterSettings.PrinterName = report.PrintSettings.Printer;
  158. // print to file
  159. if (report.PrintSettings.PrintToFile)
  160. {
  161. this.doc.PrinterSettings.PrintFileName = report.PrintSettings.PrintToFileName;
  162. this.doc.PrinterSettings.PrintToFile = true;
  163. }
  164. // set job name
  165. if (!String.IsNullOrEmpty(report.ReportInfo.Name))
  166. this.doc.DocumentName = report.ReportInfo.Name;
  167. else
  168. this.doc.DocumentName = report.FileName;
  169. // set copies and collation
  170. if (report.PrintSettings.Copies < 1)
  171. report.PrintSettings.Copies = 1;
  172. this.doc.PrinterSettings.Copies = (short)report.PrintSettings.Copies;
  173. this.doc.PrinterSettings.Collate = report.PrintSettings.Collate;
  174. }
  175. }
  176. }