HomePagePrinterDialogViewModel.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. using System.Collections.ObjectModel;
  34. using System.Windows.Forms;
  35. using ComboBox = System.Windows.Controls.ComboBox;
  36. using CheckBox = System.Windows.Controls.CheckBox;
  37. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  38. {
  39. public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware
  40. {
  41. public IRegionManager printRegion;
  42. public IDialogService printDialogs;
  43. public IEventAggregator printEvent;
  44. private CPDFViewer PDFViewer;
  45. public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo();
  46. #region 常用字符串
  47. public string Title => "Primter";
  48. public string HomePagePrinterDocumentName = "HomePagePrinterDocumentContent";
  49. public string HomePagePrinterModSizeName = "HomePagePrinterModSizeContent";
  50. public string HomePagePrinterModPosterName = "HomePagePrinterModPosterContent";
  51. public string HomePagePrinterModMultipleName = "HomePagePrinterModMultipleContent";
  52. public string HomePagePrinterModBookletName = "HomePagePrinterModBookletContent";
  53. #endregion
  54. #region 字典
  55. public Dictionary<string, string> GetPrintModNameFromRadioButton = new Dictionary<string, string>();
  56. #endregion
  57. #region 打印系统配置
  58. private readonly PrintServer localDefaultPrintServer = new PrintServer();
  59. private PrintQueue printQueue;
  60. #endregion
  61. #region 打印参数设置
  62. /// <summary>
  63. /// (文档本身的)打印页集合
  64. /// </summary>
  65. private List<int> DocumentTargetPages = new List<int>();
  66. #endregion
  67. private int _printOrientationIndex = 0;
  68. public int PrintOrientationIndex
  69. {
  70. get => _printOrientationIndex;
  71. set => SetProperty(ref _printOrientationIndex, value);
  72. }
  73. #region 相关region
  74. private string _homePagePrinterDocumentRegionName;
  75. public string HomePagePrinterDocumentRegionName
  76. {
  77. get { return _homePagePrinterDocumentRegionName; }
  78. set { _homePagePrinterDocumentRegionName = value; }
  79. }
  80. private string _homePagePrinterModRegionName;
  81. public string HomePagePrinterModRegionName
  82. {
  83. get { return _homePagePrinterModRegionName; }
  84. set { _homePagePrinterModRegionName = value; }
  85. }
  86. #endregion
  87. #region 打印参数设置
  88. /// <summary>
  89. /// 打印机名称
  90. /// </summary>
  91. public string PrinterName;
  92. private int _maxCopiesNumber;
  93. /// <summary>
  94. /// 最大打印份数
  95. /// </summary>
  96. public int MaxCopiesNumber
  97. {
  98. get => _maxCopiesNumber;
  99. set => SetProperty(ref _maxCopiesNumber, value);
  100. }
  101. private Visibility _printPageBorderVisibility = Visibility.Collapsed;
  102. public Visibility PrintPageBorderVisibility
  103. {
  104. get => _printPageBorderVisibility;
  105. set => SetProperty(ref _printPageBorderVisibility, value);
  106. }
  107. /// <summary>
  108. /// 打印机支持的颜色
  109. /// </summary>
  110. public List<OutputColor> OutputColorList = new List<OutputColor>();
  111. #endregion
  112. #region 组件
  113. private List<string> _printerNameList;
  114. /// <summary>
  115. /// 打印机名
  116. /// </summary>
  117. public List<string> PrinterNameList
  118. {
  119. get { return _printerNameList; }
  120. set { _printerNameList = value; }
  121. }
  122. private List<string> _printContentList;
  123. /// <summary>
  124. /// 打印内容
  125. /// </summary>
  126. public List<string> PrintContentList
  127. {
  128. get { return _printContentList; }
  129. set { _printContentList = value; }
  130. }
  131. private int _printContentIndex = 0;
  132. public int PrintContentIndex
  133. {
  134. get => _printContentIndex;
  135. set => SetProperty(ref _printContentIndex, value);
  136. }
  137. private List<string> _printerOrientationList;
  138. /// <summary>
  139. /// 打印列表
  140. /// </summary>
  141. public List<string> PrintOrientationList
  142. {
  143. get { return _printerOrientationList; }
  144. set
  145. {
  146. _printerOrientationList = value;
  147. }
  148. }
  149. private int _printerSelectedIndex;
  150. public int PrinterSelectedIndex
  151. {
  152. get => _printerSelectedIndex;
  153. set => SetProperty(ref _printerSelectedIndex, value);
  154. }
  155. #endregion
  156. #region 打印态
  157. private bool _isBothSides;
  158. public bool IsBothSides
  159. {
  160. get => _isBothSides;
  161. set => _isBothSides = value;
  162. }
  163. public int printCurrentPageIndex;
  164. public bool isCurrentPage;
  165. /// <summary>
  166. /// 双面打印阶段:正面或背面
  167. /// </summary>
  168. public EnumBothSidesStage BothSidesStage = EnumBothSidesStage.StatusNone;
  169. #endregion
  170. #region 窗口相关参数
  171. private EnumPrintMod currentHomePagePrinterMod;
  172. private string _currentHomePagePrinterModName;
  173. public string CurrentHomePagePrinterModName
  174. {
  175. get => _currentHomePagePrinterModName;
  176. set
  177. {
  178. _currentHomePagePrinterModName = value;
  179. if (value == "HomePagePrinterModSizeContent")
  180. {
  181. currentHomePagePrinterMod = EnumPrintMod.StatusSize;
  182. }
  183. else if (value == "HomePagePrinterModPosterContent")
  184. {
  185. currentHomePagePrinterMod = EnumPrintMod.StatusPoster;
  186. }
  187. else if (value == "HomePagePrinterModMultipleContent")
  188. {
  189. currentHomePagePrinterMod = EnumPrintMod.StatusMultiple;
  190. }
  191. else if (value == "HomePagePrinterModBookletContent")
  192. {
  193. currentHomePagePrinterMod = EnumPrintMod.StatusBooklet;
  194. }
  195. }
  196. }
  197. public bool IsFirstOpen = true;
  198. string Unicode = null;
  199. #endregion
  200. #region 委托
  201. public DelegateCommand CancelCommand { get; set; }
  202. public DelegateCommand ConfirmPrintCommand { get; set; }
  203. public DelegateCommand SetPaperCommand { get; set; }
  204. public DelegateCommand<object> SelectPrinterCommand { get; set; }
  205. public DelegateCommand<object> ChangePrintModCommand { get; set; }
  206. public DelegateCommand<object> SetGrayscaleCommand { get; set; }
  207. public DelegateCommand<object> SetDuplexCommand { get; set; }
  208. public DelegateCommand<object> SetPageRangeSelectionIndexCommand { get; set; }
  209. public DelegateCommand<object> SetCustomPageRangeCommand { get; set; }
  210. public DelegateCommand<object> SetPrintContentCommand { get; set; }
  211. public DelegateCommand<object> SetReversePageCommand { get; set; }
  212. public DelegateCommand<object> SetPrintBorderCommand { get; set; }
  213. public DelegateCommand<object> SetPrintOritationCommand { get; set; }
  214. #endregion
  215. HomePagePrinterDialogViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  216. {
  217. this.printRegion = regionManager;
  218. this.printDialogs = dialogService;
  219. this.printEvent = eventAggregator;
  220. HomePagePrinterDocumentRegionName = Guid.NewGuid().ToString();
  221. HomePagePrinterModRegionName = Guid.NewGuid().ToString();
  222. CancelCommand = new DelegateCommand(Cancel);
  223. ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
  224. SetPaperCommand = new DelegateCommand(SetPaper);
  225. SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
  226. ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
  227. SetGrayscaleCommand = new DelegateCommand<object>(SetGrayscale);
  228. SetDuplexCommand = new DelegateCommand<object>(SetDuplex);
  229. SetPageRangeSelectionIndexCommand = new DelegateCommand<object>(SetPageRangeSelectionIndex);
  230. SetCustomPageRangeCommand = new DelegateCommand<object>(SetCustomPageRange);
  231. SetPrintContentCommand = new DelegateCommand<object>(SetPrintContent);
  232. SetReversePageCommand = new DelegateCommand<object>(SetReversePage);
  233. SetPrintBorderCommand = new DelegateCommand<object>(SetPrintBorder);
  234. SetPrintOritationCommand = new DelegateCommand<object>(SetPrintOritation);
  235. Unicode = Guid.NewGuid().ToString();
  236. InitComponent();
  237. InitDictionary();
  238. InitParams();
  239. PageSetupDialog pageSetupDialog = new PageSetupDialog();
  240. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  241. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
  242. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false;
  243. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  244. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  245. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  246. {
  247. PrintOrientationIndex = 0;
  248. }
  249. else
  250. {
  251. PrintOrientationIndex = 1;
  252. }
  253. printEvent.GetEvent<SendPrintSettingsModInfoEvent>().Subscribe(RecvPrintSettingsModInfo, e => e.Unicode == Unicode);
  254. printEvent.GetEvent<SendDuplexPrintModEvent>().Subscribe(RecvDuplexPrintMod, e => e.Unicode == Unicode);
  255. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  256. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  257. {
  258. PrintOrientationIndex = 0;
  259. }
  260. else
  261. {
  262. PrintOrientationIndex = 1;
  263. }
  264. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  265. }
  266. private void InitComponent()
  267. {
  268. InitPrinterNameList();
  269. InitPrintContentList();
  270. InitPrintOrientationList();
  271. }
  272. private void InitDictionary()
  273. {
  274. InitGetPrintModNameFromRadioButton();
  275. }
  276. private void InitParams()
  277. {
  278. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  279. List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
  280. }
  281. private void InitPrinterNameList()
  282. {
  283. PrinterNameList = new List<string>();
  284. foreach (string printerListItem in PrinterSettings.InstalledPrinters)
  285. {
  286. PrinterNameList.Add(printerListItem);
  287. }
  288. if (PrinterNameList.Contains("Microsoft Print to PDF"))
  289. {
  290. PrinterSelectedIndex = PrinterNameList.IndexOf("Microsoft Print to PDF");
  291. }
  292. else
  293. {
  294. PrinterSelectedIndex = 0;
  295. }
  296. PrinterName = PrinterNameList[PrinterSelectedIndex];
  297. }
  298. private void InitPrintContentList()
  299. {
  300. PrintContentList = new List<string>();
  301. PrintContentList.Add("文档");
  302. PrintContentList.Add("文档和标记");
  303. PrintContentList.Add("文档和图章");
  304. PrintContentList.Add("文档和表单");
  305. }
  306. private void InitPrintOrientationList()
  307. {
  308. PrintOrientationList = new List<string>();
  309. PrintOrientationList.Add("纵向");
  310. PrintOrientationList.Add("横向");
  311. }
  312. public void InitGetPrintModNameFromRadioButton()
  313. {
  314. GetPrintModNameFromRadioButton.Clear();
  315. GetPrintModNameFromRadioButton.Add("ModSizeRdo", HomePagePrinterModSizeName);
  316. GetPrintModNameFromRadioButton.Add("ModPosterRdo", HomePagePrinterModPosterName);
  317. GetPrintModNameFromRadioButton.Add("ModMultipleRdo", HomePagePrinterModMultipleName);
  318. GetPrintModNameFromRadioButton.Add("ModBookletRdo", HomePagePrinterModBookletName);
  319. }
  320. /// <summary>
  321. /// 选择打印机
  322. /// </summary>
  323. /// <param name="e"></param>
  324. public void SelectPrinter(object e)
  325. {
  326. var comboBox = e as ComboBox;
  327. PrinterName = comboBox.SelectedItem.ToString();
  328. }
  329. /// <summary>
  330. /// 打开纸张设置窗口
  331. /// </summary>
  332. public void SetPaper()
  333. {
  334. if (true)
  335. {
  336. PageSetupDialog pageSetupDialog = new PageSetupDialog();
  337. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  338. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  339. if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  340. {
  341. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  342. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  343. {
  344. PrintOrientationIndex = 0;
  345. }
  346. else
  347. {
  348. PrintOrientationIndex = 1;
  349. }
  350. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode }); ;
  351. }
  352. }
  353. else
  354. {
  355. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  356. LoadPrinterSettings();
  357. DialogParameters value = new DialogParameters();
  358. value.Add("Unicode", Unicode);
  359. value.Add(ParameterNames.PrintQueue, printQueue);
  360. printDialogs.ShowDialog(DialogNames.HomePagePrinterPaperSettingsDialog, value, e =>
  361. {
  362. if (e.Result == ButtonResult.OK)
  363. {
  364. PrintSettingsInfo.PageMediaSize = e.Parameters.GetValue<PageMediaSize>("PageMediaSize");
  365. PrintSettingsInfo.Margin.Clear();
  366. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginLeft"));
  367. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginTop"));
  368. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginRight"));
  369. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginBottom"));
  370. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  371. }
  372. });
  373. }
  374. }
  375. public void ChangePrintMod(object e)
  376. {
  377. var rdo = e as System.Windows.Controls.RadioButton;
  378. CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
  379. if (CurrentHomePagePrinterModName == "HomePagePrinterModMultipleContent" || CurrentHomePagePrinterModName == "HomePagePrinterModBookletContent")
  380. {
  381. PrintPageBorderVisibility = Visibility.Visible;
  382. }
  383. else
  384. {
  385. PrintPageBorderVisibility = Visibility.Collapsed;
  386. }
  387. NavigationParameters param = new NavigationParameters();
  388. param.Add("document", PDFViewer.Document);
  389. param.Add("Unicode", Unicode);
  390. printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName, param);
  391. }
  392. /// <summary>
  393. /// 加载对应打印设置
  394. /// </summary>
  395. public void LoadPrinterSettings()
  396. {
  397. if (printQueue.GetPrintCapabilities().MaxCopyCount.HasValue)
  398. {
  399. MaxCopiesNumber = printQueue.GetPrintCapabilities().MaxCopyCount.Value;
  400. }
  401. else
  402. {
  403. MaxCopiesNumber = 1;
  404. }
  405. OutputColorList.Clear();
  406. foreach (OutputColor color in printQueue.GetPrintCapabilities().OutputColorCapability)
  407. {
  408. OutputColorList.Add(color);
  409. }
  410. }
  411. #region 来自UI的设置
  412. /// <summary>
  413. /// 设置灰度
  414. /// 改变打印机设置
  415. /// 改变渲染
  416. /// </summary>
  417. public void SetGrayscale(object e)
  418. {
  419. var chk = e as CheckBox;
  420. if ((bool)chk.IsChecked)
  421. {
  422. PrintSettingsInfo.IsGrayscale = true;
  423. }
  424. else if (!(bool)chk.IsChecked)
  425. {
  426. PrintSettingsInfo.IsGrayscale = false;
  427. }
  428. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  429. }
  430. /// <summary>
  431. /// 设置双面打印
  432. /// 改变打印流程
  433. /// 不改变打印相关参数
  434. /// </summary>
  435. /// <param name="e"></param>
  436. public void SetDuplex(object e)
  437. {
  438. var chk = e as CheckBox;
  439. if ((bool)chk.IsChecked)
  440. {
  441. PrintSettingsInfo.IsDuplex = true;
  442. }
  443. else if (!(bool)chk.IsChecked)
  444. {
  445. PrintSettingsInfo.IsDuplex = false;
  446. }
  447. }
  448. public void SetPageRangeSelectionIndex(object e)
  449. {
  450. var cmb = e as WritableComboBox;
  451. PrintSettingsInfo.PageRangeList.Clear();
  452. if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count - 1)
  453. {
  454. if (!isCurrentPage)
  455. {
  456. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  457. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusAllRange)
  458. {
  459. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  460. {
  461. PrintSettingsInfo.PageRangeList.Add(temp);
  462. }
  463. }
  464. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusOddRange)
  465. {
  466. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  467. {
  468. PrintSettingsInfo.PageRangeList.Add(temp);
  469. }
  470. }
  471. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusEvenRange)
  472. {
  473. //TODO: 偶数页页数不够异常
  474. if (PDFViewer.Document.PageCount <= 1)
  475. {
  476. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  477. {
  478. PrintSettingsInfo.PageRangeList.Add(temp);
  479. }
  480. }
  481. else
  482. {
  483. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  484. {
  485. PrintSettingsInfo.PageRangeList.Add(temp);
  486. }
  487. }
  488. }
  489. }
  490. else
  491. {
  492. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  493. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusAllRange)
  494. {
  495. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  496. {
  497. PrintSettingsInfo.PageRangeList.Add(temp);
  498. }
  499. }
  500. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCurrentPage)
  501. {
  502. PrintSettingsInfo.PageRangeList.Add(printCurrentPageIndex);
  503. }
  504. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusOddRange)
  505. {
  506. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  507. {
  508. PrintSettingsInfo.PageRangeList.Add(temp);
  509. }
  510. }
  511. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusEvenRange)
  512. {
  513. //TODO: 偶数页页数不够异常
  514. if (PDFViewer.Document.PageCount <= 1)
  515. {
  516. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  517. {
  518. PrintSettingsInfo.PageRangeList.Add(temp);
  519. }
  520. }
  521. else
  522. {
  523. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  524. {
  525. PrintSettingsInfo.PageRangeList.Add(temp);
  526. }
  527. }
  528. }
  529. }
  530. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  531. }
  532. else
  533. {
  534. if (!isCurrentPage)
  535. {
  536. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange;
  537. }
  538. else
  539. {
  540. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange;
  541. }
  542. }
  543. }
  544. public void SetCustomPageRange(object e)
  545. {
  546. if ((PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange&&!isCurrentPage)||(PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange && isCurrentPage))
  547. {
  548. var cmb = e as WritableComboBox;
  549. if (cmb == null)
  550. {
  551. return;
  552. }
  553. if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  554. { //TODO
  555. Trace.WriteLine("输入不对");
  556. MessageBoxEx.Show("输入不对");
  557. return;
  558. }
  559. PrintSettingsInfo.PageRangeList = DocumentTargetPages;
  560. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  561. }
  562. }
  563. public void SetPrintOritation(object e)
  564. {
  565. var cmb = e as ComboBox;
  566. if ((EnumPrintOrientation)cmb.SelectedIndex == EnumPrintOrientation.StatusPortrait)
  567. {
  568. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false;
  569. }
  570. else
  571. {
  572. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  573. }
  574. PrintSettingsInfo.EnumPrintOrientation = (EnumPrintOrientation)cmb.SelectedIndex;
  575. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  576. }
  577. public void SetPrintContent(object e)
  578. {
  579. var cmb = e as ComboBox;
  580. if (cmb.SelectedIndex == (int)EnumPrintContent.StatusOnlyDocument)
  581. {
  582. PrintSettingsInfo.IsPrintWithAnnot = false;
  583. PrintSettingsInfo.IsPrintWithForm = false;
  584. }
  585. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndMarkUps)
  586. {
  587. PrintSettingsInfo.IsPrintWithAnnot = true;
  588. PrintSettingsInfo.IsPrintWithForm = true;
  589. }
  590. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndStamps)
  591. {
  592. PrintSettingsInfo.IsPrintWithAnnot = true;
  593. PrintSettingsInfo.IsPrintWithForm = false;
  594. }
  595. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndForm)
  596. {
  597. PrintSettingsInfo.IsPrintWithAnnot = false;
  598. PrintSettingsInfo.IsPrintWithForm = true;
  599. }
  600. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  601. }
  602. public void SetReversePage(object e)
  603. {
  604. PrintSettingsInfo.PageRangeList.Reverse();
  605. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  606. }
  607. public void SetPrintBorder(object e)
  608. {
  609. var chk = e as CheckBox;
  610. if ((bool)chk.IsChecked)
  611. {
  612. PrintSettingsInfo.IsPrintPageBorde = true;
  613. }
  614. else
  615. {
  616. PrintSettingsInfo.IsPrintPageBorde = false;
  617. }
  618. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  619. }
  620. public void RecvPrintSettingsModInfo(PrintModInfoWithUnicode printModInfoWithUnicode)
  621. {
  622. if (printModInfoWithUnicode.printModInfo.EnumPrintMod == currentHomePagePrinterMod)
  623. {
  624. PrintSettingsInfo.PrintModInfo = printModInfoWithUnicode.printModInfo;
  625. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  626. }
  627. }
  628. public void RecvDuplexPrintMod(EnumDuplexPrintModWithUnicode enumDuplexPrintModWithUnicode)
  629. {
  630. PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod = enumDuplexPrintModWithUnicode.enumDuplexPrintMod;
  631. }
  632. #endregion
  633. public void Cancel()
  634. {
  635. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  636. }
  637. public void NavigateDocumentRegion()
  638. {
  639. NavigationParameters param = new NavigationParameters();
  640. param.Add(ParameterNames.PDFViewer, PDFViewer);
  641. param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo);
  642. param.Add("Unicode", Unicode);
  643. printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param);
  644. PageSetupDialog pageSetupDialog = new PageSetupDialog();
  645. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  646. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  647. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  648. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  649. {
  650. PrintOrientationIndex = 0;
  651. }
  652. else
  653. {
  654. PrintOrientationIndex = 1;
  655. }
  656. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  657. }
  658. public void NavigateModRegion()
  659. {
  660. NavigationParameters param = new NavigationParameters();
  661. param.Add(ParameterNames.PDFViewer, PDFViewer);
  662. param.Add("Unicode", Unicode);
  663. printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName, param);
  664. }
  665. public bool IsBookletSinglePage(PrintModInfo printModInfo)
  666. {
  667. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  668. {
  669. var bookletInfo = (BookletInfo)printModInfo;
  670. if (!(bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides))
  671. {
  672. return true;
  673. }
  674. }
  675. return false;
  676. }
  677. public bool IsBookletBothPage(PrintModInfo printModInfo)
  678. {
  679. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  680. {
  681. var bookletInfo = (BookletInfo)printModInfo;
  682. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  683. {
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. /// <summary>
  690. /// 确认打印,单面,双面或手动双面
  691. /// 大小模式支持单面,双面
  692. /// 海报模式只支持单面
  693. /// 多页模式支持单面,双面
  694. /// 小册子模式双面打印:双面
  695. /// 仅正面/背面:单面
  696. ///
  697. /// 单面打印:正常传参
  698. ///
  699. /// 双面打印:
  700. /// 长边翻页,短边翻页
  701. ///
  702. /// 自动双面打印
  703. /// 传值给打印机就行
  704. ///
  705. /// 手动双面打印
  706. /// 长边翻页 先打印奇数页,进入二阶段,正向打印偶数页
  707. /// 短边翻页 先打印奇数页,进入二阶段,反向打印偶数页
  708. ///
  709. /// 异常退出是否要挂起打印机线程?
  710. ///
  711. /// </summary>
  712. public void ConfirmPrint()
  713. {
  714. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  715. LoadPrinterSettings();
  716. Collection<Duplexing> list = new Collection<Duplexing>();
  717. if (printQueue.GetPrintCapabilities().DuplexingCapability.Count > 0)
  718. {
  719. list.Clear();
  720. for (int temp = 0; temp < printQueue.GetPrintCapabilities().DuplexingCapability.Count; temp++)
  721. {
  722. list.Add(printQueue.GetPrintCapabilities().DuplexingCapability[temp]);
  723. }
  724. }
  725. ///单面打印的情况:直接执行
  726. if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusNone && !IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  727. {
  728. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  729. }
  730. else if (IsBookletSinglePage(PrintSettingsInfo.PrintModInfo))
  731. {
  732. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  733. }
  734. ///双面打印的情况
  735. else if ((int)PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod > 0 || IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  736. {
  737. ///支持自动双面
  738. if (list.Count > 0)
  739. {
  740. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  741. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  742. }
  743. ///手动翻面
  744. ///手动翻面需要注意只有一页时和单面打印一致
  745. ///那奇数页呢?
  746. else
  747. {
  748. if ((PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize && PrintSettingsInfo.PageRangeList.Count == 1) || (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet && PrintSettingsInfo.PageRangeList.Count == 1))
  749. {
  750. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  751. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  752. }
  753. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  754. {
  755. var multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  756. if (PrintSettingsInfo.PageRangeList.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0)
  757. {
  758. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  759. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  760. }
  761. else
  762. {
  763. ///z
  764. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  765. if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
  766. {
  767. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  768. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  769. }
  770. else
  771. {
  772. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  773. }
  774. }
  775. }
  776. else
  777. {
  778. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  779. if (MessageBoxEx.Show("Printing on the front side of the paper is complete. Please reverse the paper, click 'OK' to print the reverse side", "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
  780. {
  781. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  782. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  783. }
  784. else
  785. {
  786. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  787. }
  788. }
  789. }
  790. }
  791. }
  792. public event Action<IDialogResult> RequestClose;
  793. public bool CanCloseDialog()
  794. {
  795. return true;
  796. }
  797. public void OnDialogClosed()
  798. {
  799. this.printQueue.Dispose();
  800. }
  801. public void OnDialogOpened(IDialogParameters parameters)
  802. {
  803. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  804. if (PDFViewer != null && PDFViewer.Document != null)
  805. {
  806. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  807. {
  808. PrintSettingsInfo.PageRangeList.Add(temp);
  809. }
  810. if (parameters.TryGetValue<int>(ParameterNames.PrintCurrentPage, out printCurrentPageIndex))
  811. {
  812. isCurrentPage = true;
  813. }
  814. else
  815. {
  816. isCurrentPage = false;
  817. }
  818. NavigationParameters param = new NavigationParameters();
  819. NavigateDocumentRegion();
  820. NavigateModRegion();
  821. }
  822. }
  823. }
  824. }