HomePagePrinterDocumentContentViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.EventAggregators;
  3. using PDF_Office.Model;
  4. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  13. {
  14. public class HomePagePrinterDocumentContentViewModel : BindableBase, INavigationAware
  15. {
  16. private CPDFViewer PDFViewer;
  17. public PrintSettingsInfo PrintSettingsInfo;
  18. public IEventAggregator printDocumentEvent;
  19. private string _paperWidth;
  20. public string PaperWidth
  21. {
  22. get => _paperWidth;
  23. set => SetProperty(ref _paperWidth, value);
  24. }
  25. private string _paperHeight;
  26. public string PaperHeight
  27. {
  28. get => _paperHeight;
  29. set => SetProperty(ref _paperHeight, value);
  30. }
  31. private string _viewBoxWidth;
  32. public string ViewBoxWidth
  33. {
  34. get => _viewBoxWidth;
  35. set => SetProperty(ref _viewBoxWidth, value);
  36. }
  37. private string _viewBoxHeight;
  38. public string ViewBoxHeight
  39. {
  40. get => _viewBoxHeight;
  41. set => SetProperty(ref _viewBoxHeight, value);
  42. }
  43. /// <summary>
  44. /// 打印的总页数
  45. /// </summary>
  46. private int _printedPageCount;
  47. public int PrintedPageCount
  48. {
  49. get => _printedPageCount;
  50. set => SetProperty(ref _printedPageCount, value);
  51. }
  52. public HomePagePrinterDocumentContentViewModel(IEventAggregator eventAggregator)
  53. {
  54. this.printDocumentEvent = eventAggregator;
  55. printDocumentEvent.GetEvent<SendPrintSettingsInfoEvent>().Subscribe(RecvPrintSettingsInfo);
  56. }
  57. public void RecvPrintSettingsInfo(PrintSettingsInfo printSettingsInfo)
  58. {
  59. if (PDFViewer != null && printSettingsInfo != null)
  60. {
  61. SetPreview();
  62. CaculatePrintedPageCount();
  63. }
  64. }
  65. public void SetViewBox()
  66. {
  67. if (float.Parse(PaperHeight) / float.Parse(PaperWidth) >= (306.0 / 224.0))
  68. {
  69. ViewBoxHeight = "306";
  70. ViewBoxWidth = (float.Parse(PaperWidth) / float.Parse(PaperHeight) * 306).ToString();
  71. }
  72. else
  73. {
  74. ViewBoxWidth = "224";
  75. ViewBoxHeight = (float.Parse(PaperHeight) / float.Parse(PaperWidth) * 224).ToString();
  76. }
  77. }
  78. public void SetPreview()
  79. {
  80. if (PrintSettingsInfo.printModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
  81. {
  82. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  83. {
  84. PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
  85. PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
  86. }
  87. else
  88. {
  89. PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
  90. PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
  91. }
  92. SetViewBox();
  93. }
  94. else
  95. {
  96. }
  97. }
  98. public void CaculatePrintedPageCount()
  99. {
  100. PrintedPageCount = PDFViewer.Document.PageCount;
  101. if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusOddRange)
  102. {
  103. if (PrintedPageCount % 2 == 0)
  104. {
  105. PrintedPageCount = PrintedPageCount / 2;
  106. }
  107. else
  108. {
  109. PrintedPageCount = PrintedPageCount / 2 + 1;
  110. }
  111. }
  112. else if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusEvenRange)
  113. {
  114. if (PrintedPageCount % 2 == 0)
  115. {
  116. PrintedPageCount = PrintedPageCount / 2;
  117. }
  118. else
  119. {
  120. PrintedPageCount = PrintedPageCount / 2;
  121. }
  122. }
  123. else if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusCustomizedRange)
  124. {
  125. PrintedPageCount = PrintSettingsInfo.CustomRange.Count;
  126. }
  127. if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  128. {
  129. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
  130. if (PrintedPageCount % (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) != 0)
  131. {
  132. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) + 1;
  133. }
  134. else
  135. {
  136. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber);
  137. }
  138. }
  139. else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  140. {
  141. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
  142. if (PrintedPageCount == 1)
  143. {
  144. PrintedPageCount = 1;
  145. }
  146. else
  147. {
  148. PrintedPageCount = (bookletInfo.EndPaperIndex - bookletInfo.BeginPaperIndex) * 2;
  149. //PrintedPageCount = (PrintedPageCount%4 == 0)?(PrintedPageCount/2):((PrintedPageCount / 4) + 1) * 2;
  150. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly ||
  151. bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly)
  152. {
  153. PrintedPageCount /= 2;
  154. }
  155. }
  156. }
  157. }
  158. public bool IsNavigationTarget(NavigationContext navigationContext)
  159. {
  160. return true;
  161. }
  162. public void OnNavigatedFrom(NavigationContext navigationContext)
  163. {
  164. }
  165. public void OnNavigatedTo(NavigationContext navigationContext)
  166. {
  167. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  168. navigationContext.Parameters.TryGetValue<PrintSettingsInfo>(ParameterNames.PrintSettingsInfo, out PrintSettingsInfo);
  169. if (PDFViewer != null && PrintSettingsInfo != null)
  170. {
  171. SetPreview();
  172. CaculatePrintedPageCount();
  173. }
  174. }
  175. }
  176. }