PrintControllerBase.cs 5.4 KB

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