HomePagePrinterDialogViewModel.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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_Master.Model;
  11. using System.Drawing;
  12. using System.Reflection;
  13. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  14. using System.Threading.Tasks;
  15. using ComPDFKit.PDFPage;
  16. using PDF_Master.Helper;
  17. using System.Drawing.Drawing2D;
  18. using ComPDFKitViewer.PdfViewer;
  19. using System.Windows.Media.Imaging;
  20. using System.Drawing.Imaging;
  21. using PDF_Master.CustomControl;
  22. using Prism.Events;
  23. using ImTools;
  24. using PDF_Master.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_Master.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. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  38. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  39. {
  40. public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware
  41. {
  42. #region 文案
  43. private string T_title;
  44. public string T_Title
  45. {
  46. get { return T_title; }
  47. set
  48. {
  49. SetProperty(ref T_title, value);
  50. }
  51. }
  52. private string T_printer;
  53. public string T_Printer
  54. {
  55. get { return T_printer; }
  56. set
  57. {
  58. SetProperty(ref T_printer, value);
  59. }
  60. }
  61. private string T_pageSettings;
  62. public string T_PageSettings
  63. {
  64. get { return T_pageSettings; }
  65. set
  66. {
  67. SetProperty(ref T_pageSettings, value);
  68. }
  69. }
  70. private string T_copies;
  71. public string T_Copies
  72. {
  73. get { return T_copies; }
  74. set
  75. {
  76. SetProperty(ref T_copies, value);
  77. }
  78. }
  79. private string T_blackAndWhite;
  80. public string T_BlackAndWhite
  81. {
  82. get { return T_blackAndWhite; }
  83. set
  84. {
  85. SetProperty(ref T_blackAndWhite, value);
  86. }
  87. }
  88. private string T_printSettings;
  89. public string T_PrintSettings
  90. {
  91. get { return T_printSettings; }
  92. set
  93. {
  94. SetProperty(ref T_printSettings, value);
  95. }
  96. }
  97. private string T_orientation;
  98. public string T_Orientation
  99. {
  100. get { return T_orientation; }
  101. set
  102. {
  103. SetProperty(ref T_orientation, value);
  104. }
  105. }
  106. private string T_printContent;
  107. public string T_PrintContent
  108. {
  109. get { return T_printContent; }
  110. set
  111. {
  112. SetProperty(ref T_printContent, value);
  113. }
  114. }
  115. private string T_reversePages;
  116. public string T_ReversePages
  117. {
  118. get { return T_reversePages; }
  119. set
  120. {
  121. SetProperty(ref T_reversePages, value);
  122. }
  123. }
  124. private void InitString()
  125. {
  126. T_Title = App.MainPageLoader.GetString("Print_Title");
  127. T_Printer = App.MainPageLoader.GetString("Print_Printer");
  128. T_PageSettings = App.MainPageLoader.GetString("Print_PageSettings");
  129. T_Copies = App.MainPageLoader.GetString("Print_Copies");
  130. T_BlackAndWhite = App.MainPageLoader.GetString("Print_BlackAndWhite");
  131. T_PrintSettings = App.MainPageLoader.GetString("Print_PrintSettings");
  132. T_Orientation = App.MainPageLoader.GetString("Print_Orientation");
  133. T_PrintContent = App.MainPageLoader.GetString("Print_PrintContent");
  134. T_ReversePages = App.MainPageLoader.GetString("Print_ReversePages");
  135. }
  136. #endregion
  137. public IRegionManager printRegion;
  138. public IDialogService printDialogs;
  139. public IEventAggregator printEvent;
  140. private CPDFViewer PDFViewer;
  141. public PrintSettingsInfo PrintSettingsInfo = new PrintSettingsInfo();
  142. List<int> PageList = new List<int>();
  143. #region 常用字符串
  144. public string Title => "";
  145. public string HomePagePrinterDocumentName = "HomePagePrinterDocumentContent";
  146. public string HomePagePrinterModSizeName = "HomePagePrinterModSizeContent";
  147. public string HomePagePrinterModPosterName = "HomePagePrinterModPosterContent";
  148. public string HomePagePrinterModMultipleName = "HomePagePrinterModMultipleContent";
  149. public string HomePagePrinterModBookletName = "HomePagePrinterModBookletContent";
  150. #endregion
  151. #region 字典
  152. public Dictionary<string, string> GetPrintModNameFromRadioButton = new Dictionary<string, string>();
  153. #endregion
  154. #region 打印系统配置
  155. private readonly PrintServer localDefaultPrintServer = new PrintServer();
  156. private PrintQueue printQueue;
  157. #endregion
  158. #region 打印参数设置
  159. /// <summary>
  160. /// (文档本身的)打印页集合
  161. /// </summary>
  162. private List<int> DocumentTargetPages = new List<int>();
  163. #endregion
  164. private int _printOrientationIndex = 0;
  165. public int PrintOrientationIndex
  166. {
  167. get => _printOrientationIndex;
  168. set => SetProperty(ref _printOrientationIndex, value);
  169. }
  170. private int _maxPageRange = 0;
  171. public int MaxPageRange
  172. {
  173. get => _maxPageRange;
  174. set => SetProperty(ref _maxPageRange, value);
  175. }
  176. private int _pageRangeSelectIndex = 0;
  177. public int PageRangeSelectIndex
  178. {
  179. get => _pageRangeSelectIndex;
  180. set => SetProperty(ref _pageRangeSelectIndex, value);
  181. }
  182. #region 相关region
  183. private string _homePagePrinterDocumentRegionName;
  184. public string HomePagePrinterDocumentRegionName
  185. {
  186. get { return _homePagePrinterDocumentRegionName; }
  187. set { _homePagePrinterDocumentRegionName = value; }
  188. }
  189. private string _homePagePrinterModRegionName;
  190. public string HomePagePrinterModRegionName
  191. {
  192. get { return _homePagePrinterModRegionName; }
  193. set { _homePagePrinterModRegionName = value; }
  194. }
  195. #endregion
  196. #region 打印参数设置
  197. /// <summary>
  198. /// 打印机名称
  199. /// </summary>
  200. public string PrinterName;
  201. private int _maxCopiesNumber;
  202. /// <summary>
  203. /// 最大打印份数
  204. /// </summary>
  205. public int MaxCopiesNumber
  206. {
  207. get => _maxCopiesNumber;
  208. set => SetProperty(ref _maxCopiesNumber, value);
  209. }
  210. private Visibility _printPageBorderVisibility = Visibility.Collapsed;
  211. public Visibility PrintPageBorderVisibility
  212. {
  213. get => _printPageBorderVisibility;
  214. set => SetProperty(ref _printPageBorderVisibility, value);
  215. }
  216. /// <summary>
  217. /// 打印机支持的颜色
  218. /// </summary>
  219. public List<OutputColor> OutputColorList = new List<OutputColor>();
  220. #endregion
  221. #region 组件
  222. private List<string> _printerNameList;
  223. /// <summary>
  224. /// 打印机名
  225. /// </summary>
  226. public List<string> PrinterNameList
  227. {
  228. get { return _printerNameList; }
  229. set { _printerNameList = value; }
  230. }
  231. private List<string> _printContentList;
  232. /// <summary>
  233. /// 打印内容
  234. /// </summary>
  235. public List<string> PrintContentList
  236. {
  237. get { return _printContentList; }
  238. set { _printContentList = value; }
  239. }
  240. private int _printContentIndex = 1;
  241. public int PrintContentIndex
  242. {
  243. get => _printContentIndex;
  244. set => SetProperty(ref _printContentIndex, value);
  245. }
  246. private List<string> _printerOrientationList;
  247. /// <summary>
  248. /// 打印列表
  249. /// </summary>
  250. public List<string> PrintOrientationList
  251. {
  252. get { return _printerOrientationList; }
  253. set
  254. {
  255. _printerOrientationList = value;
  256. }
  257. }
  258. private int _printerSelectedIndex;
  259. public int PrinterSelectedIndex
  260. {
  261. get => _printerSelectedIndex;
  262. set => SetProperty(ref _printerSelectedIndex, value);
  263. }
  264. public PageSetupDialog pageSetupDialog = new PageSetupDialog();
  265. #endregion
  266. #region 打印态
  267. private bool _isBothSides;
  268. public bool IsBothSides
  269. {
  270. get => _isBothSides;
  271. set => _isBothSides = value;
  272. }
  273. public bool FinishedFrontSideFlag = false;
  274. public int printCurrentPageIndex;
  275. public string PageListString = null;
  276. public bool isCurrentPage;
  277. /// <summary>
  278. /// 双面打印阶段:正面或背面
  279. /// </summary>
  280. public EnumBothSidesStage BothSidesStage = EnumBothSidesStage.StatusNone;
  281. #endregion
  282. #region 窗口相关参数
  283. private EnumPrintMod currentHomePagePrinterMod;
  284. private string _currentHomePagePrinterModName;
  285. public string CurrentHomePagePrinterModName
  286. {
  287. get => _currentHomePagePrinterModName;
  288. set
  289. {
  290. _currentHomePagePrinterModName = value;
  291. if (value == "HomePagePrinterModSizeContent")
  292. {
  293. currentHomePagePrinterMod = EnumPrintMod.StatusSize;
  294. }
  295. else if (value == "HomePagePrinterModPosterContent")
  296. {
  297. currentHomePagePrinterMod = EnumPrintMod.StatusPoster;
  298. }
  299. else if (value == "HomePagePrinterModMultipleContent")
  300. {
  301. currentHomePagePrinterMod = EnumPrintMod.StatusMultiple;
  302. }
  303. else if (value == "HomePagePrinterModBookletContent")
  304. {
  305. currentHomePagePrinterMod = EnumPrintMod.StatusBooklet;
  306. }
  307. }
  308. }
  309. private string _customRangeString;
  310. public string CustomRangeString
  311. {
  312. get => _customRangeString;
  313. set
  314. {
  315. SetProperty(ref _customRangeString, value);
  316. }
  317. }
  318. public bool IsFirstOpen = true;
  319. string Unicode = null;
  320. #endregion
  321. #region 委托
  322. public DelegateCommand CancelCommand { get; set; }
  323. public DelegateCommand ConfirmPrintCommand { get; set; }
  324. public DelegateCommand SetPaperCommand { get; set; }
  325. public DelegateCommand<object> SelectPrinterCommand { get; set; }
  326. public DelegateCommand<object> ChangePrintModCommand { get; set; }
  327. public DelegateCommand<object> SetGrayscaleCommand { get; set; }
  328. public DelegateCommand<object> SetDuplexCommand { get; set; }
  329. public DelegateCommand<object> SetPageRangeSelectionIndexCommand { get; set; }
  330. public DelegateCommand<object> SetCustomPageRangeCommand { get; set; }
  331. public DelegateCommand<object> SetPrintContentCommand { get; set; }
  332. public DelegateCommand<object> SetReversePageCommand { get; set; }
  333. public DelegateCommand<object> SetPrintBorderCommand { get; set; }
  334. public DelegateCommand<object> SetPrintOritationCommand { get; set; }
  335. #endregion
  336. HomePagePrinterDialogViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  337. {
  338. InitString();
  339. this.printRegion = regionManager;
  340. this.printDialogs = dialogService;
  341. this.printEvent = eventAggregator;
  342. HomePagePrinterDocumentRegionName = Guid.NewGuid().ToString();
  343. HomePagePrinterModRegionName = Guid.NewGuid().ToString();
  344. CancelCommand = new DelegateCommand(Cancel);
  345. ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
  346. ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
  347. SetPaperCommand = new DelegateCommand(SetPaper);
  348. SelectPrinterCommand = new DelegateCommand<object>(SelectPrinter);
  349. SetGrayscaleCommand = new DelegateCommand<object>(SetGrayscale);
  350. SetDuplexCommand = new DelegateCommand<object>(SetDuplex);
  351. SetPageRangeSelectionIndexCommand = new DelegateCommand<object>(SetPageRangeSelectionIndex);
  352. SetCustomPageRangeCommand = new DelegateCommand<object>(SetCustomPageRange);
  353. SetPrintContentCommand = new DelegateCommand<object>(SetPrintContent);
  354. SetReversePageCommand = new DelegateCommand<object>(SetReversePage);
  355. SetPrintBorderCommand = new DelegateCommand<object>(SetPrintBorder);
  356. SetPrintOritationCommand = new DelegateCommand<object>(SetPrintOritation);
  357. Unicode = Guid.NewGuid().ToString();
  358. InitComponent();
  359. InitDictionary();
  360. InitParams();
  361. pageSetupDialog = new PageSetupDialog();
  362. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  363. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
  364. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false;
  365. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  366. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  367. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  368. {
  369. PrintOrientationIndex = 0;
  370. }
  371. else
  372. {
  373. PrintOrientationIndex = 1;
  374. }
  375. printEvent.GetEvent<SendPrintSettingsModInfoEvent>().Subscribe(RecvPrintSettingsModInfo, e => e.Unicode == Unicode);
  376. printEvent.GetEvent<SendDuplexPrintModEvent>().Subscribe(RecvDuplexPrintMod, e => e.Unicode == Unicode);
  377. printEvent.GetEvent<SendFinishedFrontSideEvent>().Subscribe(RecvFinishedFrontSide, e => e.Unicode == Unicode);
  378. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  379. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  380. {
  381. PrintOrientationIndex = 0;
  382. }
  383. else
  384. {
  385. PrintOrientationIndex = 1;
  386. }
  387. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  388. }
  389. private void InitComponent()
  390. {
  391. InitPrinterNameList();
  392. InitPrintContentList();
  393. InitPrintOrientationList();
  394. }
  395. private void InitDictionary()
  396. {
  397. InitGetPrintModNameFromRadioButton();
  398. }
  399. private void InitParams()
  400. {
  401. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  402. List<PageMediaSize> PageMediaSizeList = new List<PageMediaSize>();
  403. }
  404. private void InitPrinterNameList()
  405. {
  406. PrinterNameList = new List<string>();
  407. foreach (string printerListItem in PrinterSettings.InstalledPrinters)
  408. {
  409. PrinterNameList.Add(printerListItem);
  410. }
  411. if (PrinterNameList.Contains("Microsoft Print to PDF"))
  412. {
  413. PrinterSelectedIndex = PrinterNameList.IndexOf("Microsoft Print to PDF");
  414. }
  415. else
  416. {
  417. PrinterSelectedIndex = 0;
  418. }
  419. PrinterName = PrinterNameList[PrinterSelectedIndex];
  420. }
  421. private void InitPrintContentList()
  422. {
  423. PrintContentList = new List<string>();
  424. PrintContentList.Add("文档");
  425. PrintContentList.Add("文档和标记");
  426. PrintContentList.Add("文档和图章");
  427. PrintContentList.Add("文档和表单");
  428. }
  429. private void InitPrintOrientationList()
  430. {
  431. PrintOrientationList = new List<string>();
  432. PrintOrientationList.Add("纵向");
  433. PrintOrientationList.Add("横向");
  434. }
  435. public void InitGetPrintModNameFromRadioButton()
  436. {
  437. GetPrintModNameFromRadioButton.Clear();
  438. GetPrintModNameFromRadioButton.Add("ModSizeRdo", HomePagePrinterModSizeName);
  439. GetPrintModNameFromRadioButton.Add("ModPosterRdo", HomePagePrinterModPosterName);
  440. GetPrintModNameFromRadioButton.Add("ModMultipleRdo", HomePagePrinterModMultipleName);
  441. GetPrintModNameFromRadioButton.Add("ModBookletRdo", HomePagePrinterModBookletName);
  442. }
  443. /// <summary>
  444. /// 选择打印机
  445. /// </summary>
  446. /// <param name="e"></param>
  447. public void SelectPrinter(object e)
  448. {
  449. var comboBox = e as ComboBox;
  450. PrinterName = comboBox.SelectedItem.ToString();
  451. }
  452. /// <summary>
  453. /// 打开纸张设置窗口
  454. /// </summary>
  455. public void SetPaper()
  456. {
  457. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  458. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  459. pageSetupDialog.Document.DefaultPageSettings.Margins = new Margins((int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Left * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Right * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Top * 2.54), (int)(PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins.Bottom * 2.54));
  460. if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  461. {
  462. PrintSettingsInfo.IsPaperSizeChanged = true;
  463. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  464. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  465. {
  466. PrintOrientationIndex = 0;
  467. }
  468. else
  469. {
  470. PrintOrientationIndex = 1;
  471. }
  472. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  473. PrintSettingsInfo.IsPaperSizeChanged = false;
  474. }
  475. }
  476. public void ChangePrintMod(object e)
  477. {
  478. var rdo = e as System.Windows.Controls.RadioButton;
  479. CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
  480. if (CurrentHomePagePrinterModName == "HomePagePrinterModMultipleContent" || CurrentHomePagePrinterModName == "HomePagePrinterModBookletContent")
  481. {
  482. PrintPageBorderVisibility = Visibility.Visible;
  483. }
  484. else
  485. {
  486. PrintPageBorderVisibility = Visibility.Collapsed;
  487. }
  488. NavigationParameters param = new NavigationParameters();
  489. param.Add("document", PDFViewer.Document);
  490. param.Add("Unicode", Unicode);
  491. printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName, param);
  492. }
  493. /// <summary>
  494. /// 加载对应打印设置
  495. /// </summary>
  496. public void LoadPrinterSettings()
  497. {
  498. if (printQueue.GetPrintCapabilities().MaxCopyCount.HasValue)
  499. {
  500. MaxCopiesNumber = printQueue.GetPrintCapabilities().MaxCopyCount.Value;
  501. }
  502. else
  503. {
  504. MaxCopiesNumber = 1;
  505. }
  506. OutputColorList.Clear();
  507. foreach (OutputColor color in printQueue.GetPrintCapabilities().OutputColorCapability)
  508. {
  509. OutputColorList.Add(color);
  510. }
  511. }
  512. #region 来自UI的设置
  513. /// <summary>
  514. /// 设置灰度
  515. /// 改变打印机设置
  516. /// 改变渲染
  517. /// </summary>
  518. public void SetGrayscale(object e)
  519. {
  520. var chk = e as CheckBox;
  521. if ((bool)chk.IsChecked)
  522. {
  523. PrintSettingsInfo.IsGrayscale = true;
  524. }
  525. else if (!(bool)chk.IsChecked)
  526. {
  527. PrintSettingsInfo.IsGrayscale = false;
  528. }
  529. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  530. }
  531. /// <summary>
  532. /// 设置双面打印
  533. /// 改变打印流程
  534. /// 不改变打印相关参数
  535. /// </summary>
  536. /// <param name="e"></param>
  537. public void SetDuplex(object e)
  538. {
  539. var chk = e as CheckBox;
  540. if ((bool)chk.IsChecked)
  541. {
  542. PrintSettingsInfo.IsDuplex = true;
  543. }
  544. else if (!(bool)chk.IsChecked)
  545. {
  546. PrintSettingsInfo.IsDuplex = false;
  547. }
  548. }
  549. public void SetPageRangeSelectionIndex(object e)
  550. {
  551. var cmb = e as WritableComboBox;
  552. PrintSettingsInfo.PageRangeList.Clear();
  553. if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count - 1)
  554. {
  555. if (!isCurrentPage)
  556. {
  557. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  558. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusAllRange)
  559. {
  560. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  561. {
  562. PrintSettingsInfo.PageRangeList.Add(temp);
  563. }
  564. }
  565. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusOddRange)
  566. {
  567. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  568. {
  569. PrintSettingsInfo.PageRangeList.Add(temp);
  570. }
  571. }
  572. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusEvenRange)
  573. {
  574. //TODO: 偶数页页数不够异常
  575. if (PDFViewer.Document.PageCount <= 1)
  576. {
  577. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  578. {
  579. PrintSettingsInfo.PageRangeList.Add(temp);
  580. }
  581. PageRangeSelectIndex = 0;
  582. }
  583. else
  584. {
  585. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  586. {
  587. PrintSettingsInfo.PageRangeList.Add(temp);
  588. }
  589. }
  590. }
  591. }
  592. else
  593. {
  594. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  595. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusAllRange)
  596. {
  597. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  598. {
  599. PrintSettingsInfo.PageRangeList.Add(temp);
  600. }
  601. }
  602. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCurrentPage)
  603. {
  604. PrintSettingsInfo.PageRangeList.Add(printCurrentPageIndex);
  605. }
  606. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusOddRange)
  607. {
  608. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  609. {
  610. PrintSettingsInfo.PageRangeList.Add(temp);
  611. }
  612. }
  613. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusEvenRange)
  614. {
  615. //TODO: 偶数页页数不够异常
  616. if (PDFViewer.Document.PageCount <= 1)
  617. {
  618. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  619. {
  620. PrintSettingsInfo.PageRangeList.Add(temp);
  621. }
  622. PageRangeSelectIndex = 0;
  623. }
  624. else
  625. {
  626. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  627. {
  628. PrintSettingsInfo.PageRangeList.Add(temp);
  629. }
  630. }
  631. }
  632. }
  633. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  634. }
  635. else
  636. {
  637. if (!isCurrentPage)
  638. {
  639. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  640. {
  641. PrintSettingsInfo.PageRangeList.Add(temp);
  642. }
  643. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange;
  644. }
  645. else
  646. {
  647. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  648. {
  649. PrintSettingsInfo.PageRangeList.Add(temp);
  650. }
  651. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange;
  652. }
  653. }
  654. }
  655. public void SetCustomPageRange(object e)
  656. {
  657. if ((PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange && !isCurrentPage) || (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange && isCurrentPage))
  658. {
  659. var cmb = e as WritableComboBox;
  660. if (cmb == null)
  661. {
  662. return;
  663. }
  664. if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  665. { //TODO
  666. Trace.WriteLine("输入不对");
  667. MessageBoxEx.Show("输入不对");
  668. return;
  669. }
  670. PrintSettingsInfo.PageRangeList = DocumentTargetPages;
  671. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  672. }
  673. }
  674. public void SetPrintOritation(object e)
  675. {
  676. var cmb = e as ComboBox;
  677. if ((EnumPrintOrientation)cmb.SelectedIndex == EnumPrintOrientation.StatusPortrait)
  678. {
  679. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false;
  680. }
  681. else
  682. {
  683. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  684. }
  685. PrintSettingsInfo.EnumPrintOrientation = (EnumPrintOrientation)cmb.SelectedIndex;
  686. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  687. }
  688. public void SetPrintContent(object e)
  689. {
  690. var cmb = e as ComboBox;
  691. if (cmb.SelectedIndex == (int)EnumPrintContent.StatusOnlyDocument)
  692. {
  693. PrintSettingsInfo.IsPrintWithAnnot = false;
  694. PrintSettingsInfo.IsPrintWithForm = false;
  695. }
  696. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndMarkUps)
  697. {
  698. PrintSettingsInfo.IsPrintWithAnnot = true;
  699. PrintSettingsInfo.IsPrintWithForm = true;
  700. }
  701. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndStamps)
  702. {
  703. PrintSettingsInfo.IsPrintWithAnnot = true;
  704. PrintSettingsInfo.IsPrintWithForm = false;
  705. }
  706. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndForm)
  707. {
  708. PrintSettingsInfo.IsPrintWithAnnot = false;
  709. PrintSettingsInfo.IsPrintWithForm = true;
  710. }
  711. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  712. }
  713. public void SetReversePage(object e)
  714. {
  715. PrintSettingsInfo.PageRangeList.Reverse();
  716. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  717. }
  718. public void SetPrintBorder(object e)
  719. {
  720. var chk = e as CheckBox;
  721. if ((bool)chk.IsChecked)
  722. {
  723. PrintSettingsInfo.IsPrintPageBorde = true;
  724. }
  725. else
  726. {
  727. PrintSettingsInfo.IsPrintPageBorde = false;
  728. }
  729. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  730. }
  731. public void RecvPrintSettingsModInfo(PrintModInfoWithUnicode printModInfoWithUnicode)
  732. {
  733. if (printModInfoWithUnicode.printModInfo.EnumPrintMod == currentHomePagePrinterMod)
  734. {
  735. PrintSettingsInfo.PrintModInfo = printModInfoWithUnicode.printModInfo;
  736. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  737. }
  738. }
  739. public void RecvDuplexPrintMod(EnumDuplexPrintModWithUnicode enumDuplexPrintModWithUnicode)
  740. {
  741. PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod = enumDuplexPrintModWithUnicode.enumDuplexPrintMod;
  742. }
  743. public void RecvFinishedFrontSide(FinishedFrontSide finishedFrontSide)
  744. {
  745. FinishedFrontSideFlag = true;
  746. }
  747. #endregion
  748. public void Cancel()
  749. {
  750. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  751. }
  752. public void NavigateDocumentRegion()
  753. {
  754. NavigationParameters param = new NavigationParameters();
  755. param.Add(ParameterNames.PDFViewer, PDFViewer);
  756. param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo);
  757. param.Add("Unicode", Unicode);
  758. printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param);
  759. PageSetupDialog pageSetupDialog = new PageSetupDialog();
  760. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  761. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  762. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  763. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  764. {
  765. PrintOrientationIndex = 0;
  766. }
  767. else
  768. {
  769. PrintOrientationIndex = 1;
  770. }
  771. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  772. }
  773. public void NavigateModRegion()
  774. {
  775. NavigationParameters param = new NavigationParameters();
  776. param.Add(ParameterNames.PDFViewer, PDFViewer);
  777. param.Add("Unicode", Unicode);
  778. printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName, param);
  779. }
  780. public bool IsBookletSinglePage(PrintModInfo printModInfo)
  781. {
  782. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  783. {
  784. var bookletInfo = (BookletInfo)printModInfo;
  785. if (!(bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides))
  786. {
  787. return true;
  788. }
  789. }
  790. return false;
  791. }
  792. public bool IsBookletBothPage(PrintModInfo printModInfo)
  793. {
  794. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  795. {
  796. var bookletInfo = (BookletInfo)printModInfo;
  797. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  798. {
  799. return true;
  800. }
  801. }
  802. return false;
  803. }
  804. /// <summary>
  805. /// 确认打印,单面,双面或手动双面
  806. /// 大小模式支持单面,双面
  807. /// 海报模式只支持单面
  808. /// 多页模式支持单面,双面
  809. /// 小册子模式双面打印:双面
  810. /// 仅正面/背面:单面
  811. ///
  812. /// 单面打印:正常传参
  813. ///
  814. /// 双面打印:
  815. /// 长边翻页,短边翻页
  816. ///
  817. /// 自动双面打印
  818. /// 传值给打印机就行
  819. ///
  820. /// 手动双面打印
  821. /// 长边翻页 先打印奇数页,进入二阶段,正向打印偶数页
  822. /// 短边翻页 先打印奇数页,进入二阶段,反向打印偶数页
  823. ///
  824. /// 异常退出是否要挂起打印机线程?
  825. ///
  826. /// </summary>
  827. public void ConfirmPrint()
  828. {
  829. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  830. LoadPrinterSettings();
  831. Collection<Duplexing> list = new Collection<Duplexing>();
  832. if (printQueue.GetPrintCapabilities().DuplexingCapability.Count > 0)
  833. {
  834. list.Clear();
  835. for (int temp = 0; temp < printQueue.GetPrintCapabilities().DuplexingCapability.Count; temp++)
  836. {
  837. list.Add(printQueue.GetPrintCapabilities().DuplexingCapability[temp]);
  838. }
  839. }
  840. ///单面打印的情况:直接执行
  841. if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusNone && !IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  842. {
  843. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  844. }
  845. else if (IsBookletSinglePage(PrintSettingsInfo.PrintModInfo))
  846. {
  847. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  848. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  849. }
  850. ///双面打印的情况
  851. else if ((int)PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod > 0 || IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  852. {
  853. ///支持自动双面
  854. if (list.Count > 0)
  855. {
  856. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  857. }
  858. ///手动翻面
  859. ///手动翻面需要注意只有一页时和单面打印一致
  860. ///那奇数页呢?
  861. else
  862. {
  863. if ((PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize && PrintSettingsInfo.PageRangeList.Count == 1) || (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet && PrintSettingsInfo.PageRangeList.Count == 1))
  864. {
  865. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  866. }
  867. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  868. {
  869. var multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  870. if (PrintSettingsInfo.PageRangeList.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0)
  871. {
  872. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  873. }
  874. else
  875. {
  876. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  877. if (FinishedFrontSideFlag && 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)
  878. {
  879. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  880. }
  881. }
  882. }
  883. else
  884. {
  885. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  886. if (FinishedFrontSideFlag && 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)
  887. {
  888. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  889. }
  890. }
  891. }
  892. }
  893. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  894. }
  895. public event Action<IDialogResult> RequestClose;
  896. public bool CanCloseDialog()
  897. {
  898. return true;
  899. }
  900. public void OnDialogClosed()
  901. {
  902. this.printQueue.Dispose();
  903. }
  904. public void OnDialogOpened(IDialogParameters parameters)
  905. {
  906. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  907. VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, Model.Dialog.ToolsDialogs.SaftyDialogs.EnumPermissionsSet.StatusAllowsPrinting, printDialogs);
  908. if (result.IsDiscryptied)
  909. {
  910. if (result.Password != null)
  911. {
  912. string filePath = PDFViewer.Document.FilePath;
  913. if (PDFViewer.Document.IsLocked)
  914. {
  915. PDFViewer.Document.UnlockWithPassword(result.Password);
  916. }
  917. else
  918. {
  919. PDFViewer.Document.CheckOwnerPassword(result.Password);
  920. }
  921. }
  922. ///TODO:
  923. ///此处填入需要执行的代码
  924. }
  925. if (!result.IsDiscryptied)
  926. {
  927. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  928. {
  929. this.RequestClose(new Prism.Services.Dialogs.DialogResult());
  930. }));
  931. }
  932. if (PDFViewer != null && PDFViewer.Document != null && result.IsDiscryptied)
  933. {
  934. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  935. {
  936. PrintSettingsInfo.PageRangeList.Add(temp);
  937. }
  938. if (parameters.TryGetValue<int>(ParameterNames.PrintCurrentPage, out printCurrentPageIndex))
  939. {
  940. isCurrentPage = true;
  941. }
  942. else
  943. {
  944. isCurrentPage = false;
  945. }
  946. if (parameters.TryGetValue<List<int>>(ParameterNames.PageList, out PageList))
  947. {
  948. PageListString = CommonHelper.GetPageParmFromList(PageList);
  949. if (!string.IsNullOrEmpty(PageListString))
  950. {
  951. if (PageList.Count == 1)
  952. {
  953. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  954. {
  955. PageRangeSelectIndex = 1;
  956. }));
  957. }
  958. else
  959. {
  960. PrintSettingsInfo.PageRangeList = PageList;
  961. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  962. {
  963. CustomRangeString = PageListString;
  964. PageRangeSelectIndex = 4;
  965. }));
  966. }
  967. }
  968. }
  969. System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
  970. if ((pageSize.Height / pageSize.Width > 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width < 1.0)
  971. || (pageSize.Height / pageSize.Width < 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width > 1.0))
  972. {
  973. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  974. PrintOrientationIndex = 0;
  975. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  976. }
  977. MaxPageRange = PDFViewer.Document.PageCount;
  978. NavigationParameters param = new NavigationParameters();
  979. NavigateDocumentRegion();
  980. NavigateModRegion();
  981. }
  982. }
  983. }
  984. }