HomePagePrinterDialogViewModel.cs 39 KB

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