HomePagePrinterDialogViewModel.cs 39 KB

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