HomePagePrinterDialogViewModel.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  38. namespace PDF_Office.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. PDFViewer.CloseDocument();
  914. PDFViewer.InitDocument(filePath);
  915. PDFViewer.Document.UnlockWithPassword(result.Password);
  916. PDFViewer.Load();
  917. }
  918. ///TODO:
  919. ///此处填入需要执行的代码
  920. }
  921. if (!result.IsDiscryptied)
  922. {
  923. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  924. {
  925. this.RequestClose(new Prism.Services.Dialogs.DialogResult());
  926. }));
  927. }
  928. if (PDFViewer != null && PDFViewer.Document != null && result.IsDiscryptied)
  929. {
  930. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  931. {
  932. PrintSettingsInfo.PageRangeList.Add(temp);
  933. }
  934. if (parameters.TryGetValue<int>(ParameterNames.PrintCurrentPage, out printCurrentPageIndex))
  935. {
  936. isCurrentPage = true;
  937. }
  938. else
  939. {
  940. isCurrentPage = false;
  941. }
  942. if (parameters.TryGetValue<List<int>>(ParameterNames.PageList, out PageList))
  943. {
  944. PageListString = CommonHelper.GetPageParmFromList(PageList);
  945. if (!string.IsNullOrEmpty(PageListString))
  946. {
  947. if (PageList.Count == 1)
  948. {
  949. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  950. {
  951. PageRangeSelectIndex = 1;
  952. }));
  953. }
  954. else
  955. {
  956. PrintSettingsInfo.PageRangeList = PageList;
  957. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  958. {
  959. CustomRangeString = PageListString;
  960. PageRangeSelectIndex = 4;
  961. }));
  962. }
  963. }
  964. }
  965. System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
  966. if ((pageSize.Height / pageSize.Width > 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width < 1.0)
  967. || (pageSize.Height / pageSize.Width < 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width > 1.0))
  968. {
  969. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  970. PrintOrientationIndex = 0;
  971. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  972. }
  973. MaxPageRange = PDFViewer.Document.PageCount;
  974. NavigationParameters param = new NavigationParameters();
  975. NavigateDocumentRegion();
  976. NavigateModRegion();
  977. }
  978. }
  979. }
  980. }