ScalePrintController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Drawing.Printing;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing.Drawing2D;
  6. using FastReport.Utils;
  7. using System.Drawing;
  8. namespace FastReport.Print
  9. {
  10. internal class ScalePrintController : PrintControllerBase
  11. {
  12. #region Fields
  13. private bool firstTime = true;
  14. #endregion
  15. #region Private Methods
  16. private float CorrectScaleForUnlimitedPage(float scale, bool sourceIsLandscape,
  17. float sheetWidth, float sheetHeight, float paperWidth, float paperHeight)
  18. {
  19. if (Page.UnlimitedWidth || Page.UnlimitedHeight)
  20. {
  21. switch (Report.PrintSettings.PagesOnSheet)
  22. {
  23. case PagesOnSheet.Two:
  24. if (sourceIsLandscape)
  25. {
  26. float twoPagesHeight = paperHeight * scale * 2;
  27. if (twoPagesHeight > sheetHeight)
  28. {
  29. scale *= twoPagesHeight / sheetHeight / 2;
  30. }
  31. }
  32. break;
  33. case PagesOnSheet.Eight:
  34. if (sourceIsLandscape)
  35. {
  36. float fourPagesHeight = paperHeight * scale * 4;
  37. if (fourPagesHeight > sheetHeight)
  38. {
  39. scale *= fourPagesHeight / sheetHeight / 2;
  40. }
  41. }
  42. break;
  43. }
  44. }
  45. return scale;
  46. }
  47. #endregion // Private Methods
  48. public override void QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
  49. {
  50. if (firstTime)
  51. Page = GetNextPage();
  52. firstTime = false;
  53. if (Page != null)
  54. {
  55. SetPaperSize(Report.PrintSettings.PrintOnSheetWidth, Report.PrintSettings.PrintOnSheetHeight,
  56. Report.PrintSettings.PrintOnSheetRawPaperSize, e);
  57. float paperWidth = Page.UnlimitedWidth ? Page.UnlimitedWidthValue / Units.Millimeters : Page.PaperWidth;
  58. float paperHeight = Page.UnlimitedHeight ? Page.UnlimitedHeightValue / Units.Millimeters : Page.PaperHeight;
  59. // rotate page if print 2 or 8 pages per sheet
  60. bool rotate = Report.PrintSettings.PagesOnSheet == PagesOnSheet.Two ||
  61. Report.PrintSettings.PagesOnSheet == PagesOnSheet.Eight;
  62. bool sourceIsLandscape = paperWidth > paperHeight;
  63. e.PageSettings.Landscape = rotate ? !sourceIsLandscape : sourceIsLandscape;
  64. SetPaperSource(Page, e);
  65. Duplex duplex = Page.Duplex;
  66. if (duplex != Duplex.Default)
  67. e.PageSettings.PrinterSettings.Duplex = duplex;
  68. }
  69. }
  70. public override void PrintPage(object sender, PrintPageEventArgs e)
  71. {
  72. StartPage(e);
  73. if (Page != null)
  74. {
  75. int countX = 0;
  76. int countY = 0;
  77. float scale = 0;
  78. float paperWidth = Page.UnlimitedWidth ? Page.UnlimitedWidthValue / Units.Millimeters : Page.PaperWidth;
  79. float paperHeight = Page.UnlimitedHeight ? Page.UnlimitedHeightValue / Units.Millimeters : Page.PaperHeight;
  80. // switch dimensions because FSheetWidth, FSheetHeight is a portrait dimensions
  81. float sheetWidth = Report.PrintSettings.PrintOnSheetWidth;
  82. float sheetHeight = Report.PrintSettings.PrintOnSheetHeight;
  83. bool sourceIsLandscape = paperWidth > paperHeight;
  84. if (sourceIsLandscape)
  85. {
  86. sheetWidth = Report.PrintSettings.PrintOnSheetHeight;
  87. sheetHeight = Report.PrintSettings.PrintOnSheetWidth;
  88. }
  89. switch (Report.PrintSettings.PagesOnSheet)
  90. {
  91. case PagesOnSheet.One:
  92. countX = 1;
  93. countY = 1;
  94. scale = Math.Min(sheetWidth / paperWidth, sheetHeight / paperHeight);
  95. break;
  96. case PagesOnSheet.Two:
  97. countX = 2;
  98. countY = 1;
  99. if (sourceIsLandscape)
  100. {
  101. countX = 1;
  102. countY = 2;
  103. }
  104. scale = Math.Min(sheetHeight / paperWidth, sheetWidth / paperHeight);
  105. scale = CorrectScaleForUnlimitedPage(scale, sourceIsLandscape, sheetWidth, sheetHeight, paperWidth, paperHeight);
  106. break;
  107. case PagesOnSheet.Four:
  108. countX = 2;
  109. countY = 2;
  110. scale = Math.Min(sheetWidth / paperWidth, sheetHeight / paperHeight) / 2;
  111. break;
  112. case PagesOnSheet.Eight:
  113. countX = 4;
  114. countY = 2;
  115. if (sourceIsLandscape)
  116. {
  117. countX = 2;
  118. countY = 4;
  119. }
  120. scale = Math.Min(sheetHeight / paperWidth, sheetWidth / paperHeight) / 2;
  121. scale = CorrectScaleForUnlimitedPage(scale, sourceIsLandscape, sheetWidth, sheetHeight, paperWidth, paperHeight);
  122. break;
  123. }
  124. float pieceX = paperWidth * scale;
  125. float pieceY = paperHeight * scale;
  126. float leftMargin = e.PageSettings.HardMarginX / 100f * 25.4f;
  127. float topMargin = e.PageSettings.HardMarginY / 100f * 25.4f;
  128. float offsY = -topMargin;
  129. for (int y = 0; y < countY; y++)
  130. {
  131. float offsX = -leftMargin;
  132. for (int x = 0; x < countX; x++)
  133. {
  134. Graphics g = e.Graphics;
  135. FRPaintEventArgs paintArgs;
  136. GraphicsState state = g.Save();
  137. IGraphics gr = GdiGraphics.FromGraphics(g);
  138. try
  139. {
  140. if (Config.IsRunningOnMono)
  141. {
  142. // Point is the only right thing to use in mono. Pixel unit produces weird layout
  143. g.PageUnit = GraphicsUnit.Point;
  144. g.TranslateTransform(offsX * Units.Millimeters * 72f / 96f, offsY * Units.Millimeters * 72f / 96f);
  145. // workaround different pango/cairo rendering behavior
  146. if (DrawUtils.GetMonoRendering(gr) == MonoRendering.Pango)
  147. {
  148. g.ScaleTransform(scale * 72f / 96f, scale * 72f / 96f);
  149. paintArgs = new FRPaintEventArgs(gr, 1, 1, Report.GraphicCache);
  150. }
  151. else
  152. {
  153. g.ScaleTransform(scale, scale);
  154. paintArgs = new FRPaintEventArgs(gr, 72f / 96f, 72f / 96f, Report.GraphicCache);
  155. }
  156. }
  157. else
  158. {
  159. g.PageUnit = GraphicsUnit.Pixel;
  160. g.TranslateTransform(offsX * Units.Millimeters * g.DpiX / 96, offsY * Units.Millimeters * g.DpiY / 96);
  161. g.ScaleTransform(scale, scale);
  162. paintArgs = new FRPaintEventArgs(gr, g.DpiX / 96, g.DpiY / 96, Report.GraphicCache);
  163. }
  164. Page.Print(paintArgs);
  165. #if Demo
  166. using (Watermark trialWatermark = new Watermark())
  167. {
  168. trialWatermark.Text = typeof(Duplex).Name[0].ToString() + typeof(Exception).Name[0].ToString() +
  169. typeof(Margins).Name[0].ToString() + typeof(Object).Name[0].ToString() + " " +
  170. typeof(ValueType).Name[0].ToString() + typeof(Exception).Name[0].ToString() +
  171. typeof(Rectangle).Name[0].ToString() + typeof(ShapeKind).Name[0].ToString() +
  172. typeof(ICloneable).Name[0].ToString() + typeof(Object).Name[0].ToString() +
  173. typeof(NonSerializedAttribute).Name[0].ToString();
  174. trialWatermark.DrawText(paintArgs, new RectangleF(0, 0,
  175. Page.PaperWidth * Units.Millimeters, Page.PaperHeight * Units.Millimeters), Report, true);
  176. }
  177. #endif
  178. #if Academic
  179. using (Watermark trialWatermark = new Watermark())
  180. {
  181. trialWatermark.Text = typeof(Array).Name[0].ToString() + typeof(Char).Name[0].ToString() +
  182. typeof(Array).Name[0].ToString() + typeof(DateTime).Name[0].ToString() +
  183. typeof(Enum).Name[0].ToString() + typeof(Margins).Name[0].ToString() +
  184. typeof(IEnumerable).Name[0].ToString() + typeof(Char).Name[0].ToString() +
  185. " " +
  186. typeof(LineObject).Name[0].ToString() + typeof(IEnumerable).Name[0].ToString() +
  187. typeof(Char).Name[0].ToString() + typeof(Enum).Name[0].ToString() +
  188. typeof(Nullable).Name[0].ToString() + typeof(ShapeObject).Name[0].ToString() +
  189. typeof(Enum).Name[0].ToString();
  190. trialWatermark.DrawText(paintArgs, new RectangleF(0, 0,
  191. Page.PaperWidth * Units.Millimeters, Page.PaperHeight * Units.Millimeters), Report, true);
  192. }
  193. #endif
  194. }
  195. finally
  196. {
  197. g.Restore(state);
  198. }
  199. offsX += pieceX;
  200. Page.Dispose();
  201. Page = GetNextPage();
  202. if (Page == null)
  203. break;
  204. }
  205. if (Page == null)
  206. break;
  207. offsY += pieceY;
  208. }
  209. }
  210. FinishPage(e);
  211. e.HasMorePages = Page != null;
  212. }
  213. public ScalePrintController(Report report, PrintDocument doc, int curPage) : base(report, doc, curPage)
  214. {
  215. }
  216. }
  217. }