Printer.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using FastReport.Utils;
  2. using System.Drawing.Printing;
  3. namespace FastReport.Print
  4. {
  5. internal class Printer
  6. {
  7. private Report report;
  8. public void Print(PrinterSettings printerSettings, int curPage)
  9. {
  10. if (report.PrintSettings.CopyNames.Length > 0)
  11. {
  12. // copy names are set, handle copies in code
  13. int copies = report.PrintSettings.Copies;
  14. try
  15. {
  16. report.PrintSettings.Copies = 1;
  17. for (int copyIndex = 1; copyIndex <= copies; copyIndex++)
  18. {
  19. report.PreparedPages.MacroValues["Copy#"] = copyIndex;
  20. PrintInternal(printerSettings, curPage);
  21. }
  22. }
  23. finally
  24. {
  25. report.PrintSettings.Copies = copies;
  26. report.PreparedPages.MacroValues.Remove("Copy#");
  27. }
  28. }
  29. else
  30. {
  31. // just print
  32. PrintInternal(printerSettings, curPage);
  33. }
  34. }
  35. private void PrintInternal(PrinterSettings printerSettings, int curPage)
  36. {
  37. using (PrintDocument doc = new PrintDocument())
  38. {
  39. if (printerSettings != null)
  40. doc.PrinterSettings = printerSettings;
  41. PrintControllerBase controller = null;
  42. switch (report.PrintSettings.PrintMode)
  43. {
  44. case PrintMode.Default:
  45. controller = new DefaultPrintController(report, doc, curPage);
  46. break;
  47. case PrintMode.Split:
  48. controller = new SplitPrintController(report, doc, curPage);
  49. break;
  50. case PrintMode.Scale:
  51. controller = new ScalePrintController(report, doc, curPage);
  52. break;
  53. }
  54. doc.PrintController = new StandardPrintController();
  55. doc.PrintPage += new PrintPageEventHandler(controller.PrintPage);
  56. doc.QueryPageSettings += new QueryPageSettingsEventHandler(controller.QueryPageSettings);
  57. Duplex duplex = report.PrintSettings.Duplex;
  58. if (duplex != Duplex.Default)
  59. doc.PrinterSettings.Duplex = duplex;
  60. try
  61. {
  62. report.SetOperation(ReportOperation.Printing);
  63. Config.ReportSettings.OnStartProgress(report);
  64. doc.Print();
  65. Config.ReportSettings.OnReportPrinted(report);
  66. }
  67. finally
  68. {
  69. Config.ReportSettings.OnFinishProgress(report);
  70. report.SetOperation(ReportOperation.None);
  71. }
  72. }
  73. }
  74. public Printer(Report report)
  75. {
  76. this.report = report;
  77. }
  78. }
  79. }