HomePagePrinterPaperSettingsDialogViewModel.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using Microsoft.Xaml.Behaviors;
  2. using PDF_Office.Model;
  3. using PDF_Office.CustomControl;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Printing;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  16. {
  17. public class HomePagePrinterPaperSettingsDialogViewModel : BindableBase, IDialogAware
  18. {
  19. public string Title => "PaperSettings";
  20. public event Action<IDialogResult> RequestClose;
  21. private PrintQueue printQueue;
  22. private Visibility _isPageSizeStackPanelsVisible = Visibility.Hidden;
  23. public Visibility IsPageSizeStackPanelsVisible
  24. {
  25. get { return _isPageSizeStackPanelsVisible; }
  26. set { SetProperty(ref _isPageSizeStackPanelsVisible, value); }
  27. }
  28. /// <summary>
  29. /// 是否自定义纸张
  30. /// </summary>
  31. private bool _isCustomSize;
  32. public bool IsCustomSize
  33. {
  34. get => _isCustomSize;
  35. set
  36. {
  37. if (value)
  38. {
  39. IsPageSizeStackPanelsVisible = Visibility.Visible;
  40. }
  41. else
  42. {
  43. IsPageSizeStackPanelsVisible = Visibility.Hidden;
  44. }
  45. _isCustomSize = value;
  46. }
  47. }
  48. private int? _customPaperSizeWidth;
  49. /// <summary>
  50. /// 自定义页面尺寸:宽度
  51. /// </summary>
  52. public int? CustomPaperSizeWidth
  53. {
  54. get => _customPaperSizeWidth;
  55. set => _customPaperSizeWidth = value;
  56. }
  57. private int? _customPaperSizeHeight;
  58. /// <summary>
  59. /// 自定义页面尺寸:高度
  60. /// </summary>
  61. public int? CustomPaperSizeHeight
  62. {
  63. get => _customPaperSizeHeight;
  64. set => _customPaperSizeHeight = value;
  65. }
  66. /// <summary>
  67. /// 当前选中的纸张尺寸
  68. /// </summary>
  69. public PageMediaSize currentPageMediaSize;
  70. /// <summary>
  71. /// 打印机支持的纸张
  72. /// </summary>
  73. public List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
  74. /// <summary>
  75. /// 纸张大小列表
  76. /// </summary>
  77. private List<string> _pageMediaSizeNameList = new List<string>();
  78. public List<string> PageMediaSizeNameList
  79. {
  80. get => _pageMediaSizeNameList;
  81. set => _pageMediaSizeNameList = value;
  82. }
  83. private List<int> _margin = new List<int>();
  84. public List<int> Margin
  85. {
  86. get => _margin;
  87. set => _margin = value;
  88. }
  89. private int _pageMediaSizeIndex = 0;
  90. public int PageMediaSizeIndex
  91. {
  92. get=> _pageMediaSizeIndex;
  93. set => SetProperty(ref _pageMediaSizeIndex, value);
  94. }
  95. /// <summary>
  96. /// 页边距
  97. /// </summary>
  98. private int _marginTop;
  99. public int MarginTop
  100. {
  101. get => _marginTop;
  102. set => SetProperty(ref _marginTop, value);
  103. }
  104. private int _marginBottom;
  105. public int MarginBottom
  106. {
  107. get => _marginBottom;
  108. set => SetProperty(ref _marginBottom, value);
  109. }
  110. private int _marginLeft;
  111. public int MarginLeft
  112. {
  113. get => _marginLeft;
  114. set => SetProperty(ref _marginLeft, value);
  115. }
  116. private int _marginRight;
  117. public int MarginRight
  118. {
  119. get => _marginRight;
  120. set => SetProperty(ref _marginRight, value);
  121. }
  122. public DelegateCommand CancelCommand { get; set; }
  123. public DelegateCommand<object> SetPaperSizeCommand { get; set; }
  124. public DelegateCommand ConfirmPaperSettingsCommand { get; set; }
  125. HomePagePrinterPaperSettingsDialogViewModel()
  126. {
  127. CancelCommand = new DelegateCommand(Cancel);
  128. SetPaperSizeCommand = new DelegateCommand<object>(SetPaperSize);
  129. ConfirmPaperSettingsCommand = new DelegateCommand(ConfirmPaperSettings);
  130. }
  131. private void InitPaperSizeList()
  132. {
  133. PageMediaSizeList.Clear();
  134. PageMediaSizeNameList.Clear();
  135. foreach (PageMediaSize size in printQueue.GetPrintCapabilities().PageMediaSizeCapability)
  136. {
  137. if (size == null || size.PageMediaSizeName == null)
  138. {
  139. continue;
  140. }
  141. PageMediaSizeList.Add(size);
  142. PageMediaSizeNameList.Add(size.PageMediaSizeName.ToString());
  143. }
  144. PageMediaSizeNameList.Add("自定义");
  145. if (PageMediaSizeNameList.Contains("ISOA4"))
  146. {
  147. PageMediaSizeIndex = PageMediaSizeNameList.IndexOf("ISOA4");
  148. }
  149. else
  150. {
  151. PageMediaSizeIndex = 0;
  152. }
  153. currentPageMediaSize = PageMediaSizeList[PageMediaSizeIndex];
  154. }
  155. public void SetPaperSize(object e)
  156. {
  157. var cmb = e as ComboBox;
  158. if (cmb.SelectedIndex < PageMediaSizeNameList.Count - 1)
  159. {
  160. IsCustomSize = false;
  161. currentPageMediaSize = PageMediaSizeList[PageMediaSizeNameList.IndexOf(cmb.SelectedItem.ToString())];
  162. }
  163. else
  164. {
  165. IsCustomSize = true;
  166. }
  167. }
  168. public void ConfirmPaperSettings ()
  169. {
  170. if (IsCustomSize)
  171. {
  172. if (CustomPaperSizeHeight!=null&&CustomPaperSizeWidth!=null)
  173. {
  174. currentPageMediaSize = new PageMediaSize((double)CustomPaperSizeWidth, (double)CustomPaperSizeHeight);
  175. }
  176. else
  177. {
  178. MessageBoxEx.Show("纸张尺寸缺失");
  179. return;
  180. }
  181. }
  182. Margin.Add(MarginLeft);
  183. Margin.Add(MarginTop);
  184. Margin.Add(MarginRight);
  185. Margin.Add(MarginBottom);
  186. var dialogResult = new DialogResult(ButtonResult.OK);
  187. dialogResult.Parameters.Add("PageMediaSize", currentPageMediaSize);
  188. dialogResult.Parameters.Add("MarginLeft", MarginLeft);
  189. dialogResult.Parameters.Add("MarginTop", MarginTop);
  190. dialogResult.Parameters.Add("MarginRight", MarginRight);
  191. dialogResult.Parameters.Add("MarginBottom", MarginBottom);
  192. RequestClose.Invoke(dialogResult);
  193. }
  194. public void Cancel()
  195. {
  196. var dialogResult = new DialogResult(ButtonResult.Cancel);
  197. RequestClose.Invoke(dialogResult);
  198. }
  199. public bool CanCloseDialog()
  200. {
  201. return true;
  202. }
  203. public void OnDialogClosed()
  204. {
  205. }
  206. public void OnDialogOpened(IDialogParameters parameters)
  207. {
  208. parameters.TryGetValue<PrintQueue>(ParameterNames.PrintQueue, out printQueue);
  209. InitPaperSizeList();
  210. }
  211. }
  212. }