ToolsBarContentViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. dialogs.ShowDialog(DialogNames.MergeDialog, value, e => { });
  179. }
  180. private void OpenSetPasswordDialog()
  181. {
  182. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  183. if (result.IsDiscryptied)
  184. {
  185. if (result.Password != null)
  186. {
  187. //string filePath = PDFViewer.Document.FilePath;
  188. //PDFViewer.CloseDocument();
  189. //PDFViewer.InitDocument(filePath);
  190. //if (PDFViewer.Document != null)
  191. //{
  192. // PDFViewer.Load();
  193. //}
  194. PDFViewer.Document.CheckOwnerPassword(result.Password);
  195. }
  196. DialogParameters value = new DialogParameters();
  197. value.Add(ParameterNames.PDFViewer, this.PDFViewer);
  198. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  199. dialogs.ShowDialog(DialogNames.SetPasswordDialog, value, e =>
  200. {
  201. if (e.Result == ButtonResult.OK)
  202. {
  203. SecurityHelper.IsPasswordChanged = true;
  204. if (viewContentViewModel.SecurityInfo.IsPasswordChanged)
  205. {
  206. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  207. }
  208. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusSetPasswordSuccessfully, Unicode = unicode });
  209. }
  210. });
  211. }
  212. }
  213. private void OpenCancelPasswordDialog()
  214. {
  215. ///无可用安全性设置的原因:
  216. ///文件本身无安全性设置,且没有设置过密码
  217. ///文件安全性已经被重置
  218. ///
  219. if ((!PDFViewer.Document.IsEncrypted && !viewContentViewModel.SecurityInfo.IsPasswordChanged) || (viewContentViewModel.SecurityInfo.IsPasswordRemoved))
  220. {
  221. MessageBoxEx.Show("No security settings available ");
  222. }
  223. else
  224. {
  225. VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(PDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
  226. if (result.IsDiscryptied)
  227. {
  228. if (result.Password != null)
  229. {
  230. PDFViewer.Document.CheckOwnerPassword(result.Password);
  231. }
  232. DialogParameters value = new DialogParameters();
  233. value.Add(ParameterNames.PDFDocument, PDFViewer.Document);
  234. value.Add(ParameterNames.ViewContentViewModel, this.viewContentViewModel);
  235. dialogs.ShowDialog(DialogNames.DeleteSafetySettingsDialog, value, e =>
  236. {
  237. if (e.Result == ButtonResult.OK)
  238. {
  239. if (viewContentViewModel.SecurityInfo.IsPasswordRemoved)
  240. {
  241. viewContentViewModel.PDFViewer.UndoManager.CanSave = true;
  242. }
  243. this.eventAggregator.GetEvent<ShowTipEvent>().Publish(new ShowTipEventArgs() { enumTipKind = EnumTipKind.StatusRemoveSecuritySuccessfully, Unicode = unicode });
  244. }
  245. });
  246. }
  247. }
  248. }
  249. private async void CropCurrentPageWM()
  250. {
  251. viewContentViewModel.IsLoading = Visibility.Visible;
  252. await WaitCropCurrentPageWM();
  253. viewContentViewModel.IsLoading = Visibility.Collapsed;
  254. }
  255. private async void CropAllPagesWM()
  256. {
  257. viewContentViewModel.IsLoading = Visibility.Visible;
  258. await WaitCropAllPagesWM();
  259. viewContentViewModel.IsLoading = Visibility.Collapsed;
  260. //PDFViewer.UndoManager.AddHistory();
  261. }
  262. private async Task WaitCropCurrentPageWM()
  263. {
  264. //付费锁
  265. if (!App.IsLogin)
  266. {
  267. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  268. return;
  269. }
  270. await Task.Run(() =>
  271. {
  272. if (PDFViewer.CurrentIndex >= 0)
  273. {
  274. UndoManager.cropPageList.Add(PDFViewer.CurrentIndex);
  275. }
  276. UndoManager.ADDCropCurrentPageWM();
  277. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  278. PDFViewer.UndoManager.AddHistory(UndoManager);
  279. PDFViewer.UndoManager.CanSave = true;
  280. });
  281. }
  282. private async Task WaitCropAllPagesWM()
  283. {
  284. //付费锁
  285. if (!App.IsLogin)
  286. {
  287. dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  288. return;
  289. }
  290. await Task.Run(() =>
  291. {
  292. List<int> cropPagesList = new List<int>();
  293. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  294. {
  295. cropPagesList.Add(i);
  296. }
  297. UndoManager.cropPageList.AddRange(cropPagesList);
  298. UndoManager.ADDCropAllPagesWM();
  299. PDFViewer?.SetCropMode(true, UndoManager.cropPageList);
  300. PDFViewer.UndoManager.AddHistory(UndoManager);
  301. PDFViewer.UndoManager.CanSave = true;
  302. });
  303. }
  304. public bool IsNavigationTarget(NavigationContext navigationContext)
  305. {
  306. return true;
  307. }
  308. public void OnNavigatedFrom(NavigationContext navigationContext)
  309. {
  310. }
  311. public void OnNavigatedTo(NavigationContext navigationContext)
  312. {
  313. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  314. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  315. if (PDFViewer != null)
  316. {
  317. UndoManager.setPDFViewer(PDFViewer);
  318. //DFViewer.UndoManager.AddHistory(UndoManager);
  319. isFirstLoad = false;
  320. }
  321. }
  322. }
  323. }