ToolsBarContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Office.Interop.Word;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.EventAggregators;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Linq;
  18. using System.Security.Cryptography.X509Certificates;
  19. using System.Windows;
  20. using System.Threading.Tasks;
  21. using static PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
  22. using Task = System.Threading.Tasks.Task;
  23. using PDF_Master.Model.Dialog.ToolsDialogs;
  24. using ComPDFKitViewer;
  25. namespace PDF_Master.ViewModels.Tools
  26. {
  27. public class ToolsBarContentViewModel : BindableBase, INavigationAware
  28. {
  29. #region 文案
  30. private string T_setPassWord;
  31. public string T_SetPassWord
  32. {
  33. get { return T_setPassWord; }
  34. set
  35. {
  36. SetProperty(ref T_setPassWord, value);
  37. }
  38. }
  39. private string T_removePassword;
  40. public string T_RemovePassword
  41. {
  42. get { return T_removePassword; }
  43. set
  44. {
  45. SetProperty(ref T_removePassword, value);
  46. }
  47. }
  48. private void InitString()
  49. {
  50. T_SetPassWord = App.MainPageLoader.GetString("Security_SetPassword");
  51. T_RemovePassword = App.MainPageLoader.GetString("Security_RemovePassword");
  52. }
  53. #endregion
  54. private CPDFViewer PDFViewer;
  55. private ViewContentViewModel viewContentViewModel;
  56. public IDialogService dialogs;
  57. public CropPageUndoManager UndoManager = new CropPageUndoManager();
  58. public IEventAggregator eventAggregator;
  59. List<int> cropCurrentPageList = new List<int>();
  60. private CPDFDocument.GetPageIndexDelegate indexDelegate = null;
  61. public string unicode = null;
  62. /// <summary>
  63. /// 是否是第一次加载
  64. /// </summary>
  65. private bool isFirstLoad = true;
  66. private string _pageIndex = "0";
  67. ///// <value>
  68. ///// 当前页
  69. ///// </value>
  70. //public string PageIndex
  71. //{
  72. // get { return _pageIndex; }
  73. // set
  74. // {
  75. // SetProperty(ref _pageIndex, value);
  76. // App.mainWindowViewModel.Value = int.Parse(PageIndex);
  77. // }
  78. //}
  79. //private CPDFDocument tempDocument;
  80. //private IntPtr compressingIntpr = IntPtr.Zero;
  81. //private string _pageNumber = "";
  82. ///// <value>
  83. ///// 总页数
  84. ///// </value>
  85. //public string PageNumber
  86. //{
  87. // get { return _pageNumber; }
  88. // set
  89. // {
  90. // SetProperty(ref _pageNumber, value);
  91. // App.mainWindowViewModel.MaxValue = int.Parse(PageNumber);
  92. // }
  93. //}
  94. public DelegateCommand CompressCommand { get; set; }
  95. public DelegateCommand MergeCommand { get; set; }
  96. public DelegateCommand SetPasswordCommand { get; set; }
  97. public DelegateCommand CancelPasswordCommand { get; set; }
  98. public DelegateCommand<object> SetEditToolsCommand { get; set; }
  99. public DelegateCommand SetWatermarkCommand { get; set; }
  100. public DelegateCommand CropCurrentPageWMCommand { get; set; }
  101. public DelegateCommand CropAllPagesWMCommand { get; set; }
  102. public ToolsBarContentViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
  103. {
  104. InitString();
  105. dialogs = dialogService;
  106. this.eventAggregator = eventAggregator;
  107. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  108. CompressCommand = new DelegateCommand(OpenCompressDialog);
  109. MergeCommand = new DelegateCommand(MergeDialog);
  110. SetPasswordCommand = new DelegateCommand(OpenSetPasswordDialog);
  111. CancelPasswordCommand = new DelegateCommand(OpenCancelPasswordDialog);
  112. SetEditToolsCommand = new DelegateCommand<object>(SetEditTools);
  113. CropCurrentPageWMCommand = new DelegateCommand(CropCurrentPageWM);
  114. CropAllPagesWMCommand = new DelegateCommand(CropAllPagesWM);
  115. }
  116. private void SetEditTools(object e)
  117. {
  118. var args = e as System.Windows.Controls.Button;
  119. if (args != null)
  120. {
  121. this.eventAggregator.GetEvent<EnterSelectedEditToolEvent>().Publish(new StringWithUnicode() { Unicode = unicode, EditToolsContentName = args.Name });
  122. }
  123. }
  124. private void OpenCompressDialog()
  125. {
  126. DialogParameters value = new DialogParameters();
  127. value.Add(ParameterNames.PDFViewer, PDFViewer);
  128. dialogs.ShowDialog(DialogNames.CompressDialog, value, e =>
  129. {
  130. CompressDialogModel compressDialogModel = new CompressDialogModel();
  131. compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
  132. // OnOpened((Prism.Services.Dialogs.DialogResult)e);
  133. });
  134. }
  135. //private int GetIndex(int pageindex)
  136. //{
  137. // PageIndex = (pageindex + 1).ToString();
  138. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  139. // if (PageIndex == PageNumber) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  140. // return 0;
  141. //}
  142. //private void CompressClear()
  143. //{
  144. // tempDocument.CompressFile_Cancel(compressingIntpr);
  145. // App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  146. //}
  147. //public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  148. //{
  149. // if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel) { return; }
  150. // CPDFDocument doc = null;
  151. // string filepath = "";
  152. // string password = "";
  153. // int compressquality = 45;
  154. // doc = e.Parameters.GetValue<CPDFDocument>(ParameterNames.PDFDocument);
  155. // filepath = e.Parameters.GetValue<string>(ParameterNames.FilePath);
  156. // password = e.Parameters.GetValue<string>(ParameterNames.PassWord);
  157. // compressquality = e.Parameters.GetValue<int>("compressDialogModel.CompressQuality");
  158. // if (doc != null)
  159. // {
  160. // PageNumber = doc.PageCount.ToString();
  161. // CPDFDocument document = CPDFDocument.InitWithFilePath(doc.FilePath);
  162. // document.UnlockWithPassword(password);
  163. // indexDelegate += GetIndex;
  164. // compressingIntpr = document.CompressFile_Init(compressquality, indexDelegate);
  165. // //GC.KeepAlive(indexDelegate);
  166. // tempDocument = document;
  167. // Trace.WriteLine("compressDialogModel.CompressQuality" + compressquality);
  168. // App.mainWindowViewModel.ProcessCloseAction = CompressClear;
  169. // await Task.Run<bool>(() => { return document.CompressFile_Start(compressingIntpr, filepath); });
  170. // CommonHelper.ShowFileBrowser(filepath);
  171. // document.Release();
  172. // }
  173. //}
  174. private void MergeDialog()
  175. {
  176. DialogParameters value = new DialogParameters();
  177. value.Add(ParameterNames.PDFViewer, PDFViewer);
  178. value.Add(ParameterNames.FilePath, PDFViewer.Document.FilePath);
  179. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  180. }
  181. private void OpenSetPasswordDialog()
  182. {
  183. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  184. if (result.IsDiscryptied)
  185. {
  186. if (result.Password != null)
  187. {
  188. PDFViewer.Document.CheckOwnerPassword(result.Password);
  189. }
  190. DialogParameters value = new DialogParameters();
  191. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  192. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  193. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  194. {
  195. if (e.Result == ButtonResult.OK)
  196. {
  197. SecurityHelper.IsPasswordChanged = true;
  198. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  199. {
  200. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  201. }
  202. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  203. }
  204. });
  205. }
  206. }
  207. private void OpenCancelPasswordDialog()
  208. {
  209. ///无可用安全性设置的原因:
  210. ///文件本身无安全性设置,且没有设置过密码
  211. ///文件安全性已经被重置
  212. ///
  213. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  214. {
  215. MessageBoxEx.Show("No security settings available ");
  216. }
  217. else
  218. {
  219. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  220. if (result.IsDiscryptied)
  221. {
  222. if (result.Password != null)
  223. {
  224. PDFViewer.Document.CheckOwnerPassword(result.Password);
  225. }
  226. DialogParameters value = new DialogParameters();
  227. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  228. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  229. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  230. {
  231. if (e.Result == ButtonResult.OK)
  232. {
  233. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  234. {
  235. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  236. }
  237. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  238. }
  239. });
  240. }
  241. }
  242. }
  243. private async void CropCurrentPageWM()
  244. {
  245. viewContentViewModel.IsLoading = Visibility.Visible;
  246. await WaitCropCurrentPageWM();
  247. viewContentViewModel.IsLoading = Visibility.Collapsed;
  248. }
  249. private async void CropAllPagesWM()
  250. {
  251. viewContentViewModel.IsLoading = Visibility.Visible;
  252. await WaitCropAllPagesWM();
  253. viewContentViewModel.IsLoading = Visibility.Collapsed;
  254. //PDFViewer.UndoManager.AddHistory();
  255. }
  256. private async Task WaitCropCurrentPageWM()
  257. {
  258. //付费锁
  259. if (!App.IsLogin)
  260. {
  261. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  262. return;
  263. }
  264. await Task.Run(() =>
  265. {
  266. if (PDFViewer.CurrentIndex >= 0)
  267. {
  268. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  269. }
  270. UndoManager.ADDCropCurrentPageWM();
  271. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  272. PDFViewer.UndoManager.AddHistory(UndoManager);
  273. PDFViewer.UndoManager.CanSave = true;
  274. });
  275. }
  276. private async Task WaitCropAllPagesWM()
  277. {
  278. //付费锁
  279. if (!App.IsLogin)
  280. {
  281. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  282. return;
  283. }
  284. await Task.Run(() =>
  285. {
  286. List<int> cropPagesList = new List<int>();
  287. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  288. {
  289. cropPagesList.Add(i);
  290. }
  291. UndoManager.cropPageList.AddRange(cropPagesList);
  292. UndoManager.ADDCropAllPagesWM();
  293. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  294. PDFViewer.UndoManager.AddHistory(UndoManager);
  295. PDFViewer.UndoManager.CanSave = true;
  296. });
  297. }
  298. public bool IsNavigationTarget(NavigationContext navigationContext)
  299. {
  300. return true;
  301. }
  302. public void OnNavigatedFrom(NavigationContext navigationContext)
  303. {
  304. }
  305. public void OnNavigatedTo(NavigationContext navigationContext)
  306. {
  307. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  308. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  309. if (PDFViewer != null)
  310. {
  311. UndoManager.setPDFViewer(PDFViewer);
  312. //DFViewer.UndoManager.AddHistory(UndoManager);
  313. isFirstLoad = false;
  314. }
  315. }
  316. }
  317. }