HomePagePrinterDialogViewModel.cs 40 KB

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