HomePagePrinterDialogViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using Prism.Regions;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Windows;
  8. using System.Drawing.Printing;
  9. using ComPDFKit.PDFDocument;
  10. using PDF_Office.Model;
  11. using System.Drawing;
  12. using System.Reflection;
  13. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  14. using System.Threading.Tasks;
  15. using ComPDFKit.PDFPage;
  16. using PDF_Office.Helper;
  17. using System.Drawing.Drawing2D;
  18. using ComPDFKitViewer.PdfViewer;
  19. using System.Windows.Media.Imaging;
  20. using System.Drawing.Imaging;
  21. using PDF_Office.CustomControl;
  22. using Prism.Events;
  23. using ImTools;
  24. using PDF_Office.Views.Dialog.HomePageToolsDialogs.HomePagePrinter;
  25. using System.Printing;
  26. using System.Windows.Media.Animation;
  27. using System.Windows.Controls;
  28. using static Dropbox.Api.TeamLog.SharedLinkAccessLevel;
  29. using DryIoc;
  30. using System.Text.RegularExpressions;
  31. using System.Diagnostics;
  32. using PDF_Office.EventAggregators;
  33. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  34. {
  35. public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware
  36. {
  37. public IRegionManager printRegion;
  38. public IDialogService printDialogs;
  39. public IEventAggregator printEvent;
  40. private CPDFViewer PDFViewer;
  41. public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo();
  42. #region 常用字符串
  43. public string Title => "Primter";
  44. public string HomePagePrinterDocumentName = "HomePagePrinterDocumentContent";
  45. public string HomePagePrinterModSizeName = "HomePagePrinterModSizeContent";
  46. public string HomePagePrinterModPosterName = "HomePagePrinterModPosterContent";
  47. public string HomePagePrinterModMultipleName = "HomePagePrinterModMultipleContent";
  48. public string HomePagePrinterModBookletName = "HomePagePrinterModBookletContent";
  49. #endregion
  50. #region 字典
  51. public Dictionary<string, string> GetPrintModNameFromRadioButton = new Dictionary<string, string>();
  52. #endregion
  53. #region 打印系统配置
  54. private readonly PrintServer localDefaultPrintServer = new PrintServer();
  55. private PrintQueue printQueue;
  56. #endregion
  57. #region 打印参数设置
  58. /// <summary>
  59. /// (文档本身的)打印页集合
  60. /// </summary>
  61. private List<int> DocumentTargetPages = new List<int>();
  62. #endregion
  63. private int _printOrientationIndex = 0;
  64. public int PrintOrientationIndex
  65. {
  66. get => _printOrientationIndex;
  67. set => SetProperty(ref _printOrientationIndex, value);
  68. }
  69. #region 相关region
  70. private string _homePagePrinterDocumentRegionName;
  71. public string HomePagePrinterDocumentRegionName
  72. {
  73. get { return _homePagePrinterDocumentRegionName; }
  74. set { _homePagePrinterDocumentRegionName = value; }
  75. }
  76. private string _homePagePrinterModRegionName;
  77. public string HomePagePrinterModRegionName
  78. {
  79. get { return _homePagePrinterModRegionName; }
  80. set { _homePagePrinterModRegionName = value; }
  81. }
  82. #endregion
  83. #region 打印参数设置
  84. /// <summary>
  85. /// 打印机名称
  86. /// </summary>
  87. public string PrinterName;
  88. private int _maxCopiesNumber;
  89. /// <summary>
  90. /// 最大打印份数
  91. /// </summary>
  92. public int MaxCopiesNumber
  93. {
  94. get => _maxCopiesNumber;
  95. set => SetProperty(ref _maxCopiesNumber, value);
  96. }
  97. /// <summary>
  98. /// 打印机支持的颜色
  99. /// </summary>
  100. public List<OutputColor> OutputColorList = new List<OutputColor>();
  101. #endregion
  102. #region 组件
  103. private List<string> _printerNameList;
  104. /// <summary>
  105. /// 打印机名
  106. /// </summary>
  107. public List<string> PrinterNameList
  108. {
  109. get { return _printerNameList; }
  110. set { _printerNameList = value; }
  111. }
  112. private List<string> _printContentList;
  113. /// <summary>
  114. /// 打印内容
  115. /// </summary>
  116. public List<string> PrintContentList
  117. {
  118. get { return _printContentList; }
  119. set { _printContentList = value; }
  120. }
  121. private int _printContentIndex = 0;
  122. public int PrintContentIndex
  123. {
  124. get => _printContentIndex;
  125. set => SetProperty(ref _printContentIndex, value);
  126. }
  127. private List<string> _printerOrientationList;
  128. /// <summary>
  129. /// 打印列表
  130. /// </summary>
  131. public List<string> PrintOrientationList
  132. {
  133. get { return _printerOrientationList; }
  134. set
  135. {
  136. _printerOrientationList = value;
  137. }
  138. }
  139. private int _printerSelectedIndex;
  140. public int PrinterSelectedIndex
  141. {
  142. get => _printerSelectedIndex;
  143. set => SetProperty(ref _printerSelectedIndex, value);
  144. }
  145. #endregion
  146. #region 打印态
  147. private bool _isBothSides;
  148. public bool IsBothSides
  149. {
  150. get => _isBothSides;
  151. set => _isBothSides = value;
  152. }
  153. public enum EnumBothSidesStage
  154. {
  155. StatusFrontSide,
  156. StatusBackSide
  157. }
  158. /// <summary>
  159. /// 双面打印阶段:正面或背面
  160. /// </summary>
  161. public EnumBothSidesStage BothSidesStage;
  162. #endregion
  163. #region 窗口相关参数
  164. private EnumPrintMod currentHomePagePrinterMod;
  165. private string _currentHomePagePrinterModName;
  166. public string CurrentHomePagePrinterModName
  167. {
  168. get => _currentHomePagePrinterModName;
  169. set
  170. {
  171. _currentHomePagePrinterModName = value;
  172. if (value == "HomePagePrinterModSizeContent")
  173. {
  174. currentHomePagePrinterMod = EnumPrintMod.StatusSize;
  175. }
  176. else if (value == "HomePagePrinterModPosterContent")
  177. {
  178. currentHomePagePrinterMod = EnumPrintMod.StatusPoster;
  179. }
  180. else if (value == "HomePagePrinterModMultipleContent")
  181. {
  182. currentHomePagePrinterMod = EnumPrintMod.StatusMultiple;
  183. }
  184. else if (value == "HomePagePrinterModBookletContent")
  185. {
  186. currentHomePagePrinterMod = EnumPrintMod.StatusBooklet;
  187. }
  188. }
  189. }
  190. public bool IsFirstOpen = true;
  191. #endregion
  192. #region 委托
  193. public DelegateCommand<object> SelectPrinterCommand { get; set; }
  194. public DelegateCommand CancelCommand { get; set; }
  195. public DelegateCommand ConfirmPrintCommand { get; set; }
  196. public DelegateCommand SetPaperCommand { get; set; }
  197. public DelegateCommand<object> ChangePrintModCommand { get; set; }
  198. public DelegateCommand<object> SetGrayscaleCommand { get; set; }
  199. public DelegateCommand<object> SetBothSidesCommand { get; set; }
  200. public DelegateCommand<object> SetPageRangeSelectionIndexCommand { get; set; }
  201. public DelegateCommand<object> SetCustomPageRangeCommand { get; set; }
  202. #endregion
  203. HomePagePrinterDialogViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  204. {
  205. this.printRegion = regionManager;
  206. this.printDialogs = dialogService;
  207. this.printEvent = eventAggregator;
  208. HomePagePrinterDocumentRegionName = Guid.NewGuid().ToString();
  209. HomePagePrinterModRegionName = Guid.NewGuid().ToString();
  210. SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
  211. CancelCommand = new DelegateCommand(Cancel);
  212. ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
  213. SetPaperCommand = new DelegateCommand(SetPaper);
  214. ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
  215. SetGrayscaleCommand = new DelegateCommand<object>(SetGrayscale);
  216. SetBothSidesCommand = new DelegateCommand<object>(SetBothSides);
  217. SetPageRangeSelectionIndexCommand = new DelegateCommand<object>(SetPageRangeSelectionIndex);
  218. SetCustomPageRangeCommand = new DelegateCommand<object>(SetCustomPageRange);
  219. InitComponent();
  220. InitDictionary();
  221. InitParams();
  222. printEvent.GetEvent<SendPrintSettingsModInfoEvent>().Subscribe(RecvPrintSettingssModInfo);
  223. }
  224. private void InitComponent()
  225. {
  226. InitPrinterNameList();
  227. InitPrintContentList();
  228. InitPrintOrientationList();
  229. }
  230. private void InitDictionary()
  231. {
  232. InitGetPrintModNameFromRadioButton();
  233. }
  234. private void InitParams()
  235. {
  236. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  237. List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
  238. foreach (PageMediaSize size in printQueue.GetPrintCapabilities().PageMediaSizeCapability)
  239. {
  240. if (size == null || size.PageMediaSizeName == null)
  241. {
  242. continue;
  243. }
  244. if (size.PageMediaSizeName == PageMediaSizeName.ISOA4)
  245. {
  246. PrintSettingsInfo.PageMediaSize = size;
  247. }
  248. PageMediaSizeList.Add(size);
  249. }
  250. if (PrintSettingsInfo.PageMediaSize == null)
  251. {
  252. PrintSettingsInfo.PageMediaSize = PageMediaSizeList[0];
  253. }
  254. }
  255. private void InitPrinterNameList()
  256. {
  257. PrinterNameList = new List<string>();
  258. PrintDocument printDocument = new PrintDocument();
  259. string defaultPrinterName = printDocument.PrinterSettings.PrinterName;
  260. foreach (string printerListItem in PrinterSettings.InstalledPrinters)
  261. {
  262. PrinterNameList.Add(printerListItem);
  263. }
  264. if (PrinterNameList.Contains("Microsoft Print to PDF"))
  265. {
  266. PrinterSelectedIndex = PrinterNameList.IndexOf("Microsoft Print to PDF");
  267. }
  268. else
  269. {
  270. PrinterSelectedIndex = 0;
  271. }
  272. PrinterName = PrinterNameList[PrinterSelectedIndex];
  273. }
  274. private void InitPrintContentList()
  275. {
  276. PrintContentList = new List<string>();
  277. PrintContentList.Add("文档");
  278. PrintContentList.Add("文档和标记");
  279. PrintContentList.Add("文档和图章");
  280. PrintContentList.Add("文档和表单");
  281. }
  282. private void InitPrintOrientationList()
  283. {
  284. PrintOrientationList = new List<string>();
  285. PrintOrientationList.Add("纵向");
  286. PrintOrientationList.Add("横向");
  287. }
  288. public void InitGetPrintModNameFromRadioButton()
  289. {
  290. GetPrintModNameFromRadioButton.Clear();
  291. GetPrintModNameFromRadioButton.Add("ModSizeRdo", HomePagePrinterModSizeName);
  292. GetPrintModNameFromRadioButton.Add("ModPosterRdo", HomePagePrinterModPosterName);
  293. GetPrintModNameFromRadioButton.Add("ModMultipleRdo", HomePagePrinterModMultipleName);
  294. GetPrintModNameFromRadioButton.Add("ModBookletRdo", HomePagePrinterModBookletName);
  295. }
  296. public void AutoOritation()
  297. {
  298. //纸张长边为高
  299. if (PrintSettingsInfo.PageMediaSize.Height / PrintSettingsInfo.PageMediaSize.Width >= 1)
  300. {
  301. //PDF长边为高
  302. if (PDFViewer.Document.GetPageSize(0).Height / PDFViewer.Document.GetPageSize(0).Width >= 1)
  303. {
  304. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
  305. }
  306. //PDF长边为宽
  307. else
  308. {
  309. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  310. }
  311. }
  312. //纸张短边为高
  313. else
  314. {
  315. //PDF长边为高
  316. if (PDFViewer.Document.GetPageSize(0).Height / PDFViewer.Document.GetPageSize(0).Width >= 1)
  317. {
  318. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  319. }
  320. //PDF长边为宽
  321. else
  322. {
  323. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
  324. }
  325. }
  326. PrintOrientationIndex = (int)PrintSettingsInfo.EnumPrintOrientation;
  327. }
  328. public void InitParamsByPDFView()
  329. {
  330. AutoOritation();
  331. }
  332. /// <summary>
  333. /// 选择打印机
  334. /// </summary>
  335. /// <param name="e"></param>
  336. public void SelectPrinter(object e)
  337. {
  338. var comboBox = e as ComboBox;
  339. PrinterName = comboBox.SelectedItem.ToString();
  340. }
  341. /// <summary>
  342. /// 打开纸张设置窗口
  343. /// </summary>
  344. public void SetPaper()
  345. {
  346. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  347. LoadPrinterSettings();
  348. DialogParameters value = new DialogParameters();
  349. value.Add(ParameterNames.PrintQueue, printQueue);
  350. printDialogs.ShowDialog(DialogNames.HomePagePrinterPaperSettingsDialog, value, e =>
  351. {
  352. if (e.Result == ButtonResult.OK)
  353. {
  354. PrintSettingsInfo.PageMediaSize = e.Parameters.GetValue<PageMediaSize>("PageMediaSize");
  355. PrintSettingsInfo.Margin.Clear();
  356. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginLeft"));
  357. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginTop"));
  358. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginRight"));
  359. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginBottom"));
  360. AutoOritation();
  361. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(PrintSettingsInfo);
  362. }
  363. });
  364. }
  365. public void ChangePrintMod(object e)
  366. {
  367. var rdo = e as RadioButton;
  368. CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
  369. printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName);
  370. }
  371. /// <summary>
  372. /// 加载对应打印设置
  373. /// </summary>
  374. public void LoadPrinterSettings()
  375. {
  376. if (printQueue.GetPrintCapabilities().MaxCopyCount.HasValue)
  377. {
  378. MaxCopiesNumber = printQueue.GetPrintCapabilities().MaxCopyCount.Value;
  379. }
  380. else
  381. {
  382. MaxCopiesNumber = 1;
  383. }
  384. OutputColorList.Clear();
  385. foreach (OutputColor color in printQueue.GetPrintCapabilities().OutputColorCapability)
  386. {
  387. OutputColorList.Add(color);
  388. }
  389. }
  390. #region 来自UI的设置
  391. /// <summary>
  392. /// 设置灰度
  393. /// 改变打印机设置
  394. /// 改变渲染
  395. /// </summary>
  396. public void SetGrayscale(object e)
  397. {
  398. var chk = e as CheckBox;
  399. if ((bool)chk.IsChecked)
  400. {
  401. PrintSettingsInfo.IsGrayscale = true;
  402. }
  403. else if (!(bool)chk.IsChecked)
  404. {
  405. PrintSettingsInfo.IsGrayscale = false;
  406. }
  407. }
  408. /// <summary>
  409. /// 设置双面打印
  410. /// 改变打印流程
  411. /// 不改变打印相关参数
  412. /// </summary>
  413. /// <param name="e"></param>
  414. public void SetBothSides(object e)
  415. {
  416. var chk = e as CheckBox;
  417. if ((bool)chk.IsChecked)
  418. {
  419. IsBothSides = true;
  420. BothSidesStage = EnumBothSidesStage.StatusFrontSide;
  421. }
  422. else if (!(bool)chk.IsChecked)
  423. {
  424. IsBothSides = false;
  425. }
  426. }
  427. public void SetPageRangeSelectionIndex(object e)
  428. {
  429. var cmb = e as WritableComboBox;
  430. PrintSettingsInfo.EnumPageRange = (EnumPageRange)int.Parse(cmb.SelectedIndex);
  431. if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count)
  432. {
  433. }
  434. else
  435. {
  436. }
  437. }
  438. public void SetCustomPageRange(object e)
  439. {
  440. if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusCustomizedRange)
  441. {
  442. var cmb = e as WritableComboBox;
  443. if (cmb == null)
  444. {
  445. return;
  446. }
  447. if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  448. { //TODO
  449. Trace.WriteLine("输入不对");
  450. MessageBoxEx.Show("输入不对");
  451. return;
  452. }
  453. PrintSettingsInfo.CustomRange = DocumentTargetPages;
  454. }
  455. }
  456. public void RecvPrintSettingssModInfo(PrintModInfo printModInfo)
  457. {
  458. if(printModInfo.EnumPrintMod == currentHomePagePrinterMod)
  459. {
  460. PrintSettingsInfo.printModInfo = printModInfo;
  461. }
  462. }
  463. #endregion
  464. public void Cancel()
  465. {
  466. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  467. }
  468. public void NavigateDocumentRegion()
  469. {
  470. NavigationParameters param = new NavigationParameters();
  471. param.Add(ParameterNames.PDFViewer, PDFViewer);
  472. param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo);
  473. printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param);
  474. }
  475. public void NavigateModRegion()
  476. {
  477. NavigationParameters param = new NavigationParameters();
  478. param.Add(ParameterNames.PDFViewer, PDFViewer);
  479. printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName);
  480. }
  481. /// <summary>
  482. /// 确认打印,单面或手动双面
  483. /// </summary>
  484. public void ConfirmPrint()
  485. {
  486. if (!IsBothSides)
  487. {
  488. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  489. }
  490. else if (IsBothSides)
  491. {
  492. MessageBoxEx.Show("翻页");
  493. RequestClose.Invoke(new DialogResult(ButtonResult.OK));
  494. }
  495. }
  496. public event Action<IDialogResult> RequestClose;
  497. public bool CanCloseDialog()
  498. {
  499. return true;
  500. }
  501. public void OnDialogClosed()
  502. {
  503. }
  504. public void OnDialogOpened(IDialogParameters parameters)
  505. {
  506. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  507. if (PDFViewer != null)
  508. {
  509. NavigationParameters param = new NavigationParameters();
  510. NavigateDocumentRegion();
  511. NavigateModRegion();
  512. InitParamsByPDFView();
  513. }
  514. }
  515. }
  516. }