HomePagePrinterDialogViewModel.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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. if (true)
  350. {
  351. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  352. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  353. if (pageSetupDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  354. {
  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. }
  366. }
  367. else
  368. {
  369. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  370. LoadPrinterSettings();
  371. DialogParameters value = new DialogParameters();
  372. value.Add("Unicode", Unicode);
  373. value.Add(ParameterNames.PrintQueue, printQueue);
  374. printDialogs.ShowDialog(DialogNames.HomePagePrinterPaperSettingsDialog, value, e =>
  375. {
  376. if (e.Result == ButtonResult.OK)
  377. {
  378. PrintSettingsInfo.PageMediaSize = e.Parameters.GetValue<PageMediaSize>("PageMediaSize");
  379. PrintSettingsInfo.Margin.Clear();
  380. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginLeft"));
  381. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginTop"));
  382. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginRight"));
  383. PrintSettingsInfo.Margin.Add(e.Parameters.GetValue<int>("MarginBottom"));
  384. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  385. }
  386. });
  387. }
  388. }
  389. public void ChangePrintMod(object e)
  390. {
  391. var rdo = e as System.Windows.Controls.RadioButton;
  392. CurrentHomePagePrinterModName = GetPrintModNameFromRadioButton[rdo.Name];
  393. if (CurrentHomePagePrinterModName == "HomePagePrinterModMultipleContent" || CurrentHomePagePrinterModName == "HomePagePrinterModBookletContent")
  394. {
  395. PrintPageBorderVisibility = Visibility.Visible;
  396. }
  397. else
  398. {
  399. PrintPageBorderVisibility = Visibility.Collapsed;
  400. }
  401. NavigationParameters param = new NavigationParameters();
  402. param.Add("document", PDFViewer.Document);
  403. param.Add("Unicode", Unicode);
  404. printRegion.RequestNavigate(HomePagePrinterModRegionName, CurrentHomePagePrinterModName, param);
  405. }
  406. /// <summary>
  407. /// 加载对应打印设置
  408. /// </summary>
  409. public void LoadPrinterSettings()
  410. {
  411. if (printQueue.GetPrintCapabilities().MaxCopyCount.HasValue)
  412. {
  413. MaxCopiesNumber = printQueue.GetPrintCapabilities().MaxCopyCount.Value;
  414. }
  415. else
  416. {
  417. MaxCopiesNumber = 1;
  418. }
  419. OutputColorList.Clear();
  420. foreach (OutputColor color in printQueue.GetPrintCapabilities().OutputColorCapability)
  421. {
  422. OutputColorList.Add(color);
  423. }
  424. }
  425. #region 来自UI的设置
  426. /// <summary>
  427. /// 设置灰度
  428. /// 改变打印机设置
  429. /// 改变渲染
  430. /// </summary>
  431. public void SetGrayscale(object e)
  432. {
  433. var chk = e as CheckBox;
  434. if ((bool)chk.IsChecked)
  435. {
  436. PrintSettingsInfo.IsGrayscale = true;
  437. }
  438. else if (!(bool)chk.IsChecked)
  439. {
  440. PrintSettingsInfo.IsGrayscale = false;
  441. }
  442. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  443. }
  444. /// <summary>
  445. /// 设置双面打印
  446. /// 改变打印流程
  447. /// 不改变打印相关参数
  448. /// </summary>
  449. /// <param name="e"></param>
  450. public void SetDuplex(object e)
  451. {
  452. var chk = e as CheckBox;
  453. if ((bool)chk.IsChecked)
  454. {
  455. PrintSettingsInfo.IsDuplex = true;
  456. }
  457. else if (!(bool)chk.IsChecked)
  458. {
  459. PrintSettingsInfo.IsDuplex = false;
  460. }
  461. }
  462. public void SetPageRangeSelectionIndex(object e)
  463. {
  464. var cmb = e as WritableComboBox;
  465. PrintSettingsInfo.PageRangeList.Clear();
  466. if (int.Parse(cmb.SelectedIndex) < cmb.Items.Count - 1)
  467. {
  468. if (!isCurrentPage)
  469. {
  470. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  471. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusAllRange)
  472. {
  473. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  474. {
  475. PrintSettingsInfo.PageRangeList.Add(temp);
  476. }
  477. }
  478. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusOddRange)
  479. {
  480. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  481. {
  482. PrintSettingsInfo.PageRangeList.Add(temp);
  483. }
  484. }
  485. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusEvenRange)
  486. {
  487. //TODO: 偶数页页数不够异常
  488. if (PDFViewer.Document.PageCount <= 1)
  489. {
  490. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  491. {
  492. PrintSettingsInfo.PageRangeList.Add(temp);
  493. }
  494. PageRangeSelectIndex = 0;
  495. }
  496. else
  497. {
  498. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  499. {
  500. PrintSettingsInfo.PageRangeList.Add(temp);
  501. }
  502. }
  503. }
  504. }
  505. else
  506. {
  507. PrintSettingsInfo.EnumPageRange = int.Parse(cmb.SelectedIndex);
  508. if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusAllRange)
  509. {
  510. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  511. {
  512. PrintSettingsInfo.PageRangeList.Add(temp);
  513. }
  514. }
  515. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCurrentPage)
  516. {
  517. PrintSettingsInfo.PageRangeList.Add(printCurrentPageIndex);
  518. }
  519. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusOddRange)
  520. {
  521. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp += 2)
  522. {
  523. PrintSettingsInfo.PageRangeList.Add(temp);
  524. }
  525. }
  526. else if (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusEvenRange)
  527. {
  528. //TODO: 偶数页页数不够异常
  529. if (PDFViewer.Document.PageCount <= 1)
  530. {
  531. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  532. {
  533. PrintSettingsInfo.PageRangeList.Add(temp);
  534. }
  535. PageRangeSelectIndex = 0;
  536. }
  537. else
  538. {
  539. for (int temp = 1; temp < PDFViewer.Document.PageCount; temp += 2)
  540. {
  541. PrintSettingsInfo.PageRangeList.Add(temp);
  542. }
  543. }
  544. }
  545. }
  546. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  547. }
  548. else
  549. {
  550. if (!isCurrentPage)
  551. {
  552. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  553. {
  554. PrintSettingsInfo.PageRangeList.Add(temp);
  555. }
  556. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange;
  557. }
  558. else
  559. {
  560. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  561. {
  562. PrintSettingsInfo.PageRangeList.Add(temp);
  563. }
  564. PrintSettingsInfo.EnumPageRange = (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange;
  565. }
  566. }
  567. }
  568. public void SetCustomPageRange(object e)
  569. {
  570. if ((PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithoutCurrentPage.StatusCustomizedRange && !isCurrentPage) || (PrintSettingsInfo.EnumPageRange == (int)EnumPageRangeWithCurrentPage.StatusCustomizedRange && isCurrentPage))
  571. {
  572. var cmb = e as WritableComboBox;
  573. if (cmb == null)
  574. {
  575. return;
  576. }
  577. if (!CommonHelper.GetPagesInRange(ref DocumentTargetPages, cmb.Text, PDFViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  578. { //TODO
  579. Trace.WriteLine("输入不对");
  580. MessageBoxEx.Show("输入不对");
  581. return;
  582. }
  583. PrintSettingsInfo.PageRangeList = DocumentTargetPages;
  584. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  585. }
  586. }
  587. public void SetPrintOritation(object e)
  588. {
  589. var cmb = e as ComboBox;
  590. if ((EnumPrintOrientation)cmb.SelectedIndex == EnumPrintOrientation.StatusPortrait)
  591. {
  592. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = false;
  593. }
  594. else
  595. {
  596. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  597. }
  598. PrintSettingsInfo.EnumPrintOrientation = (EnumPrintOrientation)cmb.SelectedIndex;
  599. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  600. }
  601. public void SetPrintContent(object e)
  602. {
  603. var cmb = e as ComboBox;
  604. if (cmb.SelectedIndex == (int)EnumPrintContent.StatusOnlyDocument)
  605. {
  606. PrintSettingsInfo.IsPrintWithAnnot = false;
  607. PrintSettingsInfo.IsPrintWithForm = false;
  608. }
  609. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndMarkUps)
  610. {
  611. PrintSettingsInfo.IsPrintWithAnnot = true;
  612. PrintSettingsInfo.IsPrintWithForm = true;
  613. }
  614. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndStamps)
  615. {
  616. PrintSettingsInfo.IsPrintWithAnnot = true;
  617. PrintSettingsInfo.IsPrintWithForm = false;
  618. }
  619. else if (cmb.SelectedIndex == (int)EnumPrintContent.StatusDocumentAndForm)
  620. {
  621. PrintSettingsInfo.IsPrintWithAnnot = false;
  622. PrintSettingsInfo.IsPrintWithForm = true;
  623. }
  624. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  625. }
  626. public void SetReversePage(object e)
  627. {
  628. PrintSettingsInfo.PageRangeList.Reverse();
  629. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  630. }
  631. public void SetPrintBorder(object e)
  632. {
  633. var chk = e as CheckBox;
  634. if ((bool)chk.IsChecked)
  635. {
  636. PrintSettingsInfo.IsPrintPageBorde = true;
  637. }
  638. else
  639. {
  640. PrintSettingsInfo.IsPrintPageBorde = false;
  641. }
  642. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  643. }
  644. public void RecvPrintSettingsModInfo(PrintModInfoWithUnicode printModInfoWithUnicode)
  645. {
  646. if (printModInfoWithUnicode.printModInfo.EnumPrintMod == currentHomePagePrinterMod)
  647. {
  648. PrintSettingsInfo.PrintModInfo = printModInfoWithUnicode.printModInfo;
  649. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  650. }
  651. }
  652. public void RecvDuplexPrintMod(EnumDuplexPrintModWithUnicode enumDuplexPrintModWithUnicode)
  653. {
  654. PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod = enumDuplexPrintModWithUnicode.enumDuplexPrintMod;
  655. }
  656. public void RecvFinishedFrontSide(FinishedFrontSide finishedFrontSide)
  657. {
  658. FinishedFrontSideFlag = true;
  659. }
  660. #endregion
  661. public void Cancel()
  662. {
  663. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  664. }
  665. public void NavigateDocumentRegion()
  666. {
  667. NavigationParameters param = new NavigationParameters();
  668. param.Add(ParameterNames.PDFViewer, PDFViewer);
  669. param.Add(ParameterNames.PrintSettingsInfo, PrintSettingsInfo);
  670. param.Add("Unicode", Unicode);
  671. printRegion.RequestNavigate(HomePagePrinterDocumentRegionName, HomePagePrinterDocumentName, param);
  672. PageSetupDialog pageSetupDialog = new PageSetupDialog();
  673. PrintSettingsInfo.PrintDocument.PrinterSettings.PrinterName = PrinterName;
  674. pageSetupDialog.Document = PrintSettingsInfo.PrintDocument;
  675. PrintSettingsInfo.PrintDocument.DefaultPageSettings = pageSetupDialog.PageSettings;
  676. if (!PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape)
  677. {
  678. PrintOrientationIndex = 0;
  679. }
  680. else
  681. {
  682. PrintOrientationIndex = 1;
  683. }
  684. this.printEvent.GetEvent<SendPrintSettingsInfoEvent>().Publish(new PrintSettingsInfoWithUnicode { printSettingsInfo = this.PrintSettingsInfo, Unicode = this.Unicode });
  685. }
  686. public void NavigateModRegion()
  687. {
  688. NavigationParameters param = new NavigationParameters();
  689. param.Add(ParameterNames.PDFViewer, PDFViewer);
  690. param.Add("Unicode", Unicode);
  691. printRegion.RequestNavigate(HomePagePrinterModRegionName, HomePagePrinterModSizeName, param);
  692. }
  693. public bool IsBookletSinglePage(PrintModInfo printModInfo)
  694. {
  695. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  696. {
  697. var bookletInfo = (BookletInfo)printModInfo;
  698. if (!(bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides))
  699. {
  700. return true;
  701. }
  702. }
  703. return false;
  704. }
  705. public bool IsBookletBothPage(PrintModInfo printModInfo)
  706. {
  707. if (printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  708. {
  709. var bookletInfo = (BookletInfo)printModInfo;
  710. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  711. {
  712. return true;
  713. }
  714. }
  715. return false;
  716. }
  717. /// <summary>
  718. /// 确认打印,单面,双面或手动双面
  719. /// 大小模式支持单面,双面
  720. /// 海报模式只支持单面
  721. /// 多页模式支持单面,双面
  722. /// 小册子模式双面打印:双面
  723. /// 仅正面/背面:单面
  724. ///
  725. /// 单面打印:正常传参
  726. ///
  727. /// 双面打印:
  728. /// 长边翻页,短边翻页
  729. ///
  730. /// 自动双面打印
  731. /// 传值给打印机就行
  732. ///
  733. /// 手动双面打印
  734. /// 长边翻页 先打印奇数页,进入二阶段,正向打印偶数页
  735. /// 短边翻页 先打印奇数页,进入二阶段,反向打印偶数页
  736. ///
  737. /// 异常退出是否要挂起打印机线程?
  738. ///
  739. /// </summary>
  740. public void ConfirmPrint()
  741. {
  742. printQueue = localDefaultPrintServer.GetPrintQueue(PrinterName);
  743. LoadPrinterSettings();
  744. Collection<Duplexing> list = new Collection<Duplexing>();
  745. if (printQueue.GetPrintCapabilities().DuplexingCapability.Count > 0)
  746. {
  747. list.Clear();
  748. for (int temp = 0; temp < printQueue.GetPrintCapabilities().DuplexingCapability.Count; temp++)
  749. {
  750. list.Add(printQueue.GetPrintCapabilities().DuplexingCapability[temp]);
  751. }
  752. }
  753. ///单面打印的情况:直接执行
  754. if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusNone && !IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  755. {
  756. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  757. }
  758. else if (IsBookletSinglePage(PrintSettingsInfo.PrintModInfo))
  759. {
  760. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  761. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  762. }
  763. ///双面打印的情况
  764. else if ((int)PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod > 0 || IsBookletBothPage(PrintSettingsInfo.PrintModInfo))
  765. {
  766. ///支持自动双面
  767. if (list.Count > 0)
  768. {
  769. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  770. }
  771. ///手动翻面
  772. ///手动翻面需要注意只有一页时和单面打印一致
  773. ///那奇数页呢?
  774. else
  775. {
  776. if ((PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize && PrintSettingsInfo.PageRangeList.Count == 1) || (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet && PrintSettingsInfo.PageRangeList.Count == 1))
  777. {
  778. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  779. }
  780. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  781. {
  782. var multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  783. if (PrintSettingsInfo.PageRangeList.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0)
  784. {
  785. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusNone, Unicode = this.Unicode });
  786. }
  787. else
  788. {
  789. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  790. 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)
  791. {
  792. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  793. }
  794. }
  795. }
  796. else
  797. {
  798. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusFrontSide, Unicode = this.Unicode });
  799. 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)
  800. {
  801. this.printEvent.GetEvent<SendPrintQueueEvent>().Publish(new PrintQueueWithUnicode { printQueue = this.printQueue, EnumBothSidesStage = EnumBothSidesStage.StatusBackSide, Unicode = this.Unicode });
  802. }
  803. }
  804. }
  805. }
  806. RequestClose.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
  807. }
  808. public event Action<IDialogResult> RequestClose;
  809. public bool CanCloseDialog()
  810. {
  811. return true;
  812. }
  813. public void OnDialogClosed()
  814. {
  815. this.printQueue.Dispose();
  816. }
  817. public void OnDialogOpened(IDialogParameters parameters)
  818. {
  819. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  820. if (PDFViewer != null && PDFViewer.Document != null)
  821. {
  822. for (int temp = 0; temp < PDFViewer.Document.PageCount; temp++)
  823. {
  824. PrintSettingsInfo.PageRangeList.Add(temp);
  825. }
  826. if (parameters.TryGetValue<int>(ParameterNames.PrintCurrentPage, out printCurrentPageIndex))
  827. {
  828. isCurrentPage = true;
  829. }
  830. else
  831. {
  832. isCurrentPage = false;
  833. }
  834. System.Windows.Size pageSize = PDFViewer.Document.GetPageSize(0);
  835. if ((pageSize.Height / pageSize.Width > 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width < 1.0)
  836. || (pageSize.Height / pageSize.Width < 1.0 && (double)PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height / PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width > 1.0))
  837. {
  838. PrintSettingsInfo.PrintDocument.DefaultPageSettings.Landscape = true;
  839. PrintOrientationIndex = 0;
  840. PrintSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  841. }
  842. MaxPageRange = PDFViewer.Document.PageCount;
  843. NavigationParameters param = new NavigationParameters();
  844. NavigateDocumentRegion();
  845. NavigateModRegion();
  846. }
  847. }
  848. }
  849. }