HomePagePrinterDialogViewModel.cs 42 KB

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