PrintSettings.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing.Printing;
  4. using FastReport.Utils;
  5. using System.Drawing;
  6. namespace FastReport
  7. {
  8. /// <summary>
  9. /// Specifies the report printing mode.
  10. /// </summary>
  11. public enum PrintMode
  12. {
  13. /// <summary>
  14. /// Specifies the default printing mode. One report page produces
  15. /// one printed paper sheet of the same size.
  16. /// </summary>
  17. Default,
  18. /// <summary>
  19. /// Specifies the split mode. Big report page produces several smaller paper sheets.
  20. /// Use this mode to print A3 report on A4 printer.
  21. /// </summary>
  22. Split,
  23. /// <summary>
  24. /// Specifies the scale mode. One or several report pages produce one bigger paper sheet.
  25. /// Use this mode to print A5 report on A4 printer.
  26. /// </summary>
  27. Scale
  28. }
  29. /// <summary>
  30. /// Specifies the number of report pages printed on one paper sheet.
  31. /// </summary>
  32. public enum PagesOnSheet
  33. {
  34. /// <summary>
  35. /// Specifies one report page per sheet.
  36. /// </summary>
  37. One,
  38. /// <summary>
  39. /// Specifies two report pages per sheet.
  40. /// </summary>
  41. Two,
  42. /// <summary>
  43. /// Specifies four report pages per sheet.
  44. /// </summary>
  45. Four,
  46. /// <summary>
  47. /// Specifies eight report pages per sheet.
  48. /// </summary>
  49. Eight
  50. }
  51. /// <summary>
  52. /// Specifies the pages to print.
  53. /// </summary>
  54. public enum PrintPages
  55. {
  56. /// <summary>
  57. /// Print all report pages.
  58. /// </summary>
  59. All,
  60. /// <summary>
  61. /// Print odd pages only.
  62. /// </summary>
  63. Odd,
  64. /// <summary>
  65. /// Print even pages only.
  66. /// </summary>
  67. Even
  68. }
  69. /// <summary>
  70. /// This class contains the printer settings.
  71. /// It is used in the <see cref="Report.PrintSettings"/> property.
  72. /// </summary>
  73. /// <remarks>
  74. /// Typical use of this class is to setup a printer properties without using the "Print"
  75. /// dialog. In this case, setup necessary properties and turn off the dialog via the
  76. /// <see cref="ShowDialog"/> property.
  77. /// </remarks>
  78. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  79. public class PrintSettings : IDisposable
  80. {
  81. #region Fields
  82. private string printer;
  83. private bool savePrinterWithReport;
  84. private bool printToFile;
  85. private string printToFileName;
  86. private PageRange pageRange;
  87. private string pageNumbers;
  88. private int copies;
  89. private bool collate;
  90. private PrintPages printPages;
  91. private bool reverse;
  92. private Duplex duplex;
  93. private int paperSource;
  94. private PrintMode printMode;
  95. private float printOnSheetWidth;
  96. private float printOnSheetHeight;
  97. private int printOnSheetRawPaperSize;
  98. private PagesOnSheet pagesOnSheet;
  99. private string[] copyNames;
  100. private bool showDialog;
  101. private IGraphics measureGraphics;
  102. #endregion
  103. #region Properties
  104. /// <summary>
  105. /// Gets or sets the printer name.
  106. /// </summary>
  107. [TypeConverterAttribute(typeof(PrinterConverter))]
  108. public string Printer
  109. {
  110. get { return printer; }
  111. set
  112. {
  113. printer = value;
  114. DisposeMeasureGraphics();
  115. }
  116. }
  117. /// <summary>
  118. /// Gets or sets a value indicating that the printer name should be saved in a report file.
  119. /// </summary>
  120. /// <remarks>
  121. /// If this property is set to <b>true</b>, the printer name will be saved in a report file.
  122. /// Next time when you open the report, the printer will be automatically selected.
  123. /// </remarks>
  124. [DefaultValue(false)]
  125. public bool SavePrinterWithReport
  126. {
  127. get { return savePrinterWithReport; }
  128. set { savePrinterWithReport = value; }
  129. }
  130. /// <summary>
  131. /// Gets or sets a value indicating that the printing output should be send
  132. /// to a file instead of a printer.
  133. /// </summary>
  134. /// <remarks>
  135. /// Also set the <see cref="PrintToFileName"/> property.
  136. /// </remarks>
  137. [DefaultValue(false)]
  138. public bool PrintToFile
  139. {
  140. get { return printToFile; }
  141. set { printToFile = value; }
  142. }
  143. /// <summary>
  144. /// The name of a file to print the report to.
  145. /// </summary>
  146. /// <remarks>
  147. /// This property is used if <see cref="PrintToFile"/> property is <b>true</b>.
  148. /// </remarks>
  149. public string PrintToFileName
  150. {
  151. get { return printToFileName; }
  152. set { printToFileName = value; }
  153. }
  154. /// <summary>
  155. /// Gets or sets a value specifies the page range to print.
  156. /// </summary>
  157. [DefaultValue(PageRange.All)]
  158. public PageRange PageRange
  159. {
  160. get { return pageRange; }
  161. set { pageRange = value; }
  162. }
  163. /// <summary>
  164. /// Gets or sets the page number(s) to print.
  165. /// </summary>
  166. /// <remarks>
  167. /// This property is used if <see cref="PageRange"/> property is set to <b>PageNumbers</b>.
  168. /// You can specify the page numbers, separated by commas, or the page ranges.
  169. /// For example: "1,3,5-12".
  170. /// </remarks>
  171. public string PageNumbers
  172. {
  173. get { return pageNumbers; }
  174. set { pageNumbers = value; }
  175. }
  176. /// <summary>
  177. /// Gets or sets the number of copies to print.
  178. /// </summary>
  179. [DefaultValue(1)]
  180. public int Copies
  181. {
  182. get { return copies; }
  183. set { copies = value; }
  184. }
  185. /// <summary>
  186. /// Gets or sets a value indicating whether the printed document should be collated.
  187. /// </summary>
  188. [DefaultValue(true)]
  189. public bool Collate
  190. {
  191. get { return collate; }
  192. set { collate = value; }
  193. }
  194. /// <summary>
  195. /// Gets or sets a value specifies the pages to print.
  196. /// </summary>
  197. [DefaultValue(PrintPages.All)]
  198. public PrintPages PrintPages
  199. {
  200. get { return printPages; }
  201. set { printPages = value; }
  202. }
  203. /// <summary>
  204. /// Gets or sets a value determines whether to print pages in reverse order.
  205. /// </summary>
  206. [DefaultValue(false)]
  207. public bool Reverse
  208. {
  209. get { return reverse; }
  210. set { reverse = value; }
  211. }
  212. /// <summary>
  213. /// Gets or sets the duplex mode.
  214. /// </summary>
  215. [DefaultValue(Duplex.Default)]
  216. public Duplex Duplex
  217. {
  218. get { return duplex; }
  219. set { duplex = value; }
  220. }
  221. /// <summary>
  222. /// Gets or sets the paper source.
  223. /// </summary>
  224. /// <remarks>
  225. /// This property corresponds to the RAW source number. Default value is 7 which
  226. /// corresponds to DMBIN_AUTO.
  227. /// </remarks>
  228. [DefaultValue(7)]
  229. public int PaperSource
  230. {
  231. get { return paperSource; }
  232. set { paperSource = value; }
  233. }
  234. /// <summary>
  235. /// Gets or sets the print mode.
  236. /// </summary>
  237. /// <remarks>
  238. /// See the <see cref="FastReport.PrintMode"/> enumeration for details. If you use
  239. /// the mode other than <b>Default</b>, you must specify the sheet size in the
  240. /// <see cref="PrintOnSheetWidth"/>, <see cref="PrintOnSheetHeight"/> properties.
  241. /// </remarks>
  242. [DefaultValue(PrintMode.Default)]
  243. public PrintMode PrintMode
  244. {
  245. get { return printMode; }
  246. set { printMode = value; }
  247. }
  248. /// <summary>
  249. /// Gets or sets the width of the paper sheet to print on.
  250. /// </summary>
  251. /// <remarks>
  252. /// This property is used if the <see cref="PrintMode"/> property is not <b>Default</b>.
  253. /// Specify the paper width in millimeters.
  254. /// </remarks>
  255. public float PrintOnSheetWidth
  256. {
  257. get { return printOnSheetWidth; }
  258. set { printOnSheetWidth = value; }
  259. }
  260. /// <summary>
  261. /// Gets or sets the height of the paper sheet to print on.
  262. /// </summary>
  263. /// <remarks>
  264. /// This property is used if the <see cref="PrintMode"/> property is not <b>Default</b>.
  265. /// Specify the paper height in millimeters.
  266. /// </remarks>
  267. public float PrintOnSheetHeight
  268. {
  269. get { return printOnSheetHeight; }
  270. set { printOnSheetHeight = value; }
  271. }
  272. /// <summary>
  273. /// Gets or sets the raw index of a paper size.
  274. /// </summary>
  275. [DefaultValue(0)]
  276. public int PrintOnSheetRawPaperSize
  277. {
  278. get { return printOnSheetRawPaperSize; }
  279. set { printOnSheetRawPaperSize = value; }
  280. }
  281. /// <summary>
  282. /// Gets or sets the number of pages per printed sheet.
  283. /// </summary>
  284. /// <remarks>
  285. /// This property is used if the <see cref="PrintMode"/> property is set to <b>Scale</b>.
  286. /// </remarks>
  287. [DefaultValue(PagesOnSheet.One)]
  288. public PagesOnSheet PagesOnSheet
  289. {
  290. get { return pagesOnSheet; }
  291. set { pagesOnSheet = value; }
  292. }
  293. /// <summary>
  294. /// Gets or sets an array of printed copy names, such as "Original", "Copy", etc.
  295. /// </summary>
  296. public string[] CopyNames
  297. {
  298. get { return copyNames; }
  299. set { copyNames = value; }
  300. }
  301. /// <summary>
  302. /// Specifies whether to display the "Print" dialog.
  303. /// </summary>
  304. [DefaultValue(true)]
  305. public bool ShowDialog
  306. {
  307. get { return showDialog; }
  308. set { showDialog = value; }
  309. }
  310. internal IGraphics MeasureGraphics
  311. {
  312. get
  313. {
  314. if (measureGraphics == null)
  315. {
  316. PrinterSettings printer = new PrinterSettings();
  317. try
  318. {
  319. if (!String.IsNullOrEmpty(Printer))
  320. printer.PrinterName = Printer;
  321. }
  322. catch
  323. {
  324. }
  325. try
  326. {
  327. measureGraphics = GdiGraphics.FromGraphics(printer.CreateMeasurementGraphics());
  328. }
  329. catch
  330. {
  331. measureGraphics = null;
  332. }
  333. }
  334. return measureGraphics;
  335. }
  336. }
  337. #endregion
  338. #region Private Methods
  339. private void DisposeMeasureGraphics()
  340. {
  341. if (measureGraphics != null)
  342. measureGraphics.Dispose();
  343. measureGraphics = null;
  344. }
  345. #endregion
  346. #region Public Methods
  347. /// <inheritdoc/>
  348. public void Dispose()
  349. {
  350. DisposeMeasureGraphics();
  351. }
  352. /// <summary>
  353. /// Assigns values from another source.
  354. /// </summary>
  355. /// <param name="source">Source to assign from.</param>
  356. public void Assign(PrintSettings source)
  357. {
  358. Printer = source.Printer;
  359. SavePrinterWithReport = source.SavePrinterWithReport;
  360. PrintToFile = source.PrintToFile;
  361. PrintToFileName = source.PrintToFileName;
  362. PageRange = source.PageRange;
  363. PageNumbers = source.PageNumbers;
  364. Copies = source.Copies;
  365. Collate = source.Collate;
  366. PrintPages = source.PrintPages;
  367. Reverse = source.Reverse;
  368. Duplex = source.Duplex;
  369. PaperSource = source.PaperSource;
  370. PrintMode = source.PrintMode;
  371. PrintOnSheetWidth = source.PrintOnSheetWidth;
  372. PrintOnSheetHeight = source.PrintOnSheetHeight;
  373. PrintOnSheetRawPaperSize = source.PrintOnSheetRawPaperSize;
  374. PagesOnSheet = source.PagesOnSheet;
  375. source.CopyNames.CopyTo(CopyNames, 0);
  376. ShowDialog = source.ShowDialog;
  377. }
  378. /// <summary>
  379. /// Resets all settings to its default values.
  380. /// </summary>
  381. public void Clear()
  382. {
  383. printer = "";
  384. savePrinterWithReport = false;
  385. printToFile = false;
  386. printToFileName = "";
  387. pageRange = PageRange.All;
  388. pageNumbers = "";
  389. copies = 1;
  390. collate = true;
  391. printPages = PrintPages.All;
  392. reverse = false;
  393. duplex = Duplex.Default;
  394. paperSource = 7;
  395. printMode = PrintMode.Default;
  396. printOnSheetWidth = 210;
  397. printOnSheetHeight = 297;
  398. printOnSheetRawPaperSize = 0;
  399. pagesOnSheet = PagesOnSheet.One;
  400. copyNames = new string[0];
  401. showDialog = true;
  402. DisposeMeasureGraphics();
  403. }
  404. internal void Serialize(FRWriter writer, PrintSettings c)
  405. {
  406. if (SavePrinterWithReport && Printer != c.Printer)
  407. writer.WriteStr("PrintSettings.Printer", Printer);
  408. if (SavePrinterWithReport != c.SavePrinterWithReport)
  409. writer.WriteBool("PrintSettings.SavePrinterWithReport", SavePrinterWithReport);
  410. if (PrintToFile != c.PrintToFile)
  411. writer.WriteBool("PrintSettings.PrintToFile", PrintToFile);
  412. if (PrintToFileName != c.PrintToFileName)
  413. writer.WriteStr("PrintSettings.PrintToFileName", PrintToFileName);
  414. if (PageRange != c.PageRange)
  415. writer.WriteValue("PrintSettings.PageRange", PageRange);
  416. if (PageNumbers != c.PageNumbers)
  417. writer.WriteStr("PrintSettings.PageNumbers", PageNumbers);
  418. if (Copies != c.Copies)
  419. writer.WriteInt("PrintSettings.Copies", Copies);
  420. if (Collate != c.Collate)
  421. writer.WriteBool("PrintSettings.Collate", Collate);
  422. if (PrintPages != c.PrintPages)
  423. writer.WriteValue("PrintSettings.PrintPages", PrintPages);
  424. if (Reverse != c.Reverse)
  425. writer.WriteBool("PrintSettings.Reverse", Reverse);
  426. if (Duplex != c.Duplex)
  427. writer.WriteValue("PrintSettings.Duplex", Duplex);
  428. if (PaperSource != c.PaperSource)
  429. writer.WriteInt("PrintSettings.PaperSource", PaperSource);
  430. if (PrintMode != c.PrintMode)
  431. writer.WriteValue("PrintSettings.PrintMode", PrintMode);
  432. if (PrintOnSheetWidth != c.PrintOnSheetWidth)
  433. writer.WriteFloat("PrintSettings.PrintOnSheetWidth", PrintOnSheetWidth);
  434. if (PrintOnSheetHeight != c.PrintOnSheetHeight)
  435. writer.WriteFloat("PrintSettings.PrintOnSheetHeight", PrintOnSheetHeight);
  436. if (PrintOnSheetRawPaperSize != c.PrintOnSheetRawPaperSize)
  437. writer.WriteInt("PrintSettings.PrintOnSheetRawPaperSize", PrintOnSheetRawPaperSize);
  438. if (PagesOnSheet != c.PagesOnSheet)
  439. writer.WriteValue("PrintSettings.PagesOnSheet", PagesOnSheet);
  440. if (!writer.AreEqual(CopyNames, c.CopyNames))
  441. writer.WriteValue("PrintSettings.CopyNames", CopyNames);
  442. if (ShowDialog != c.ShowDialog)
  443. writer.WriteBool("PrintSettings.ShowDialog", ShowDialog);
  444. }
  445. #endregion
  446. /// <summary>
  447. /// Initializes a new instance of the <see cref="PrintSettings"/> class with default settings.
  448. /// </summary>
  449. public PrintSettings()
  450. {
  451. Clear();
  452. }
  453. }
  454. }