PrintDocument.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // System.Drawing.PrintDocument.cs
  3. //
  4. // Authors:
  5. // Dennis Hayes ([email protected])
  6. // Herve Poussineau ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002 Ximian, Inc
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.ComponentModel;
  35. namespace System.Drawing.Printing
  36. {
  37. [DefaultEvent ("PrintPage"), DefaultProperty ("DocumentName")]
  38. [ToolboxItemFilter ("System.Drawing.Printing", ToolboxItemFilterType.Allow)]
  39. public class PrintDocument : System.ComponentModel.Component
  40. {
  41. private PageSettings defaultpagesettings;
  42. private PrinterSettings printersettings;
  43. private PrintController printcontroller;
  44. private string documentname;
  45. #if !(NET_1_0)
  46. private bool originAtMargins = false; // .NET V1.1 Beta
  47. #endif
  48. public PrintDocument() {
  49. documentname = "document"; //offical default.
  50. defaultpagesettings = new PageSettings(); // use default values of default printer
  51. printersettings = new PrinterSettings(); // use default values
  52. printcontroller = new StandardPrintController();
  53. }
  54. // properties
  55. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  56. [Browsable (false)]
  57. [SRDescription ("The settings for the current page.")]
  58. public PageSettings DefaultPageSettings{
  59. get{
  60. return defaultpagesettings;
  61. }
  62. set{
  63. defaultpagesettings = value;
  64. }
  65. }
  66. // Name of the document, not the file!
  67. [DefaultValue ("document")]
  68. [SRDescription ("The name of the document.")]
  69. public string DocumentName{
  70. get{
  71. return documentname;
  72. }
  73. set{
  74. documentname = value;
  75. }
  76. }
  77. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  78. [Browsable (false)]
  79. [SRDescription ("The print controller object.")]
  80. public PrintController PrintController{
  81. get{
  82. return printcontroller;
  83. }
  84. set{
  85. printcontroller = value;
  86. }
  87. }
  88. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  89. [Browsable (false)]
  90. [SRDescription ("The current settings for the active printer.")]
  91. public PrinterSettings PrinterSettings{
  92. get{
  93. return printersettings;
  94. }
  95. set{
  96. printersettings = value;
  97. }
  98. }
  99. #if !(NET_1_0)
  100. [DefaultValue (false)]
  101. [SRDescription ("Determines if the origin is set at the specified margins.")]
  102. public bool OriginAtMargins{
  103. get{
  104. return originAtMargins;
  105. }
  106. set{
  107. originAtMargins = value;
  108. }
  109. }
  110. #endif
  111. // methods
  112. public void Print(){
  113. PrintEventArgs printArgs = new PrintEventArgs();
  114. this.OnBeginPrint(printArgs);
  115. if (printArgs.Cancel)
  116. return;
  117. PrintController.OnStartPrint(this, printArgs);
  118. if (printArgs.Cancel)
  119. return;
  120. // while there is more pages
  121. PrintPageEventArgs printPageArgs;
  122. do
  123. {
  124. PageSettings pageSettings = DefaultPageSettings.Clone() as PageSettings;
  125. this.OnQueryPageSettings(new QueryPageSettingsEventArgs(pageSettings));
  126. printPageArgs = new PrintPageEventArgs(
  127. null,
  128. pageSettings.Bounds,
  129. new Rectangle(0, 0, pageSettings.PaperSize.Width, pageSettings.PaperSize.Height),
  130. pageSettings);
  131. Graphics g = PrintController.OnStartPage(this, printPageArgs);
  132. // assign Graphics in printPageArgs
  133. printPageArgs.SetGraphics(g);
  134. if (!printPageArgs.Cancel)
  135. this.OnPrintPage(printPageArgs);
  136. PrintController.OnEndPage(this, printPageArgs);
  137. if (printPageArgs.Cancel)
  138. break;
  139. } while (printPageArgs.HasMorePages);
  140. this.OnEndPrint(printArgs);
  141. PrintController.OnEndPrint(this, printArgs);
  142. }
  143. public override string ToString(){
  144. return "[PrintDocument " + this.DocumentName + "]";
  145. }
  146. // events
  147. protected virtual void OnBeginPrint(PrintEventArgs e){
  148. //fire the event
  149. if (BeginPrint != null)
  150. BeginPrint(this, e);
  151. }
  152. protected virtual void OnEndPrint(PrintEventArgs e){
  153. //fire the event
  154. if (EndPrint != null)
  155. EndPrint(this, e);
  156. }
  157. protected virtual void OnPrintPage(PrintPageEventArgs e){
  158. //fire the event
  159. if (PrintPage != null)
  160. PrintPage(this, e);
  161. }
  162. protected virtual void OnQueryPageSettings(QueryPageSettingsEventArgs e){
  163. //fire the event
  164. if (QueryPageSettings != null)
  165. QueryPageSettings(this, e);
  166. }
  167. [SRDescription ("Raised when printing begins")]
  168. public event PrintEventHandler BeginPrint;
  169. [SRDescription ("Raised when printing ends")]
  170. public event PrintEventHandler EndPrint;
  171. [SRDescription ("Raised when printing of a new page begins")]
  172. public event PrintPageEventHandler PrintPage;
  173. [SRDescription ("Raised before printing of a new page begins")]
  174. public event QueryPageSettingsEventHandler QueryPageSettings;
  175. }
  176. }