HomePagePrinterPaperSettingsDialogViewModel.cs 7.3 KB

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