SubscriptionDialogViewModel.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using ComPDFKit.PDFWatermark;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.Helper;
  4. using PDF_Master.Model;
  5. using PDF_Master.Model.EditTools.Watermark;
  6. using PDF_Master.Properties;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Resources;
  21. namespace PDF_Master.ViewModels.Dialog.ServiceDialog
  22. {
  23. public class SubscriptionDialogViewModel : BindableBase, IDialogAware
  24. {
  25. public string Title => "";
  26. private WatermarkInfo watermarkInfo;
  27. private CPDFWatermark watermark;
  28. private ViewContentViewModel viewContentViewModel;
  29. public event Action<IDialogResult> RequestClose;
  30. #region 文案
  31. #endregion
  32. /// <summary>
  33. /// 价格优惠bord
  34. /// </summary>
  35. private Visibility _Vishalfprice = Visibility.Visible;
  36. public Visibility Vishalfprice
  37. {
  38. get { return _Vishalfprice; }
  39. set
  40. {
  41. SetProperty(ref _Vishalfprice, value);
  42. }
  43. }
  44. /// <summary>
  45. /// 价格优惠
  46. /// </summary>
  47. private string _Textprice = "$39.99/year";
  48. public string Textprice
  49. {
  50. get { return _Textprice; }
  51. set
  52. {
  53. SetProperty(ref _Textprice, value);
  54. }
  55. }
  56. private string _uristore = "https://www.pdfreaderpro.com/windows/store/permanent";
  57. public string Uristore
  58. {
  59. get { return _uristore; }
  60. set
  61. {
  62. SetProperty(ref _uristore, value);
  63. }
  64. }
  65. public DelegateCommand LinkstoreCommand { get; set; }
  66. public DelegateCommand WarermarkSavingCommand { get; set; }
  67. public DelegateCommand RefreshCommand { get; set; }
  68. private void InitString()
  69. {
  70. }
  71. public SubscriptionDialogViewModel(IRegionManager regionManager, IDialogService dialogService)
  72. {
  73. RefreshCommand = new DelegateCommand(Refresh);
  74. LinkstoreCommand = new DelegateCommand(Linkstore);
  75. WarermarkSavingCommand = new DelegateCommand(WarermarkSaving);
  76. InitString();
  77. if (Settings.Default.UserDate.subscribestatus == 2)
  78. {
  79. Textprice = "$79.99/year";
  80. Vishalfprice = Visibility.Collapsed;
  81. }
  82. else
  83. {
  84. Textprice = "$39.99/year";
  85. Vishalfprice = Visibility.Visible;
  86. }
  87. }
  88. private void Linkstore()
  89. {
  90. Process.Start(new ProcessStartInfo(Uristore));
  91. }
  92. /// <summary>
  93. /// 水印保存
  94. /// </summary>
  95. private void WarermarkSaving()
  96. {
  97. CPDFViewer cPDFViewer = new CPDFViewer();
  98. Uri resourceUri = new Uri("pack://application:,,,/PDF Master;component/Resources/Service/Warermark.png");
  99. StreamResourceInfo resourceInfo = Application.GetResourceStream(resourceUri);
  100. if (resourceInfo != null)
  101. {
  102. using (Stream stream = resourceInfo.Stream)
  103. {
  104. cPDFViewer = viewContentViewModel.PDFViewer;
  105. BitmapFrame frame = null;
  106. int width = 0;
  107. int height = 0;
  108. BitmapDecoder decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
  109. if (decoder != null && decoder.Frames.Count > 0)
  110. {
  111. frame = decoder.Frames[0];
  112. }
  113. if (frame != null)
  114. {
  115. var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  116. width = frame.PixelWidth;
  117. height = frame.PixelHeight;
  118. frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
  119. watermark = cPDFViewer.Document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
  120. watermark.SetImage(ImageArray, width, height);
  121. watermark.SetScale(1);
  122. watermark.SetRotation(0);
  123. watermark.SetOpacity(255);
  124. watermark.SetFront(true);
  125. watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_TOP);
  126. watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_LEFT);
  127. watermark.SetFullScreen(false);
  128. watermark.SetVertOffset(0);
  129. watermark.SetHorizOffset(0);
  130. watermark.SetHorizontalSpacing(0);
  131. watermark.SetVerticalSpacing(0);
  132. string setpages = $"0-{cPDFViewer.Document.PageCount.ToString()}";
  133. watermark.SetPages(setpages);
  134. watermark.UpdateWatermark();
  135. cPDFViewer.Document.ReleasePages();
  136. cPDFViewer.ReloadDocument();
  137. viewContentViewModel.saveAsFile(null,cPDFViewer);
  138. Close();
  139. }
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 刷新订阅状态按钮
  145. /// </summary>
  146. private void Refresh()
  147. {
  148. if (Settings.Default.AppProperties.LoginToken != "")
  149. {
  150. if (ServiceHelper.GetUser() == "401")
  151. {
  152. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  153. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  154. App.mainWindowViewModel.OphVis = Visibility.Visible;
  155. if (Settings.Default.UserDate.IsLoginoff == false)
  156. {
  157. App.mainWindowViewModel.OpenLoginoff();
  158. Settings.Default.UserDate.IsLoginoff = true;
  159. Settings.Default.Save();
  160. }
  161. }
  162. }
  163. else
  164. {
  165. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  166. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  167. App.mainWindowViewModel.OphVis = Visibility.Visible;
  168. }
  169. if (Settings.Default.UserDate.subscribestatus == 1&&App.IsLogin == true)
  170. {
  171. Close();
  172. }
  173. }
  174. public void Close()
  175. {
  176. RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
  177. }
  178. public bool CanCloseDialog()
  179. {
  180. return true;
  181. }
  182. public void OnDialogClosed()
  183. {
  184. }
  185. public void OnDialogOpened(IDialogParameters parameters)
  186. {
  187. parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  188. }
  189. }
  190. }